@@ -20,12 +20,14 @@ import {
20
20
import { ITag } from "spektate/lib/repository/Tag" ;
21
21
import { Config } from "../../config" ;
22
22
import { build as buildCmd , exit as exitCmd } from "../../lib/commandBuilder" ;
23
+ import { build as buildError , log as logError } from "../../lib/errorBuilder" ;
24
+ import { errorStatusCode } from "../../lib/errorStatusCode" ;
23
25
import { isIntegerString } from "../../lib/validator" ;
24
26
import { logger } from "../../logger" ;
25
27
import decorator from "./get.decorator.json" ;
26
28
import { IPullRequest } from "spektate/lib/repository/IPullRequest" ;
27
29
28
- const promises : Promise < IPullRequest | undefined > [ ] = [ ] ;
30
+ const promises : Promise < void > [ ] = [ ] ;
29
31
const pullRequests : { [ id : string ] : IPullRequest } = { } ;
30
32
/**
31
33
* Output formats to display service details
@@ -104,7 +106,10 @@ export const validateValues = (opts: CommandOptions): ValidatedOptions => {
104
106
if ( isIntegerString ( opts . top ) ) {
105
107
top = parseInt ( opts . top , 10 ) ;
106
108
} else {
107
- throw new Error ( "value for top option has to be a positive number" ) ;
109
+ throw buildError ( errorStatusCode . VALIDATION_ERR , {
110
+ errorKey : "introspect-get-cmd-err-validation-top-num" ,
111
+ values : [ opts . top ] ,
112
+ } ) ;
108
113
}
109
114
}
110
115
@@ -141,8 +146,9 @@ export const initialize = async (): Promise<InitObject> => {
141
146
! config . introspection . azure . partition_key ||
142
147
! config . introspection . azure . key
143
148
) {
144
- throw Error (
145
- "You need to run `spk init` and `spk deployment onboard` to configure `spk."
149
+ throw buildError (
150
+ errorStatusCode . VALIDATION_ERR ,
151
+ "introspect-get-cmd-missing-vals"
146
152
) ;
147
153
}
148
154
@@ -178,50 +184,42 @@ export const initialize = async (): Promise<InitObject> => {
178
184
* Gets cluster sync statuses
179
185
* @param initObj captures keys and objects during the initialization process
180
186
*/
181
- export const getClusterSyncStatuses = (
187
+ export const getClusterSyncStatuses = async (
182
188
initObj : InitObject
183
189
) : Promise < ITag [ ] | undefined > => {
184
- return new Promise ( ( resolve , reject ) => {
185
- try {
186
- if ( initObj . manifestRepo && initObj . manifestRepo . includes ( "azure.com" ) ) {
187
- const manifestUrlSplit = initObj . manifestRepo . split ( "/" ) ;
188
- const manifestRepo : IAzureDevOpsRepo = {
189
- org : manifestUrlSplit [ 3 ] ,
190
- project : manifestUrlSplit [ 4 ] ,
191
- repo : manifestUrlSplit [ 6 ] ,
192
- } ;
193
- getAzureManifestSyncState ( manifestRepo , initObj . accessToken )
194
- . then ( ( syncCommits : ITag [ ] ) => {
195
- resolve ( syncCommits ) ;
196
- } )
197
- . catch ( ( e ) => {
198
- reject ( e ) ;
199
- } ) ;
200
- } else if (
201
- initObj . manifestRepo &&
202
- initObj . manifestRepo . includes ( "github.com" )
203
- ) {
204
- const manifestUrlSplit = initObj . manifestRepo . split ( "/" ) ;
205
- const manifestRepo : IGitHub = {
206
- reponame : manifestUrlSplit [ 4 ] ,
207
- username : manifestUrlSplit [ 3 ] ,
208
- } ;
209
-
210
- getGithubManifestSyncState ( manifestRepo , initObj . accessToken )
211
- . then ( ( syncCommits : ITag [ ] ) => {
212
- resolve ( syncCommits ) ;
213
- } )
214
- . catch ( ( e ) => {
215
- reject ( e ) ;
216
- } ) ;
217
- } else {
218
- resolve ( ) ;
219
- }
220
- } catch ( err ) {
221
- logger . error ( err ) ;
222
- reject ( err ) ;
190
+ try {
191
+ if ( initObj . manifestRepo && initObj . manifestRepo . includes ( "azure.com" ) ) {
192
+ const manifestUrlSplit = initObj . manifestRepo . split ( "/" ) ;
193
+ const manifestRepo : IAzureDevOpsRepo = {
194
+ org : manifestUrlSplit [ 3 ] ,
195
+ project : manifestUrlSplit [ 4 ] ,
196
+ repo : manifestUrlSplit [ 6 ] ,
197
+ } ;
198
+ return await getAzureManifestSyncState ( manifestRepo , initObj . accessToken ) ;
199
+ } else if (
200
+ initObj . manifestRepo &&
201
+ initObj . manifestRepo . includes ( "github.com" )
202
+ ) {
203
+ const manifestUrlSplit = initObj . manifestRepo . split ( "/" ) ;
204
+ const manifestRepo : IGitHub = {
205
+ reponame : manifestUrlSplit [ 4 ] ,
206
+ username : manifestUrlSplit [ 3 ] ,
207
+ } ;
208
+
209
+ return await getGithubManifestSyncState (
210
+ manifestRepo ,
211
+ initObj . accessToken
212
+ ) ;
213
+ } else {
214
+ return undefined ;
223
215
}
224
- } ) ;
216
+ } catch ( err ) {
217
+ throw buildError (
218
+ errorStatusCode . GIT_OPS_ERR ,
219
+ "introspect-get-cmd-cluster-sync-stat-err" ,
220
+ err
221
+ ) ;
222
+ }
225
223
} ;
226
224
227
225
/**
@@ -231,22 +229,23 @@ export const getClusterSyncStatuses = (
231
229
* @param deployment deployment for which PR has to be fetched
232
230
* @param initObj initialization object
233
231
*/
234
- export const fetchPRInformation = (
232
+ export const fetchPRInformation = async (
235
233
deployment : IDeployment ,
236
234
initObj : InitObject
237
- ) : void => {
235
+ ) : Promise < void > => {
238
236
if ( deployment . hldRepo && deployment . pr ) {
239
237
const repo = getRepositoryFromURL ( deployment . hldRepo ) ;
240
238
const strPr = deployment . pr . toString ( ) ;
241
239
242
240
if ( repo ) {
243
- const promise = fetchPR ( repo , strPr , initObj . accessToken ) ;
244
- promise . then ( ( pr ) => {
241
+ try {
242
+ const pr = await fetchPR ( repo , strPr , initObj . accessToken ) ;
245
243
if ( pr ) {
246
244
pullRequests [ strPr ] = pr ;
247
245
}
248
- } ) ;
249
- promises . push ( promise ) ;
246
+ } catch ( err ) {
247
+ logger . warn ( `Could not get PR ${ strPr } information: ` + err ) ;
248
+ }
250
249
}
251
250
}
252
251
} ;
@@ -261,7 +260,9 @@ export const getPRs = (
261
260
deployments : IDeployment [ ] | undefined ,
262
261
initObj : InitObject
263
262
) : void => {
264
- ( deployments || [ ] ) . forEach ( ( d ) => fetchPRInformation ( d , initObj ) ) ;
263
+ ( deployments || [ ] ) . forEach ( ( d ) => {
264
+ promises . push ( fetchPRInformation ( d , initObj ) ) ;
265
+ } ) ;
265
266
} ;
266
267
267
268
/**
@@ -475,79 +476,68 @@ export const printDeployments = (
475
476
* @param syncStatuses cluster sync statuses,
476
477
* @param initObj initialization object
477
478
*/
478
- export const displayDeployments = (
479
+ export const displayDeployments = async (
479
480
values : ValidatedOptions ,
480
481
deployments : IDeployment [ ] | undefined ,
481
482
syncStatuses : ITag [ ] | undefined ,
482
483
initObj : InitObject
483
484
) : Promise < IDeployment [ ] > => {
484
- return new Promise ( ( resolve , reject ) => {
485
- if ( values . outputFormat === OUTPUT_FORMAT . WIDE ) {
486
- getPRs ( deployments , initObj ) ;
487
- }
488
- if ( values . outputFormat === OUTPUT_FORMAT . JSON ) {
489
- console . log ( JSON . stringify ( deployments , null , 2 ) ) ;
490
- resolve ( deployments ) ;
491
- } else {
492
- Promise . all ( promises )
493
- . then ( ( ) => {
494
- printDeployments (
495
- deployments ,
496
- values . outputFormat ,
497
- values . nTop ,
498
- syncStatuses
499
- ) ;
500
- resolve ( deployments ) ;
501
- } )
502
- . catch ( ( e ) => {
503
- reject ( e ) ;
504
- } ) ;
505
- }
506
- } ) ;
485
+ if ( values . outputFormat === OUTPUT_FORMAT . WIDE ) {
486
+ getPRs ( deployments , initObj ) ;
487
+ }
488
+
489
+ if ( values . outputFormat === OUTPUT_FORMAT . JSON ) {
490
+ console . log ( JSON . stringify ( deployments , null , 2 ) ) ;
491
+ return deployments || [ ] ;
492
+ }
493
+
494
+ await Promise . all ( promises ) ;
495
+ printDeployments ( deployments , values . outputFormat , values . nTop , syncStatuses ) ;
496
+ return deployments || [ ] ;
507
497
} ;
508
498
509
499
/**
510
500
* Gets a list of deployments for the specified filters
511
501
* @param initObj captures keys and objects during the initialization process
512
502
* @param values validated command line values
513
503
*/
514
- export const getDeployments = (
504
+ export const getDeployments = async (
515
505
initObj : InitObject ,
516
506
values : ValidatedOptions
517
507
) : Promise < IDeployment [ ] > => {
518
- const syncStatusesPromise = getClusterSyncStatuses ( initObj ) ;
519
- const deploymentsPromise = getDeploymentsBasedOnFilters (
520
- initObj . accountName ,
521
- initObj . key ,
522
- initObj . tableName ,
523
- initObj . partitionKey ,
524
- initObj . srcPipeline ,
525
- initObj . hldPipeline ,
526
- initObj . clusterPipeline ,
527
- values . env ,
528
- values . imageTag ,
529
- values . buildId ,
530
- values . commitId ,
531
- values . service ,
532
- values . deploymentId
533
- ) ;
534
- return new Promise ( ( resolve , reject ) => {
535
- Promise . all ( [ deploymentsPromise , syncStatusesPromise ] )
536
- . then ( async ( tuple : [ IDeployment [ ] | undefined , ITag [ ] | undefined ] ) => {
537
- const deployments : IDeployment [ ] | undefined = tuple [ 0 ] ;
538
- const syncStatuses : ITag [ ] | undefined = tuple [ 1 ] ;
539
- const displayedDeployments = await displayDeployments (
540
- values ,
541
- deployments ,
542
- syncStatuses ,
543
- initObj
544
- ) ;
545
- resolve ( displayedDeployments ) ;
546
- } )
547
- . catch ( ( e ) => {
548
- reject ( new Error ( e ) ) ;
549
- } ) ;
550
- } ) ;
508
+ try {
509
+ const syncStatusesPromise = getClusterSyncStatuses ( initObj ) ;
510
+ const deploymentsPromise = getDeploymentsBasedOnFilters (
511
+ initObj . accountName ,
512
+ initObj . key ,
513
+ initObj . tableName ,
514
+ initObj . partitionKey ,
515
+ initObj . srcPipeline ,
516
+ initObj . hldPipeline ,
517
+ initObj . clusterPipeline ,
518
+ values . env ,
519
+ values . imageTag ,
520
+ values . buildId ,
521
+ values . commitId ,
522
+ values . service ,
523
+ values . deploymentId
524
+ ) ;
525
+
526
+ const tuple : [
527
+ IDeployment [ ] | undefined ,
528
+ ITag [ ] | undefined
529
+ ] = await Promise . all ( [ deploymentsPromise , syncStatusesPromise ] ) ;
530
+ const deployments : IDeployment [ ] | undefined = tuple [ 0 ] ;
531
+ const syncStatuses : ITag [ ] | undefined = tuple [ 1 ] ;
532
+
533
+ return await displayDeployments ( values , deployments , syncStatuses , initObj ) ;
534
+ } catch ( err ) {
535
+ throw buildError (
536
+ errorStatusCode . EXE_FLOW_ERR ,
537
+ "introspect-get-cmd-get-deployments-err" ,
538
+ err
539
+ ) ;
540
+ }
551
541
} ;
552
542
553
543
/**
@@ -591,8 +581,9 @@ export const execute = async (
591
581
await exitFn ( 0 ) ;
592
582
}
593
583
} catch ( err ) {
594
- logger . error ( `Error occurred while getting deployment(s)` ) ;
595
- logger . error ( err ) ;
584
+ logError (
585
+ buildError ( errorStatusCode . CMD_EXE_ERR , "introspect-get-cmd-failed" , err )
586
+ ) ;
596
587
await exitFn ( 1 ) ;
597
588
}
598
589
} ;
0 commit comments