Skip to content

Commit

Permalink
Support distributing to store (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Pan authored May 20, 2019
1 parent f16a74f commit 0050a18
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 75 deletions.
11 changes: 11 additions & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ lane :test do
destination_type: "group"
)

UI.message("\n\n\n=====================================\n uploading to store \n=====================================")

appcenter_upload(
api_token: ENV["TEST_APPCENTER_API_TOKEN"],
owner_name: ENV["TEST_APPCENTER_OWNER_NAME"],
app_name: "MyApplication",
apk: "./fastlane/app-release.apk",
destinations: ENV["TEST_APPCENTER_DISTRIBUTE_STORE"],
destination_type: "store"
)

UI.message("\n\n\n=====================================\n uploading mandatory release \n=====================================")

appcenter_upload(
Expand Down
22 changes: 11 additions & 11 deletions lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ def self.run_release_upload(params)

destinations_array = destinations.split(',')
destinations_array.each do |destination_name|
group = Helper::AppcenterHelper.get_group(api_token, owner_name, app_name, destination_name)
if group
group_id = group['id']
distributed_release = Helper::AppcenterHelper.add_to_group(api_token, owner_name, app_name, release_id, group_id, mandatory_update, notify_testers)
destination = Helper::AppcenterHelper.get_destination(api_token, owner_name, app_name, destination_type, destination_name)
if destination
destination_id = destination['id']
distributed_release = Helper::AppcenterHelper.add_to_destination(api_token, owner_name, app_name, release_id, destination_type, destination_id, mandatory_update, notify_testers)
if distributed_release
UI.success("Release #{distributed_release['short_version']} was successfully distributed to group \"#{destination_name}\"")
UI.success("Release #{distributed_release['short_version']} was successfully distributed to #{destination_type} \"#{destination_name}\"")
else
UI.error("Release '#{release_id}' was not found")
end
else
UI.error("Group '#{destination_name}' was not found")
UI.error("#{destination_type} '#{destination_name}' was not found")
end
end
end
Expand Down Expand Up @@ -270,32 +270,32 @@ def self.available_options

FastlaneCore::ConfigItem.new(key: :destinations,
env_name: "APPCENTER_DISTRIBUTE_DESTINATIONS",
description: "Comma separated list of destination names. Currently only distribution groups are supported",
description: "Comma separated list of destination names. Both distribution groups and stores are supported. All names are required to be of the same destination type",
default_value: "Collaborators",
optional: true,
type: String),


FastlaneCore::ConfigItem.new(key: :destination_type,
env_name: "APPCENTER_DISTRIBUTE_DESTINATION_TYPE",
description: "Destination type of distribution destination. Currently only 'group' is supported",
description: "Destination type of distribution destination. 'group' and 'store' are supported",
default_value: "group",
optional: true,
type: String,
verify_block: proc do |value|
UI.user_error!("No or incorrect destination type given. Use `destination_type: 'group'`") unless value && !value.empty? && value == "group"
UI.user_error!("No or incorrect destination type given. Use 'group' or 'store'") unless value && !value.empty? && ["group", "store"].include?(value)
end),

FastlaneCore::ConfigItem.new(key: :mandatory_update,
env_name: "APPCENTER_DISTRIBUTE_MANDATORY_UPDATE",
description: "Require users to update to this release",
description: "Require users to update to this release. Ignored if destination type is 'store'",
optional: true,
is_string: false,
default_value: false),

FastlaneCore::ConfigItem.new(key: :notify_testers,
env_name: "APPCENTER_DISTRIBUTE_NOTIFY_TESTERS",
description: "Send email notification about release",
description: "Send email notification about release. Ignored if destination type is 'store'",
optional: true,
is_string: false,
default_value: false),
Expand Down
38 changes: 20 additions & 18 deletions lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,26 +245,26 @@ def self.get_release(api_token, owner_name, app_name, release_id)
end
end

# get distribution group
def self.get_group(api_token, owner_name, app_name, group_name)
# get distribution group or store
def self.get_destination(api_token, owner_name, app_name, destination_type, destination_name)
connection = self.connection

response = connection.get do |req|
req.url("/v0.1/apps/#{owner_name}/#{app_name}/distribution_groups/#{ERB::Util.url_encode(group_name)}")
req.url("/v0.1/apps/#{owner_name}/#{app_name}/distribution_#{destination_type}s/#{ERB::Util.url_encode(destination_name)}")
req.headers['X-API-Token'] = api_token
req.headers['internal-request-source'] = "fastlane"
end

case response.status
when 200...300
group = response.body
UI.message("DEBUG: received group #{JSON.pretty_generate(group)}") if ENV['DEBUG']
group
destination = response.body
UI.message("DEBUG: received #{destination_type} #{JSON.pretty_generate(destination)}") if ENV['DEBUG']
destination
when 404
UI.error("Not found, invalid distribution group name")
UI.error("Not found, invalid distribution #{destination_type} name")
false
else
UI.error("Error getting group #{response.status}: #{response.body}")
UI.error("Error getting #{destination_type} #{response.status}: #{response.body}")
false
end
end
Expand Down Expand Up @@ -306,21 +306,23 @@ def self.update_release(api_token, owner_name, app_name, release_id, release_not
end
end

# add release to distribution group
def self.add_to_group(api_token, owner_name, app_name, release_id, group_id, mandatory_update = false, notify_testers = false)
# add release to distribution group or store
def self.add_to_destination(api_token, owner_name, app_name, release_id, destination_type, destination_id, mandatory_update = false, notify_testers = false)
connection = self.connection

UI.message("DEBUG: getting #{release_id}") if ENV['DEBUG']

body = { "id" => destination_id }
if destination_type == "group"
body["mandatory_update"] = mandatory_update
body["notify_testers"] = notify_testers
end

response = connection.post do |req|
req.url("/v0.1/apps/#{owner_name}/#{app_name}/releases/#{release_id}/groups")
req.url("/v0.1/apps/#{owner_name}/#{app_name}/releases/#{release_id}/#{destination_type}s")
req.headers['X-API-Token'] = api_token
req.headers['internal-request-source'] = "fastlane"
req.body = {
"id" => group_id,
"mandatory_update" => mandatory_update,
"notify_testers" => notify_testers
}
req.body = body
end

case response.status
Expand All @@ -339,10 +341,10 @@ def self.add_to_group(api_token, owner_name, app_name, release_id, group_id, man

release
when 404
UI.error("Not found, invalid distribution group name")
UI.error("Not found, invalid distribution #{destination_type} name")
false
else
UI.error("Error adding to group #{response.status}: #{response.body}")
UI.error("Error adding to #{destination_type} #{response.status}: #{response.body}")
false
end
end
Expand Down
Loading

0 comments on commit 0050a18

Please sign in to comment.