Class: Fastlane::Actions::TaiwanNumberOneAction

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

Defined Under Namespace

Modules: ActionResult, DecisionType

Class Method Summary collapse

Class Method Details

.api_token(params) ⇒ Object



164
165
166
167
168
169
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 164

def self.api_token(params)
  params[:api_key] ||= Actions.lane_context[SharedValues::APP_STORE_CONNECT_API_KEY]
  api_token ||= Spaceship::ConnectAPI::Token.create(params[:api_key]) if params[:api_key]
  api_token ||= Spaceship::ConnectAPI::Token.from_json_file(params[:api_key_path]) if params[:api_key_path]
  return api_token
end

.authorsObject



175
176
177
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 175

def self.authors
  ["andrew54068"]
end

.available_optionsObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 194

def self.available_options
  user = CredentialsManager::AppfileConfig.try_fetch_value(:itunes_connect_id)
  user ||= CredentialsManager::AppfileConfig.try_fetch_value(:apple_id)
  [
    FastlaneCore::ConfigItem.new(key: :app_decision,
                                 env_name: "app_decision",
                                 description: "A description of your decision, should be release or reject",
                                 optional: false,
                                 default_value: DecisionType::RELEASE,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :username,
                                 short_option: "-u",
                                 env_name: "username",
                                 description: "Your Apple ID Username",
                                 default_value: user,
                                 default_value_dynamic: true),
    FastlaneCore::ConfigItem.new(key: :app_identifier,
                                 short_option: "-a",
                                 env_name: "app_identifier",
                                 description: "The bundle identifier of your app",
                                 optional: true,
                                 code_gen_sensitive: true,
                                 default_value: CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier),
                                 default_value_dynamic: true),
    # affiliation
    FastlaneCore::ConfigItem.new(key: :team_id,
                                 short_option: "-k",
                                 env_name: "team_id",
                                 description: "The ID of your App Store Connect team if you're in multiple teams",
                                 optional: true,
                                 is_string: false, # as we also allow integers, which we convert to strings anyway
                                 code_gen_sensitive: true,
                                 default_value: CredentialsManager::AppfileConfig.try_fetch_value(:itc_team_id),
                                 default_value_dynamic: true,
                                 verify_block: proc do |value|
                                   ENV["FASTLANE_ITC_TEAM_ID"] = value.to_s
                                 end),
    FastlaneCore::ConfigItem.new(key: :team_name,
                                 short_option: "-e",
                                 env_name: "team_name",
                                 description: "The name of your App Store Connect team if you're in multiple teams",
                                 optional: true,
                                 code_gen_sensitive: true,
                                 default_value: CredentialsManager::AppfileConfig.try_fetch_value(:itc_team_name),
                                 default_value_dynamic: true,
                                 verify_block: proc do |value|
                                   ENV["FASTLANE_ITC_TEAM_NAME"] = value.to_s
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_key_path,
                                 env_name: "FL_REGISTER_DEVICE_API_KEY_PATH",
                                 description: "Path to your App Store Connect API Key JSON file (https://docs.fastlane.tools/app-store-connect-api/#using-fastlane-api-key-json-file)",
                                 optional: true,
                                 conflicting_options: [:api_key],
                                 verify_block: proc do |value|
                                   UI.user_error!("Couldn't find API key JSON file at path '#{value}'") unless File.exist?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_key,
                                 env_name: "FL_REGISTER_DEVICE_API_KEY",
                                 description: "Your App Store Connect API Key information (https://docs.fastlane.tools/app-store-connect-api/#use-return-value-and-pass-in-as-an-option)",
                                 type: Hash,
                                 optional: true,
                                 sensitive: true,
                                 conflicting_options: [:api_key_path]),
    FastlaneCore::ConfigItem.new(key: :force,
                                 env_name: "FL_DECISION_FORCE",
                                 description: "Skip verifying of current version state for reject reviewed version or cancel waiting review version",
                                 is_string: false,
                                 default_value: false),
  ]
end

.descriptionObject



171
172
173
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 171

def self.description
  "release or reject if status is Pending Developer Release."
end

.detailsObject



183
184
185
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 183

def self.details
  "use fastlane to release or reject reviewed version"
end

.example_codeObject



272
273
274
275
276
277
278
279
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 272

def self.example_code
  [
    'taiwan_number_one(
      app_decision: "release",
      api_key: "api_key" # your app_store_connect_api_key
    )'
  ]
end

.fetch_decision(params) ⇒ Object



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

def self.fetch_decision(params)
  decision = params[:app_decision]
  until ["release", "reject"].include?(decision)
    UI.user_error!("App's decision must be release or reject.")
    return
  end
  # return decision
  UI.message("return type #{decision}")
  if decision == DecisionType::RELEASE
    return DecisionType::RELEASE
  else
    return DecisionType::REJECT
  end
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


265
266
267
268
269
270
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 265

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  [:ios, :mac].include?(platform)
end

.outputObject



187
188
189
190
191
192
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 187

def self.output
  [
    [ActionResult::SUCCESS, 'Successfully release or reject.'],
    [ActionResult::DO_NOTHING, 'Do nothing.']
  ]
end

.reject_version_if_possible(app: nil, app_store_version: Spaceship::ConnectAPI::AppStoreVersion) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 149

def self.reject_version_if_possible(app: nil, app_store_version: Spaceship::ConnectAPI::AppStoreVersion)
  unless app
    UI.user_error!("Could not find app with bundle identifier '#{params[:app_identifier]}' on account #{params[:username]}")
    return ActionResult::DO_NOTHING
  end

  if app_store_version.reject!
    UI.success("rejected version #{app_store_version.version_string} Successfully!")
    return ActionResult::SUCCESS
  else
    UI.user_error!("An error occurred while rejected version #{app_store_version}")
    return ActionResult::DO_NOTHING
  end
end

.release_version_if_possible(app: nil, app_store_version: Spaceship::ConnectAPI::AppStoreVersion, token: nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 123

def self.release_version_if_possible(app: nil, app_store_version: Spaceship::ConnectAPI::AppStoreVersion, token: nil)
  unless app
    UI.user_error!("Could not find app with bundle identifier '#{params[:app_identifier]}' on account #{params[:username]}")
    return ActionResult::DO_NOTHING
  end

  begin
    if token 
      now = Time.now
      release_date_string = now.strftime("%Y-%m-%dT%H:00%:z")
      app_store_version.update(attributes: {
          earliest_release_date: release_date_string,
          release_type: Spaceship::ConnectAPI::AppStoreVersion::ReleaseType::SCHEDULED
      })
      return ActionResult::SUCCESS
    else
      app_store_version.create_app_store_version_release_request
      UI.message("release version #{app_store_version.version_string} successfully!")
      return ActionResult::SUCCESS
    end
  rescue => e
    UI.user_error!("An error occurred while releasing version #{app_store_version}, #{e.message}\n#{e.backtrace.join("\n")}")
    return ActionResult::DO_NOTHING
  end
end

.return_valueObject



179
180
181
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 179

def self.return_value
  return "'Success' if action passes, else, 'Nothing has changed'"
end

.run(params) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/taiwan_number_one/actions/taiwan_number_one_action.rb', line 18

def self.run(params)
  begin
    params[:api_key] ||= Actions.lane_context[SharedValues::APP_STORE_CONNECT_API_KEY]

    app_id = params.fetch(:app_identifier)
    username = params.fetch(:username)
    unless app_id && username
      UI.message("Could not find app_id and username.")
      return
    end

    token = self.api_token(params)
    if token
      UI.message("Using App Store Connect API token...")
      Spaceship::ConnectAPI.token = token
    else
      UI.message("Login to App Store Connect (#{params[:username]})")
      Spaceship::ConnectAPI.(
        params[:username],
        use_portal: false,
        use_tunes: true,
        tunes_team_id: params[:team_id],
        team_name: params[:team_name]
      )
      UI.message("Login successful")
    end

    app = Spaceship::ConnectAPI::App.find(app_id)
    version = app.get_app_store_versions.first
    UI.message("app_store_state is #{version.app_store_state}")
    client ||= Spaceship::ConnectAPI
    platform ||= Spaceship::ConnectAPI::Platform::IOS
    filter = {
      appStoreState: [
        Spaceship::ConnectAPI::AppStoreVersion::AppStoreState::PENDING_DEVELOPER_RELEASE
      ].join(","),
      platform: platform
    }
              
    decision ||= fetch_decision(params)
    if params[:force] && decision == DecisionType::REJECT
      UI.message("decision is reject")
      app_store_version = app.get_app_store_versions(client: client, includes: "appStoreVersionSubmission")
                           .sort_by { |v| Gem::Version.new(v.version_string) }
                           .last
      return reject_version_if_possible(app: app, app_store_version: app_store_version)
    end

    app_store_version = app.get_app_store_versions(client: client, filter: filter, includes: "appStoreVersionSubmission")
                           .sort_by { |v| Gem::Version.new(v.version_string) }
                           .last
    if app_store_version
      version_string = app_store_version.version_string
      state = app_store_version.app_store_state
      UI.message("version #{version_string} is #{state}")
      unless state == Spaceship::ConnectAPI::AppStoreVersion::AppStoreState::PENDING_DEVELOPER_RELEASE
        UI.message("AppStoreState is not PENDING_DEVELOPER_RELEASE")
        UI.message("🇹🇼 Taiwan helps you do nothing!")
        return ActionResult::DO_NOTHING
      end

      result = ActionResult::DO_NOTHING
      case decision
      when DecisionType::RELEASE
        UI.message("decision is release")
        result = release_version_if_possible(app: app, app_store_version: app_store_version, token: token)
      when DecisionType::REJECT
        UI.message("decision is reject")
        result = reject_version_if_possible(app: app, app_store_version: app_store_version)
      else
        UI.user_error!("App's decision must be release or reject")
        result = ActionResult::DO_NOTHING
      end

      UI.message("The taiwan_number_one plugin action is finished!")
      UI.message("🇹🇼 Taiwan can help!")
      return result
    else
      UI.message("no pending release version exist.")
      UI.message("The taiwan_number_one plugin action is finished!")
      UI.message("🇹🇼 Taiwan can help!")
      return ActionResult::DO_NOTHING
    end
  rescue => error
    UI.message("🇹🇼 Taiwan might not be able to help you with this...")
    UI.user_error!("The taiwan_number_one plugin action is finished with error: #{error.message}!")
    return ActionResult::DO_NOTHING
  end
end