1
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
1
/* eslint-disable @typescript-eslint/no-use-before-define */
3
2
import Table from "cli-table" ;
4
3
import commander from "commander" ;
@@ -24,7 +23,6 @@ import { Config } from "../../config";
24
23
import { build as buildCmd , exit as exitCmd } from "../../lib/commandBuilder" ;
25
24
import { isIntegerString } from "../../lib/validator" ;
26
25
import { logger } from "../../logger" ;
27
- import { ConfigYaml } from "../../types" ;
28
26
import decorator from "./get.decorator.json" ;
29
27
import { IPullRequest } from "spektate/lib/repository/IPullRequest" ;
30
28
@@ -45,11 +43,15 @@ export enum OUTPUT_FORMAT {
45
43
* process
46
44
*/
47
45
export interface InitObject {
48
- config : ConfigYaml ;
46
+ accountName : string ;
47
+ tableName : string ;
48
+ partitionKey : string ;
49
49
clusterPipeline : AzureDevOpsPipeline ;
50
50
hldPipeline : AzureDevOpsPipeline ;
51
51
key : string ;
52
52
srcPipeline : AzureDevOpsPipeline ;
53
+ manifestRepo ?: string ;
54
+ accessToken ?: string ;
53
55
}
54
56
55
57
/**
@@ -170,13 +172,12 @@ export const getDeployments = (
170
172
initObj : InitObject ,
171
173
values : ValidatedOptions
172
174
) : Promise < IDeployment [ ] > => {
173
- const config = initObj . config ;
174
175
const syncStatusesPromise = getClusterSyncStatuses ( initObj ) ;
175
176
const deploymentsPromise = getDeploymentsBasedOnFilters (
176
- config . introspection ! . azure ! . account_name ! ,
177
+ initObj . accountName ,
177
178
initObj . key ,
178
- config . introspection ! . azure ! . table_name ! ,
179
- config . introspection ! . azure ! . partition_key ! ,
179
+ initObj . tableName ,
180
+ initObj . partitionKey ,
180
181
initObj . srcPipeline ,
181
182
initObj . hldPipeline ,
182
183
initObj . clusterPipeline ,
@@ -195,7 +196,8 @@ export const getDeployments = (
195
196
const displayedDeployments = await displayDeployments (
196
197
values ,
197
198
deployments ,
198
- syncStatuses
199
+ syncStatuses ,
200
+ initObj
199
201
) ;
200
202
resolve ( displayedDeployments ) ;
201
203
} )
@@ -209,16 +211,18 @@ export const getDeployments = (
209
211
* Displays the deployments based on output format requested and top n
210
212
* @param values validated command line values
211
213
* @param deployments list of deployments to display
212
- * @param syncStatuses cluster sync statuses
214
+ * @param syncStatuses cluster sync statuses,
215
+ * @param initObj initialization object
213
216
*/
214
217
export const displayDeployments = (
215
218
values : ValidatedOptions ,
216
219
deployments : IDeployment [ ] | undefined ,
217
- syncStatuses : ITag [ ] | undefined
220
+ syncStatuses : ITag [ ] | undefined ,
221
+ initObj : InitObject
218
222
) : Promise < IDeployment [ ] > => {
219
223
return new Promise ( ( resolve , reject ) => {
220
224
if ( values . outputFormat === OUTPUT_FORMAT . WIDE ) {
221
- getPRs ( deployments ) ;
225
+ getPRs ( deployments , initObj ) ;
222
226
}
223
227
if ( values . outputFormat === OUTPUT_FORMAT . JSON ) {
224
228
console . log ( JSON . stringify ( deployments , null , 2 ) ) ;
@@ -248,47 +252,33 @@ export const displayDeployments = (
248
252
export const getClusterSyncStatuses = (
249
253
initObj : InitObject
250
254
) : Promise < ITag [ ] | undefined > => {
251
- const config = initObj . config ;
252
255
return new Promise ( ( resolve , reject ) => {
253
256
try {
254
- if (
255
- config . azure_devops ?. manifest_repository &&
256
- config . azure_devops ?. manifest_repository . includes ( "azure.com" )
257
- ) {
258
- const manifestUrlSplit = config . azure_devops ?. manifest_repository . split (
259
- "/"
260
- ) ;
257
+ if ( initObj . manifestRepo && initObj . manifestRepo . includes ( "azure.com" ) ) {
258
+ const manifestUrlSplit = initObj . manifestRepo . split ( "/" ) ;
261
259
const manifestRepo : IAzureDevOpsRepo = {
262
260
org : manifestUrlSplit [ 3 ] ,
263
261
project : manifestUrlSplit [ 4 ] ,
264
262
repo : manifestUrlSplit [ 6 ] ,
265
263
} ;
266
- getAzureManifestSyncState (
267
- manifestRepo ,
268
- config . azure_devops . access_token
269
- )
264
+ getAzureManifestSyncState ( manifestRepo , initObj . accessToken )
270
265
. then ( ( syncCommits : ITag [ ] ) => {
271
266
resolve ( syncCommits ) ;
272
267
} )
273
268
. catch ( ( e ) => {
274
269
reject ( e ) ;
275
270
} ) ;
276
271
} else if (
277
- config . azure_devops ?. manifest_repository &&
278
- config . azure_devops ?. manifest_repository . includes ( "github.com" )
272
+ initObj . manifestRepo &&
273
+ initObj . manifestRepo . includes ( "github.com" )
279
274
) {
280
- const manifestUrlSplit = config . azure_devops ?. manifest_repository . split (
281
- "/"
282
- ) ;
275
+ const manifestUrlSplit = initObj . manifestRepo . split ( "/" ) ;
283
276
const manifestRepo : IGitHub = {
284
277
reponame : manifestUrlSplit [ 4 ] ,
285
278
username : manifestUrlSplit [ 3 ] ,
286
279
} ;
287
280
288
- getGithubManifestSyncState (
289
- manifestRepo ,
290
- config . azure_devops . access_token
291
- )
281
+ getGithubManifestSyncState ( manifestRepo , initObj . accessToken )
292
282
. then ( ( syncCommits : ITag [ ] ) => {
293
283
resolve ( syncCommits ) ;
294
284
} )
@@ -310,10 +300,8 @@ export const getClusterSyncStatuses = (
310
300
*/
311
301
export const initialize = async ( ) : Promise < InitObject > => {
312
302
const config = Config ( ) ;
313
- const key = await config . introspection ! . azure ! . key ;
314
303
315
304
if (
316
- ! key ||
317
305
! config . introspection ||
318
306
! config . azure_devops ||
319
307
! config . introspection . azure ||
@@ -322,9 +310,10 @@ export const initialize = async (): Promise<InitObject> => {
322
310
! config . introspection . azure . account_name ||
323
311
! config . introspection . azure . table_name ||
324
312
! config . introspection . azure . key ||
325
- ! config . introspection . azure . partition_key
313
+ ! config . introspection . azure . partition_key ||
314
+ ! config . introspection . azure . key
326
315
) {
327
- throw new Error (
316
+ throw Error (
328
317
"You need to run `spk init` and `spk deployment onboard` to configure `spk."
329
318
) ;
330
319
}
@@ -336,20 +325,24 @@ export const initialize = async (): Promise<InitObject> => {
336
325
false ,
337
326
config . azure_devops . access_token
338
327
) ,
339
- config,
340
328
hldPipeline : new AzureDevOpsPipeline (
341
329
config . azure_devops . org ,
342
330
config . azure_devops . project ,
343
331
true ,
344
332
config . azure_devops . access_token
345
333
) ,
346
- key,
334
+ key : config . introspection . azure . key ,
347
335
srcPipeline : new AzureDevOpsPipeline (
348
336
config . azure_devops . org ,
349
337
config . azure_devops . project ,
350
338
false ,
351
339
config . azure_devops . access_token
352
340
) ,
341
+ accountName : config . introspection . azure . account_name ,
342
+ tableName : config . introspection . azure . table_name ,
343
+ partitionKey : config . introspection . azure . partition_key ,
344
+ manifestRepo : config . azure_devops . manifest_repository ,
345
+ accessToken : config . azure_devops . access_token ,
353
346
} ;
354
347
} ;
355
348
@@ -496,8 +489,8 @@ export const printDeployments = (
496
489
deployment . pr . toString ( ) in pullRequests
497
490
) {
498
491
row . push ( deployment . pr ) ;
499
- if ( pullRequests [ deployment . pr ! . toString ( ) ] . mergedBy ) {
500
- row . push ( pullRequests [ deployment . pr ! . toString ( ) ] . mergedBy ?. name ) ;
492
+ if ( pullRequests [ deployment . pr . toString ( ) ] . mergedBy ) {
493
+ row . push ( pullRequests [ deployment . pr . toString ( ) ] . mergedBy ?. name ) ;
501
494
} else {
502
495
deploymentStatus = "Waiting" ;
503
496
row . push ( "-" ) ;
@@ -551,41 +544,43 @@ export const printDeployments = (
551
544
} ;
552
545
553
546
/**
554
- * Gets PR information for all the deployments
547
+ * Gets PR information for all the deployments.
548
+ *
555
549
* @param deployments all deployments to be displayed
550
+ * @param initObj initialization object
556
551
*/
557
- export const getPRs = ( deployments : IDeployment [ ] | undefined ) => {
558
- if ( deployments && deployments . length > 0 ) {
559
- deployments . forEach ( ( deployment : IDeployment ) => {
560
- fetchPRInformation ( deployment ) ;
561
- } ) ;
562
- }
552
+ export const getPRs = (
553
+ deployments : IDeployment [ ] | undefined ,
554
+ initObj : InitObject
555
+ ) : void => {
556
+ ( deployments || [ ] ) . forEach ( ( d ) => fetchPRInformation ( d , initObj ) ) ;
563
557
} ;
564
558
565
559
/**
566
560
* Fetches pull request data for deployments that complete merge into HLD
567
561
* by merging a PR
562
+ *
568
563
* @param deployment deployment for which PR has to be fetched
564
+ * @param initObj initialization object
569
565
*/
570
- export const fetchPRInformation = ( deployment : IDeployment ) => {
571
- const config = Config ( ) ;
572
- if ( ! deployment . hldRepo || ! deployment . pr ) {
573
- return ;
574
- }
575
- const repo : IAzureDevOpsRepo | IGitHub | undefined = getRepositoryFromURL (
576
- deployment . hldRepo !
577
- ) ;
578
- const promise = fetchPR (
579
- repo ! ,
580
- deployment . pr ! . toString ( ) ,
581
- config . introspection ?. azure ?. source_repo_access_token
582
- ) ;
583
- promise . then ( ( pr : IPullRequest | undefined ) => {
584
- if ( pr ) {
585
- pullRequests [ deployment . pr ! . toString ( ) ] = pr ;
566
+ export const fetchPRInformation = (
567
+ deployment : IDeployment ,
568
+ initObj : InitObject
569
+ ) : void => {
570
+ if ( deployment . hldRepo && deployment . pr ) {
571
+ const repo = getRepositoryFromURL ( deployment . hldRepo ) ;
572
+ const strPr = deployment . pr . toString ( ) ;
573
+
574
+ if ( repo ) {
575
+ const promise = fetchPR ( repo , strPr , initObj . accountName ) ;
576
+ promise . then ( ( pr ) => {
577
+ if ( pr ) {
578
+ pullRequests [ strPr ] = pr ;
579
+ }
580
+ } ) ;
581
+ promises . push ( promise ) ;
586
582
}
587
- } ) ;
588
- promises . push ( promise ) ;
583
+ }
589
584
} ;
590
585
591
586
/**
0 commit comments