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



75
76
77
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 75

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

.available_optionsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 79

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

.check_valid_app_name(app_name) ⇒ Object



143
144
145
146
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 143

def self.check_valid_app_name(app_name)
  regexp = /^[a-zA-Z0-9\-]+$/i
  return regexp.match?(app_name)
end

.descriptionObject



71
72
73
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 71

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

.get_apps(api_token) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 132

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)
  return [] unless apps_response.kind_of?(Net::HTTPOK)
  return JSON.parse(apps_response.body)
end

.get_owner_and_app_name(api_token) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 108

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)
  app_matches = apps.select { |app| app['name'] == selected_app_name }
  return unless app_matches.count > 0
  selected_app = app_matches.first

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

.get_owner_name(api_token, app_name) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 121

def self.get_owner_name(api_token, app_name)
  apps = get_apps(api_token)
  return unless apps.count > 0
  app_matches = apps.select { |app| app['name'] == app_name }
  return unless app_matches.count > 0
  selected_app = app_matches.first

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

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 104

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fastlane/plugin/latest_appcenter_build_number/actions/latest_appcenter_build_number_action.rb', line 10

def self.run(config)
  unless check_valid_app_name(config[:app_name])
    UI.user_error!("The `app_name` ('#{config[:app_name]}') cannot contains spaces and must only contain alpha numeric characters and dashes")
    return nil
  end

  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

  if owner_name.nil?
    owner_name = get_owner_name(config[:api_token], app_name)
  end

  if app_name.nil?
    app_name = get_owner_and_app_name(config[:api_token])[0]
  end

  if app_name.nil? || owner_name.nil?
    UI.user_error!("No app '#{app_name}' found for owner #{owner_name}")
    return nil
  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.user_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

  if list_response.kind_of?(Net::HTTPNotFound)
    UI.user_error!("No app or owner found with `app_name`: '#{app_name}' and `owner_name`: '#{owner_name}'")
    return nil
  end

  releases = JSON.parse(list_response.body)
  if releases.nil?
    UI.user_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.user_error!("The app has no versions yet")
    return nil
  end

  return latest_build['version']
end