Class: Fastlane::Actions::LatestAppcenterBuildNumberAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



52
53
54
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 52

def self.authors
  ["jspargo", "ShopKeep", "pahnev", "FlixBus (original author)"]
end

.available_optionsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 56

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "APPCENTER_API_TOKEN",
                                 description: "API Token for AppCenter Access",
                                 verify_block: proc do |value|
                                   UI.user_error!("No API token for AppCenter given, pass using `api_token: 'token'`") unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :owner_name,
                                 env_name: "APPCENTER_OWNER_NAME",
                                 optional: true,
                                 description: "Name of the owner of the application on AppCenter",
                                 verify_block: proc do |value|
                                   UI.user_error!("No owner name for AppCenter given, pass using `owner_name: 'owner name'`") unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :app_name,
                                 env_name: "APPCENTER_APP_NAME",
                                 optional: true,
                                 description: "Name of the application on AppCenter",
                                 verify_block: proc do |value|
                                   UI.user_error!("No app name for AppCenter given, pass using `app_name: 'app name'`") unless value && !value.empty?
                                 end)
  ]
end

.descriptionObject



48
49
50
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 48

def self.description
  "Gets latest version number of the app from AppCenter"
end

.get_apps(api_token) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 96

def self.get_apps(api_token)
  host_uri = URI.parse('https://api.appcenter.ms')
  http = Net::HTTP.new(host_uri.host, host_uri.port)
  http.use_ssl = true
  apps_request = Net::HTTP::Get.new("/v0.1/apps")
  apps_request['X-API-Token'] = api_token
  apps_response = http.request(apps_request)
  apps = JSON.parse(apps_response.body)
  return apps
end

.get_owner_and_app_name(api_token) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 85

def self.get_owner_and_app_name(api_token)
  apps = get_apps(api_token)
  app_names = apps.map { |app| app['name'] }.sort
  selected_app_name = UI.select("Select your project: ", app_names)
  selected_app = apps.select { |app| app['name'] == selected_app_name }.first

  name = selected_app['name'].to_s
  owner = selected_app['owner']['name'].to_s
  return name, owner
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 81

def self.is_supported?(platform)
  [:ios, :android].include?(platform)
end

.run(config) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 10

def self.run(config)
  app_name = config[:app_name]
  owner_name = config[:owner_name]
  if app_name.nil? || owner_name.nil?
    owner_and_app_name = get_owner_and_app_name(config[:api_token])
    app_name = owner_and_app_name[0]
    owner_name = owner_and_app_name[1]
  end

  host_uri = URI.parse('https://api.appcenter.ms')
  http = Net::HTTP.new(host_uri.host, host_uri.port)
  http.use_ssl = true
  list_request = Net::HTTP::Get.new("/v0.1/apps/#{owner_name}/#{app_name}/releases")
  list_request['X-API-Token'] = config[:api_token]
  list_response = http.request(list_request)

  if list_response.kind_of?(Net::HTTPForbidden)
    UI.error("API Key not valid for #{owner_name}. This will be because either the API Key or the owner_name are incorrect")
    return nil
  end

  releases = JSON.parse(list_response.body)
  if releases.nil?
    UI.error("No versions found for #{app_name} owned by #{owner_name}")
    return nil
  end

  releases.sort_by { |release| release['id'] }
  latest_build = releases.first

  if latest_build.nil?
    UI.error("The app has no versions yet")
    return nil
  end

  return latest_build['version']
end