diff --git a/fastlane/Fastfile b/fastlane/Fastfile index 502e792..f8c7518 100644 --- a/fastlane/Fastfile +++ b/fastlane/Fastfile @@ -16,6 +16,19 @@ lane :test_autocreation do ) end +lane :test_autocreation_without_prompts do + # app should be autocreated if not found, without asking user + appcenter_upload( + api_token: ENV["TEST_APPCENTER_API_TOKEN"], + owner_name: ENV["TEST_APPCENTER_OWNER_NAME"], + app_name: "my-new-app-center-app", + app_display_name: "My New App Center App", + app_os: "Android", + app_platform: "Java", + apk: "./fastlane/app-release.apk" + ) +end + lane :test_release_notes do appcenter_upload( api_token: ENV["TEST_APPCENTER_API_TOKEN"], diff --git a/lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb b/lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb index 3880375..8498bb7 100644 --- a/lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb +++ b/lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb @@ -161,6 +161,9 @@ def self.get_or_create_app(params) api_token = params[:api_token] owner_name = params[:owner_name] app_name = params[:app_name] + app_display_name = params[:app_display_name] + app_os = params[:app_os] + app_platform = params[:app_platform] platforms = { "Android" => ['Java', 'React-Native', 'Xamarin'], @@ -168,17 +171,24 @@ def self.get_or_create_app(params) } if Helper::AppcenterHelper.get_app(api_token, owner_name, app_name) - true - else - if Helper.test? || UI.confirm("App with name #{app_name} not found, create one?") - os = Helper.test? ? "Android" : UI.select("Select OS", ["Android", "iOS"]) - platform = Helper.test? ? "Java" : UI.select("Select Platform", platforms[os]) + return true + end - Helper::AppcenterHelper.create_app(api_token, owner_name, app_name, os, platform) - else - UI.error("Lane aborted") - false - end + should_create_app = !app_display_name.to_s.empty? || !app_os.to_s.empty? || !app_platform.to_s.empty? + + if Helper.test? || should_create_app || UI.confirm("App with name #{app_name} not found, create one?") + app_display_name = app_name if app_display_name.to_s.empty? + os = app_os.to_s.empty? ? + (Helper.test? ? "Android" : UI.select("Select OS", ["Android", "iOS"])) : + app_os + platform = app_platform.to_s.empty? ? + (Helper.test? ? "Java" : UI.select("Select Platform", platforms[os])) : + app_platform + + Helper::AppcenterHelper.create_app(api_token, owner_name, app_name, app_display_name, os, platform) + else + UI.error("Lane aborted") + false end end @@ -238,6 +248,24 @@ def self.available_options UI.user_error!("No App name given, pass using `app_name: 'app name'`") unless value && !value.empty? end), + FastlaneCore::ConfigItem.new(key: :app_display_name, + env_name: "APPCENTER_APP_DISPLAY_NAME", + description: "App display name to use when creating a new app", + optional: true, + type: String), + + FastlaneCore::ConfigItem.new(key: :app_os, + env_name: "APPCENTER_APP_OS", + description: "App OS. Used for new app creation, if app with 'app_name' name was not found", + optional: true, + type: String), + + FastlaneCore::ConfigItem.new(key: :app_platform, + env_name: "APPCENTER_APP_PLATFORM", + description: "App Platform. Used for new app creation, if app with 'app_name' name was not found", + optional: true, + type: String), + FastlaneCore::ConfigItem.new(key: :apk, env_name: "APPCENTER_DISTRIBUTE_APK", description: "Build release path for android build", diff --git a/lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb b/lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb index b81542d..ada5eda 100644 --- a/lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb +++ b/lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb @@ -370,13 +370,13 @@ def self.get_app(api_token, owner_name, app_name) UI.message("DEBUG: #{JSON.pretty_generate(response.body)}\n") if ENV['DEBUG'] false else - UI.error("Error #{response.status}: #{response.body}") + UI.error("Error getting app #{owner}/#{app_name}, #{response.status}: #{response.body}") false end end # returns true if app exists, false in case of 404 and error otherwise - def self.create_app(api_token, owner_name, app_name, os, platform) + def self.create_app(api_token, owner_name, app_name, app_display_name, os, platform) connection = self.connection response = connection.post do |req| @@ -384,7 +384,7 @@ def self.create_app(api_token, owner_name, app_name, os, platform) req.headers['X-API-Token'] = api_token req.headers['internal-request-source'] = "fastlane" req.body = { - "display_name" => app_name, + "display_name" => app_display_name, "name" => app_name, "os" => os, "platform" => platform @@ -395,7 +395,7 @@ def self.create_app(api_token, owner_name, app_name, os, platform) when 200...300 created = response.body UI.message("DEBUG: #{JSON.pretty_generate(created)}") if ENV['DEBUG'] - UI.success("Created #{os}/#{platform} app with name \"#{created['name']}\"") + UI.success("Created #{os}/#{platform} app with name \"#{created['name']}\" and display name \"#{created['display_name']}\"") true else UI.error("Error creating app #{response.status}: #{response.body}") diff --git a/spec/appcenter_upload_spec.rb b/spec/appcenter_upload_spec.rb index 537b23a..2a9146c 100644 --- a/spec/appcenter_upload_spec.rb +++ b/spec/appcenter_upload_spec.rb @@ -6,8 +6,11 @@ def stub_check_app(status) ) end -def stub_create_app(status) +def stub_create_app(status, app_name = "app", app_display_name = "app", app_os = "Android", app_platform = "Java") stub_request(:post, "https://api.appcenter.ms/v0.1/apps") + .with( + body: "{\"display_name\":\"#{app_display_name}\",\"name\":\"#{app_name}\",\"os\":\"#{app_os}\",\"platform\":\"#{app_platform}\"}", + ) .to_return( status: status, body: "{\"name\":\"app\"}", @@ -703,7 +706,30 @@ def stub_add_to_destination(status, destination_type = "group", mandatory_update it "creates app if it was not found" do stub_check_app(404) - stub_create_app(200) + stub_create_app(200, "app", "app", "Android", "Java") + stub_create_release_upload(200) + stub_upload_build(200) + stub_update_release_upload(200, 'committed') + stub_update_release(200) + stub_get_destination(200) + stub_add_to_destination(200) + stub_get_release(200) + + Fastlane::FastFile.new.parse("lane :test do + appcenter_upload({ + api_token: 'xxx', + owner_name: 'owner', + app_name: 'app', + apk: './spec/fixtures/appfiles/apk_file_empty.apk', + destinations: 'Testers', + destination_type: 'group' + }) + end").runner.execute(:test) + end + + it "creates app if it was not found with specified os, platform and display_name" do + stub_check_app(404) + stub_create_app(200, "app", "App Name", "Android", "Java") stub_create_release_upload(200) stub_upload_build(200) stub_update_release_upload(200, 'committed') @@ -717,6 +743,9 @@ def stub_add_to_destination(status, destination_type = "group", mandatory_update api_token: 'xxx', owner_name: 'owner', app_name: 'app', + app_display_name: 'App Name', + app_os: 'Android', + app_platform: 'Java', apk: './spec/fixtures/appfiles/apk_file_empty.apk', destinations: 'Testers', destination_type: 'group'