@@ -48,8 +48,7 @@ function checkAuthError(error: any) {
48
48
}
49
49
}
50
50
51
- export async function GetAppId ( project : XcodeProject ) : Promise < XcodeProject > {
52
- if ( project . appId ) { return project ; }
51
+ export async function GetAppId ( project : XcodeProject ) : Promise < string > {
53
52
await getOrCreateClient ( project ) ;
54
53
const { data : response , error } = await appStoreConnectClient . api . AppsService . appsGetCollection ( {
55
54
query : { 'filter[bundleId]' : [ project . bundleId ] }
@@ -58,27 +57,32 @@ export async function GetAppId(project: XcodeProject): Promise<XcodeProject> {
58
57
checkAuthError ( error ) ;
59
58
throw new Error ( `Error fetching apps: ${ JSON . stringify ( error ) } ` ) ;
60
59
}
60
+ log ( `GET /appsGetCollection\n${ JSON . stringify ( response , null , 2 ) } ` ) ;
61
61
if ( ! response ) {
62
62
throw new Error ( `No apps found for bundle id ${ project . bundleId } ` ) ;
63
63
}
64
64
if ( response . data . length === 0 ) {
65
65
throw new Error ( `No apps found for bundle id ${ project . bundleId } ` ) ;
66
66
}
67
- project . appId = response . data [ 0 ] . id ;
68
- return project ;
67
+ if ( response . data . length > 1 ) {
68
+ log ( `Multiple apps found for bundle id ${ project . bundleId } !` ) ;
69
+ for ( const app of response . data ) {
70
+ log ( `[${ app . id } ] ${ app . attributes ?. bundleId } ` ) ;
71
+ if ( project . bundleId === app . attributes ?. bundleId ) {
72
+ return app . id ;
73
+ }
74
+ }
75
+ }
76
+ return response . data [ 0 ] . id ;
69
77
}
70
78
71
- export async function GetLatestBundleVersion ( project : XcodeProject ) : Promise < number > {
79
+ export async function GetLatestBundleVersion ( project : XcodeProject ) : Promise < string | null > {
72
80
await getOrCreateClient ( project ) ;
73
81
let { preReleaseVersion, build } = await getLastPreReleaseVersionAndBuild ( project ) ;
74
82
if ( ! build ) {
75
83
build = await getLastPrereleaseBuild ( preReleaseVersion ) ;
76
84
}
77
- const buildVersion = build . attributes . version ;
78
- if ( ! buildVersion ) {
79
- throw new Error ( `No build version found!\n${ JSON . stringify ( build , null , 2 ) } ` ) ;
80
- }
81
- return Number ( buildVersion ) ;
85
+ return build ?. attributes ?. version ;
82
86
}
83
87
84
88
function reMapPlatform ( project : XcodeProject ) : ( 'IOS' | 'MAC_OS' | 'TV_OS' | 'VISION_OS' ) {
@@ -97,7 +101,7 @@ function reMapPlatform(project: XcodeProject): ('IOS' | 'MAC_OS' | 'TV_OS' | 'VI
97
101
}
98
102
99
103
async function getLastPreReleaseVersionAndBuild ( project : XcodeProject ) : Promise < PreReleaseVersionWithBuild > {
100
- if ( ! project . appId ) { project = await GetAppId ( project ) ; }
104
+ if ( ! project . appId ) { project . appId = await GetAppId ( project ) ; }
101
105
const preReleaseVersionRequest : PreReleaseVersionsGetCollectionData = {
102
106
query : {
103
107
'filter[app]' : [ project . appId ] ,
@@ -109,7 +113,7 @@ async function getLastPreReleaseVersionAndBuild(project: XcodeProject): Promise<
109
113
limit : 1 ,
110
114
}
111
115
} ;
112
- log ( `/preReleaseVersions?${ JSON . stringify ( preReleaseVersionRequest . query ) } ` ) ;
116
+ log ( `GET /preReleaseVersions?${ JSON . stringify ( preReleaseVersionRequest . query ) } ` ) ;
113
117
const { data : preReleaseResponse , error : preReleaseError } = await appStoreConnectClient . api . PreReleaseVersionsService . preReleaseVersionsGetCollection ( preReleaseVersionRequest ) ;
114
118
const responseJson = JSON . stringify ( preReleaseResponse , null , 2 ) ;
115
119
if ( preReleaseError ) {
@@ -151,7 +155,7 @@ async function getLastPrereleaseBuild(prereleaseVersion: PrereleaseVersion): Pro
151
155
limit : 1
152
156
}
153
157
} ;
154
- log ( `/builds?${ JSON . stringify ( buildsRequest . query ) } ` ) ;
158
+ log ( `GET /builds?${ JSON . stringify ( buildsRequest . query ) } ` ) ;
155
159
const { data : buildsResponse , error : buildsError } = await appStoreConnectClient . api . BuildsService . buildsGetCollection ( buildsRequest ) ;
156
160
const responseJson = JSON . stringify ( buildsResponse , null , 2 ) ;
157
161
if ( buildsError ) {
@@ -173,7 +177,7 @@ async function getBetaBuildLocalization(build: Build): Promise<BetaBuildLocaliza
173
177
'fields[betaBuildLocalizations]' : [ 'whatsNew' ]
174
178
}
175
179
} ;
176
- log ( `/betaBuildLocalizations?${ JSON . stringify ( betaBuildLocalizationRequest . query ) } ` ) ;
180
+ log ( `GET /betaBuildLocalizations?${ JSON . stringify ( betaBuildLocalizationRequest . query ) } ` ) ;
177
181
const { data : betaBuildLocalizationResponse , error : betaBuildLocalizationError } = await appStoreConnectClient . api . BetaBuildLocalizationsService . betaBuildLocalizationsGetCollection ( betaBuildLocalizationRequest ) ;
178
182
const responseJson = JSON . stringify ( betaBuildLocalizationResponse , null , 2 ) ;
179
183
if ( betaBuildLocalizationError ) {
@@ -205,7 +209,7 @@ async function createBetaBuildLocalization(build: Build, whatsNew: string): Prom
205
209
}
206
210
}
207
211
}
208
- log ( `/betaBuildLocalizations\n${ JSON . stringify ( betaBuildLocalizationRequest , null , 2 ) } ` ) ;
212
+ log ( `POST /betaBuildLocalizations\n${ JSON . stringify ( betaBuildLocalizationRequest , null , 2 ) } ` ) ;
209
213
const { data : response , error : responseError } = await appStoreConnectClient . api . BetaBuildLocalizationsService . betaBuildLocalizationsCreateInstance ( {
210
214
body : betaBuildLocalizationRequest
211
215
} ) ;
@@ -228,63 +232,73 @@ async function updateBetaBuildLocalization(betaBuildLocalization: BetaBuildLocal
228
232
}
229
233
}
230
234
} ;
231
- log ( `/betaBuildLocalizations/${ betaBuildLocalization . id } \n${ JSON . stringify ( updateBuildLocalization , null , 2 ) } ` ) ;
235
+ log ( `POST /betaBuildLocalizations/${ betaBuildLocalization . id } \n${ JSON . stringify ( updateBuildLocalization , null , 2 ) } ` ) ;
232
236
const { error : updateError } = await appStoreConnectClient . api . BetaBuildLocalizationsService . betaBuildLocalizationsUpdateInstance ( {
233
237
path : { id : betaBuildLocalization . id } ,
234
238
body : updateBuildLocalization
235
239
} ) ;
236
- const responseJson = JSON . stringify ( updateBuildLocalization , null , 2 ) ;
237
240
if ( updateError ) {
238
241
checkAuthError ( updateError ) ;
239
242
throw new Error ( `Error updating beta build localization: ${ JSON . stringify ( updateError , null , 2 ) } ` ) ;
240
243
}
241
- log ( responseJson ) ;
242
244
return betaBuildLocalization ;
243
245
}
244
246
245
- async function pollForValidBuild ( project : XcodeProject , buildVersion : number , whatsNew : string , maxRetries : number = 60 , interval : number = 30 ) : Promise < BetaBuildLocalization > {
247
+ async function pollForValidBuild ( project : XcodeProject , maxRetries : number = 60 , interval : number = 30 ) : Promise < Build > {
248
+ log ( `Polling build validation...` ) ;
249
+ await new Promise ( resolve => setTimeout ( resolve , interval * 1000 ) ) ;
246
250
let retries = 0 ;
247
- while ( retries < maxRetries ) {
248
- if ( core . isDebug ( ) ) {
249
- core . startGroup ( `Polling for build... Attempt ${ ++ retries } /${ maxRetries } ` ) ;
250
- }
251
- try {
252
- let { preReleaseVersion, build } = await getLastPreReleaseVersionAndBuild ( project ) ;
253
- if ( ! preReleaseVersion ) {
254
- throw new Error ( 'No pre-release version found!' ) ;
255
- }
251
+ while ( ++ retries < maxRetries ) {
252
+ core . info ( `Polling for build... Attempt ${ retries } /${ maxRetries } ` ) ;
253
+ let { preReleaseVersion, build } = await getLastPreReleaseVersionAndBuild ( project ) ;
254
+ if ( preReleaseVersion ) {
256
255
if ( ! build ) {
257
256
build = await getLastPrereleaseBuild ( preReleaseVersion ) ;
258
257
}
259
- if ( build . attributes ?. version !== buildVersion . toString ( ) ) {
260
- throw new Error ( `Build version ${ build . attributes ?. version } does not match expected version ${ buildVersion } ` ) ;
261
- }
262
- if ( build . attributes ?. processingState !== 'VALID' ) {
263
- throw new Error ( `Build ${ buildVersion } is not valid yet!` ) ;
264
- }
265
- const betaBuildLocalization = await getBetaBuildLocalization ( build ) ;
266
- try {
267
- if ( ! betaBuildLocalization ) {
268
- return await createBetaBuildLocalization ( build , whatsNew ) ;
258
+ if ( build ) {
259
+ const normalizedBuildVersion = normalizeVersion ( build . attributes ?. version ) ;
260
+ const normalizedProjectVersion = normalizeVersion ( project . bundleVersion ) ;
261
+ switch ( build . attributes ?. processingState ) {
262
+ case 'VALID' :
263
+ if ( normalizedBuildVersion === normalizedProjectVersion ) {
264
+ core . info ( `Build ${ build . attributes . version } is VALID` ) ;
265
+ return build ;
266
+ } else {
267
+ core . info ( `Waiting for ${ project . bundleVersion } ...` ) ;
268
+ }
269
+ break ;
270
+ case 'FAILED' :
271
+ case 'INVALID' :
272
+ throw new Error ( `Build ${ build . attributes . version } === ${ build . attributes . processingState } !` ) ;
273
+ default :
274
+ core . info ( `Build ${ build . attributes . version } is ${ build . attributes . processingState } ...` ) ;
275
+ break ;
269
276
}
270
- } catch ( error ) {
271
- log ( error , core . isDebug ( ) ? 'warning' : 'info' ) ;
272
- }
273
- return await updateBetaBuildLocalization ( betaBuildLocalization , whatsNew ) ;
274
- } catch ( error ) {
275
- log ( error , core . isDebug ( ) ? 'error' : 'info' ) ;
276
- }
277
- finally {
278
- if ( core . isDebug ( ) ) {
279
- core . endGroup ( ) ;
277
+ } else {
278
+ core . info ( `Waiting for build ${ preReleaseVersion . attributes ?. version } ...` ) ;
280
279
}
280
+ } else {
281
+ core . info ( `Waiting for pre-release build ${ project . versionString } ...` ) ;
281
282
}
282
283
await new Promise ( resolve => setTimeout ( resolve , interval * 1000 ) ) ;
283
284
}
284
285
throw new Error ( 'Timed out waiting for valid build!' ) ;
285
286
}
286
287
287
- export async function UpdateTestDetails ( project : XcodeProject , buildVersion : number , whatsNew : string ) : Promise < void > {
288
+ export async function UpdateTestDetails ( project : XcodeProject , whatsNew : string ) : Promise < void > {
289
+ core . info ( `Updating test details...` ) ;
288
290
await getOrCreateClient ( project ) ;
289
- await pollForValidBuild ( project , buildVersion , whatsNew ) ;
291
+ const build = await pollForValidBuild ( project ) ;
292
+ const betaBuildLocalization = await getBetaBuildLocalization ( build ) ;
293
+ if ( ! betaBuildLocalization ) {
294
+ core . info ( `Creating beta build localization...` ) ;
295
+ await createBetaBuildLocalization ( build , whatsNew ) ;
296
+ } else {
297
+ core . info ( `Updating beta build localization...` ) ;
298
+ await updateBetaBuildLocalization ( betaBuildLocalization , whatsNew ) ;
299
+ }
290
300
}
301
+
302
+ function normalizeVersion ( version : string ) : string {
303
+ return version . split ( '.' ) . map ( part => parseInt ( part , 10 ) . toString ( ) ) . join ( '.' ) ;
304
+ }
0 commit comments