Skip to content

Commit

Permalink
Merge pull request #62 from evkhramkov/feature/auto-create-app-parame…
Browse files Browse the repository at this point in the history
…ters

Allow specifying app os, platform and display name for automated creation
  • Loading branch information
evkhramkov authored Jun 10, 2019
2 parents 2a3f243 + e78680b commit 8a1d35d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
13 changes: 13 additions & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
48 changes: 38 additions & 10 deletions lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,34 @@ 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'],
"iOS" => ['Objective-C-Swift', 'React-Native', 'Xamarin']
}

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

Expand Down Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,21 +370,21 @@ 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|
req.url("/v0.1/apps")
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
Expand All @@ -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}")
Expand Down
33 changes: 31 additions & 2 deletions spec/appcenter_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}",
Expand Down Expand Up @@ -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')
Expand All @@ -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'
Expand Down

0 comments on commit 8a1d35d

Please sign in to comment.