1010require "timeout"
1111require "uri"
1212
13+ $stdout. sync = true
14+
1315class AppStoreConnectError < StandardError ; end
1416
1517class AppStoreConnectClient
@@ -151,9 +153,10 @@ def run
151153 validate_release_notes!
152154 return puts ( "Release inputs are valid" ) if @options [ :validate_only ]
153155
154- build_run = start_xcode_cloud_run
155- wait_for_xcode_cloud_run ( build_run . fetch ( "id" ) )
156- build = wait_for_processed_build ( build_run . fetch ( "id" ) )
156+ build = wait_for_processed_build
157+ return puts ( "Uploaded build is ready; App Review submission was not requested" ) if @options [ :process_only ]
158+ return distribute_testflight ( build ) unless Array ( @options [ :testflight_groups ] ) . empty?
159+
157160 version = find_or_create_version
158161
159162 if submitted? ( version )
@@ -187,71 +190,12 @@ def validate_note!(path)
187190 raise AppStoreConnectError , "Release notes exceed Apple's 4000-character limit: #{ path } "
188191 end
189192
190- def start_xcode_cloud_run
191- source_reference = wait_for_source_reference
192- puts "Starting Xcode Cloud workflow #{ @options [ :workflow_id ] } at #{ @options [ :tag ] } "
193- @client . post (
194- "/v1/ciBuildRuns" ,
195- data : {
196- type : "ciBuildRuns" ,
197- attributes : { } ,
198- relationships : {
199- workflow : { data : { type : "ciWorkflows" , id : @options [ :workflow_id ] } } ,
200- sourceBranchOrTag : { data : { type : "scmGitReferences" , id : source_reference . fetch ( "id" ) } }
201- }
202- }
203- ) . fetch ( "data" )
204- end
205-
206- def wait_for_source_reference
207- canonical_name = "refs/tags/#{ @options [ :tag ] } "
208- loop do
209- repository = @client . get ( "/v1/ciWorkflows/#{ @options [ :workflow_id ] } /repository" ) . fetch ( "data" )
210- response = @client . get (
211- "/v1/scmRepositories/#{ repository . fetch ( 'id' ) } /gitReferences" ,
212- "fields[scmGitReferences]" => "name,canonicalName,isDeleted,kind" ,
213- "limit" => 200
214- )
215- reference = Array ( response [ "data" ] ) . find do |candidate |
216- candidate . dig ( "attributes" , "canonicalName" ) == canonical_name &&
217- !candidate . dig ( "attributes" , "isDeleted" )
218- end
219- return reference if reference
220-
221- puts "Xcode Cloud has not indexed #{ @options [ :tag ] } yet"
222- wait_or_timeout!
223- end
224- end
225-
226- def wait_for_xcode_cloud_run ( build_run_id )
227- puts "Waiting for Xcode Cloud build #{ build_run_id } "
228- loop do
229- run = @client . get ( "/v1/ciBuildRuns/#{ build_run_id } " ) . fetch ( "data" )
230- commit_sha = run . dig ( "attributes" , "sourceCommit" , "commitSha" ) . to_s
231- if !commit_sha . empty? && commit_sha != @options [ :git_sha ]
232- raise AppStoreConnectError ,
233- "Xcode Cloud build source commit #{ commit_sha } does not match release commit #{ @options [ :git_sha ] } "
234- end
235-
236- progress = run . dig ( "attributes" , "executionProgress" )
237- if progress == "COMPLETE"
238- status = run . dig ( "attributes" , "completionStatus" )
239- raise AppStoreConnectError , "Xcode Cloud build finished with #{ status } " unless status == "SUCCEEDED"
240-
241- puts "Xcode Cloud build ##{ run . dig ( 'attributes' , 'number' ) } succeeded"
242- return run
243- end
244-
245- puts "Xcode Cloud build ##{ run . dig ( 'attributes' , 'number' ) } is #{ progress } "
246- wait_or_timeout!
247- end
248- end
249-
250- def wait_for_processed_build ( build_run_id )
251- puts "Waiting for App Store Connect to process the Xcode Cloud archive"
193+ def wait_for_processed_build
194+ puts "Waiting for App Store Connect to process #{ @options [ :platform ] } #{ @options [ :version ] } (#{ @options [ :build_number ] } )"
252195 loop do
253196 response = @client . get (
254- "/v1/ciBuildRuns/#{ build_run_id } /builds" ,
197+ "/v1/builds" ,
198+ "filter[version]" => @options [ :build_number ] ,
255199 "filter[app]" => @options [ :app_id ] ,
256200 "filter[preReleaseVersion.version]" => @options [ :version ] ,
257201 "filter[preReleaseVersion.platform]" => @options [ :platform ] ,
@@ -271,19 +215,66 @@ def wait_for_processed_build(build_run_id)
271215 audience = build . dig ( "attributes" , "buildAudienceType" )
272216 unless audience == "APP_STORE_ELIGIBLE"
273217 raise AppStoreConnectError ,
274- "Build #{ build . dig ( 'attributes' , 'version' ) } is #{ audience } ; configure the Xcode Cloud " \
218+ "Build #{ build . dig ( 'attributes' , 'version' ) } is #{ audience } ; export the " \
275219 "archive for TestFlight and App Store distribution"
276220 end
277221
278222 puts "Build #{ build . dig ( 'attributes' , 'version' ) } is valid and App Store eligible"
279223 return build
280224 end
281225
282- puts builds . empty? ? "No uploaded build is associated with the run yet" : "The build is still processing"
226+ puts builds . empty? ? "The uploaded build is not visible yet" : "The build is still processing"
283227 wait_or_timeout!
284228 end
285229 end
286230
231+ # Keep the same explicit tester groups as the retired main-branch workflows.
232+ # Beta App Review is separate from public App Store review.
233+ def distribute_testflight ( build )
234+ groups = @client . get ( "/v1/apps/#{ @options [ :app_id ] } /betaGroups" , "limit" => 200 ) . fetch ( "data" )
235+ selected = @options [ :testflight_groups ] . map do |name |
236+ matches = groups . select { |group | group . dig ( "attributes" , "name" ) == name }
237+ raise AppStoreConnectError , "Expected exactly one TestFlight group named #{ name } " unless matches . length == 1
238+
239+ matches . first
240+ end
241+ build_id = build . fetch ( "id" )
242+ notes = File . read ( File . join ( @options [ :release_notes_dir ] , "default.txt" ) , encoding : "UTF-8" ) . strip
243+ localizations = @client . get ( "/v1/builds/#{ build_id } /betaBuildLocalizations" ) . fetch ( "data" )
244+ if localizations . empty?
245+ @client . post ( "/v1/betaBuildLocalizations" , data : {
246+ type : "betaBuildLocalizations" , attributes : { locale : "en-US" , whatsNew : notes } ,
247+ relationships : { build : { data : { type : "builds" , id : build_id } } }
248+ } )
249+ else
250+ localizations . each do |localization |
251+ @client . patch ( "/v1/betaBuildLocalizations/#{ localization . fetch ( 'id' ) } " , data : {
252+ type : "betaBuildLocalizations" , id : localization . fetch ( "id" ) , attributes : { whatsNew : notes }
253+ } )
254+ end
255+ end
256+
257+ if selected . any? { |group | !group . dig ( "attributes" , "isInternalGroup" ) }
258+ detail = @client . get ( "/v1/builds/#{ build_id } /buildBetaDetail" ) . fetch ( "data" )
259+ @client . patch ( "/v1/buildBetaDetails/#{ detail . fetch ( 'id' ) } " , data : {
260+ type : "buildBetaDetails" , id : detail . fetch ( "id" ) , attributes : { autoNotifyEnabled : true }
261+ } )
262+ state = detail . dig ( "attributes" , "externalBuildState" )
263+ if state == "READY_FOR_BETA_SUBMISSION"
264+ @client . post ( "/v1/betaAppReviewSubmissions" , data : {
265+ type : "betaAppReviewSubmissions" ,
266+ relationships : { build : { data : { type : "builds" , id : build_id } } }
267+ } )
268+ elsif !%w[ WAITING_FOR_BETA_REVIEW IN_BETA_REVIEW BETA_APPROVED READY_FOR_BETA_TESTING IN_BETA_TESTING ] . include? ( state )
269+ raise AppStoreConnectError , "TestFlight build cannot be distributed: #{ state } "
270+ end
271+ end
272+ @client . post ( "/v1/builds/#{ build_id } /relationships/betaGroups" , data : selected . map do |group |
273+ { type : "betaGroups" , id : group . fetch ( "id" ) }
274+ end )
275+ puts "Assigned #{ @options [ :platform ] } build #{ @options [ :build_number ] } to #{ @options [ :testflight_groups ] . join ( ', ' ) } "
276+ end
277+
287278 def find_or_create_version
288279 response = @client . get (
289280 "/v1/apps/#{ @options [ :app_id ] } /appStoreVersions" ,
@@ -491,29 +482,30 @@ def wait_or_timeout!
491482 end
492483end
493484
494- begin
485+ def run_release_cli
495486 options = {
496- timeout : 7200 ,
487+ timeout : 2700 ,
497488 release_type : "AFTER_APPROVAL" ,
498- validate_only : false
489+ validate_only : false ,
490+ testflight_groups : [ ]
499491 }
500492
501493 OptionParser . new do |parser |
502494 parser . banner = "Usage: app_store_connect_release.rb [options]"
503- parser . on ( "--workflow-id ID " ) { |value | options [ :workflow_id ] = value }
495+ parser . on ( "--build-number NUMBER " ) { |value | options [ :build_number ] = value }
504496 parser . on ( "--app-id ID" ) { |value | options [ :app_id ] = value }
505497 parser . on ( "--platform PLATFORM" ) { |value | options [ :platform ] = value }
506498 parser . on ( "--version VERSION" ) { |value | options [ :version ] = value }
507- parser . on ( "--tag TAG" ) { |value | options [ :tag ] = value }
508- parser . on ( "--git-sha SHA" ) { |value | options [ :git_sha ] = value }
509499 parser . on ( "--release-notes-dir PATH" ) { |value | options [ :release_notes_dir ] = value }
510500 parser . on ( "--release-type TYPE" ) { |value | options [ :release_type ] = value }
511501 parser . on ( "--timeout SECONDS" , Integer ) { |value | options [ :timeout ] = value }
512502 parser . on ( "--validate-only" ) { options [ :validate_only ] = true }
503+ parser . on ( "--process-only" ) { options [ :process_only ] = true }
504+ parser . on ( "--testflight-group NAME" ) { |value | options [ :testflight_groups ] << value }
513505 end . parse!
514506
515507 required = %i[ platform version release_notes_dir ]
516- required += %i[ workflow_id app_id tag git_sha ] unless options [ :validate_only ]
508+ required += %i[ app_id build_number ] unless options [ :validate_only ]
517509 missing = required . select { |key | options [ key ] . nil? || options [ key ] . empty? }
518510 raise AppStoreConnectError , "Missing options: #{ missing . join ( ', ' ) } " unless missing . empty?
519511 unless %w[ IOS MAC_OS ] . include? ( options [ :platform ] )
@@ -538,3 +530,5 @@ def wait_or_timeout!
538530 warn "::error::#{ error . message } "
539531 exit 1
540532end
533+
534+ run_release_cli if $PROGRAM_NAME == __FILE__
0 commit comments