Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit d5a8cc3

Browse files
authored
[HOUSEKEEPING] remove page-wide eslint-disable for deployment get command (#466)
1 parent b607b20 commit d5a8cc3

File tree

2 files changed

+92
-91
lines changed

2 files changed

+92
-91
lines changed

src/commands/deployment/get.test.ts

+32-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
21
import path from "path";
32
import { duration, IDeployment, status } from "spektate/lib/IDeployment";
43
import * as Deployment from "spektate/lib/IDeployment";
@@ -28,7 +27,6 @@ import {
2827
watchGetDeployments,
2928
} from "./get";
3029
import * as get from "./get";
31-
import { IPullRequest } from "spektate/lib/repository/IPullRequest";
3230

3331
const MOCKED_INPUT_VALUES: CommandOptions = {
3432
buildId: "",
@@ -337,42 +335,49 @@ describe("Print deployments", () => {
337335
"EUROPE",
338336
];
339337

340-
const matchItems = table!.filter((field) => field[2] === deployment[2]);
341-
expect(matchItems).toHaveLength(1); // one matching row
338+
expect(table).toBeDefined();
342339

343-
(matchItems[0] as IDeployment[]).forEach((field, i) => {
344-
expect(field).toEqual(deployment[i]);
345-
});
346-
expect(matchItems[0]).toHaveLength(14);
340+
if (table) {
341+
const matchItems = table.filter((field) => field[2] === deployment[2]);
342+
expect(matchItems).toHaveLength(1); // one matching row
347343

348-
table = printDeployments(
349-
mockedDeps,
350-
processOutputFormat("wide"),
351-
3,
352-
mockedClusterSyncs
353-
);
354-
expect(table).toHaveLength(3);
344+
(matchItems[0] as IDeployment[]).forEach((field, i) => {
345+
expect(field).toEqual(deployment[i]);
346+
});
347+
expect(matchItems[0]).toHaveLength(14);
348+
349+
table = printDeployments(
350+
mockedDeps,
351+
processOutputFormat("wide"),
352+
3,
353+
mockedClusterSyncs
354+
);
355+
expect(table).toHaveLength(3);
356+
}
355357
});
356358
});
357359

358360
describe("Cluster sync", () => {
359361
test("Verify cluster syncs", async () => {
360362
// test a github setup too
361-
if (initObject.config.azure_devops?.manifest_repository) {
362-
initObject.config.azure_devops!.manifest_repository! = "https://github.com/someone/something";
363+
if (initObject.manifestRepo) {
364+
initObject.manifestRepo = "https://github.com/someone/something";
363365
}
364366
const clusterSyncs = await getClusterSyncStatuses(initObject);
365367
expect(clusterSyncs).toBeDefined();
366368
expect(clusterSyncs).toHaveLength(5);
367-
expect(clusterSyncs![0].name).toBe("CANADA");
368-
expect(clusterSyncs![0].commit).toBe("efeeebe");
369-
expect(clusterSyncs![0].tagger).toBe("Weave Flux");
369+
370+
if (clusterSyncs) {
371+
expect(clusterSyncs[0].name).toBe("CANADA");
372+
expect(clusterSyncs[0].commit).toBe("efeeebe");
373+
expect(clusterSyncs[0].tagger).toBe("Weave Flux");
374+
}
370375
});
371376

372377
test("Verify cluster syncs - empty", async () => {
373378
// test empty manifest scenario
374-
if (initObject.config.azure_devops?.manifest_repository) {
375-
initObject.config.azure_devops!.manifest_repository! = "";
379+
if (initObject.manifestRepo) {
380+
initObject.manifestRepo = "";
376381
}
377382
const clusterSyncs = await getClusterSyncStatuses(initObject);
378383
expect(clusterSyncs).toBeUndefined();
@@ -387,9 +392,10 @@ describe("Output formats", () => {
387392
undefined,
388393
mockedClusterSyncs
389394
);
390-
expect(table).not.toBeUndefined();
391-
table!.forEach((field) => {
392-
expect(field).toHaveLength(20);
393-
});
395+
expect(table).toBeDefined();
396+
397+
if (table) {
398+
table.forEach((field) => expect(field).toHaveLength(20));
399+
}
394400
});
395401
});

src/commands/deployment/get.ts

+60-65
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
21
/* eslint-disable @typescript-eslint/no-use-before-define */
32
import Table from "cli-table";
43
import commander from "commander";
@@ -24,7 +23,6 @@ import { Config } from "../../config";
2423
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
2524
import { isIntegerString } from "../../lib/validator";
2625
import { logger } from "../../logger";
27-
import { ConfigYaml } from "../../types";
2826
import decorator from "./get.decorator.json";
2927
import { IPullRequest } from "spektate/lib/repository/IPullRequest";
3028

@@ -45,11 +43,15 @@ export enum OUTPUT_FORMAT {
4543
* process
4644
*/
4745
export interface InitObject {
48-
config: ConfigYaml;
46+
accountName: string;
47+
tableName: string;
48+
partitionKey: string;
4949
clusterPipeline: AzureDevOpsPipeline;
5050
hldPipeline: AzureDevOpsPipeline;
5151
key: string;
5252
srcPipeline: AzureDevOpsPipeline;
53+
manifestRepo?: string;
54+
accessToken?: string;
5355
}
5456

5557
/**
@@ -170,13 +172,12 @@ export const getDeployments = (
170172
initObj: InitObject,
171173
values: ValidatedOptions
172174
): Promise<IDeployment[]> => {
173-
const config = initObj.config;
174175
const syncStatusesPromise = getClusterSyncStatuses(initObj);
175176
const deploymentsPromise = getDeploymentsBasedOnFilters(
176-
config.introspection!.azure!.account_name!,
177+
initObj.accountName,
177178
initObj.key,
178-
config.introspection!.azure!.table_name!,
179-
config.introspection!.azure!.partition_key!,
179+
initObj.tableName,
180+
initObj.partitionKey,
180181
initObj.srcPipeline,
181182
initObj.hldPipeline,
182183
initObj.clusterPipeline,
@@ -195,7 +196,8 @@ export const getDeployments = (
195196
const displayedDeployments = await displayDeployments(
196197
values,
197198
deployments,
198-
syncStatuses
199+
syncStatuses,
200+
initObj
199201
);
200202
resolve(displayedDeployments);
201203
})
@@ -209,16 +211,18 @@ export const getDeployments = (
209211
* Displays the deployments based on output format requested and top n
210212
* @param values validated command line values
211213
* @param deployments list of deployments to display
212-
* @param syncStatuses cluster sync statuses
214+
* @param syncStatuses cluster sync statuses,
215+
* @param initObj initialization object
213216
*/
214217
export const displayDeployments = (
215218
values: ValidatedOptions,
216219
deployments: IDeployment[] | undefined,
217-
syncStatuses: ITag[] | undefined
220+
syncStatuses: ITag[] | undefined,
221+
initObj: InitObject
218222
): Promise<IDeployment[]> => {
219223
return new Promise((resolve, reject) => {
220224
if (values.outputFormat === OUTPUT_FORMAT.WIDE) {
221-
getPRs(deployments);
225+
getPRs(deployments, initObj);
222226
}
223227
if (values.outputFormat === OUTPUT_FORMAT.JSON) {
224228
console.log(JSON.stringify(deployments, null, 2));
@@ -248,47 +252,33 @@ export const displayDeployments = (
248252
export const getClusterSyncStatuses = (
249253
initObj: InitObject
250254
): Promise<ITag[] | undefined> => {
251-
const config = initObj.config;
252255
return new Promise((resolve, reject) => {
253256
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("/");
261259
const manifestRepo: IAzureDevOpsRepo = {
262260
org: manifestUrlSplit[3],
263261
project: manifestUrlSplit[4],
264262
repo: manifestUrlSplit[6],
265263
};
266-
getAzureManifestSyncState(
267-
manifestRepo,
268-
config.azure_devops.access_token
269-
)
264+
getAzureManifestSyncState(manifestRepo, initObj.accessToken)
270265
.then((syncCommits: ITag[]) => {
271266
resolve(syncCommits);
272267
})
273268
.catch((e) => {
274269
reject(e);
275270
});
276271
} 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")
279274
) {
280-
const manifestUrlSplit = config.azure_devops?.manifest_repository.split(
281-
"/"
282-
);
275+
const manifestUrlSplit = initObj.manifestRepo.split("/");
283276
const manifestRepo: IGitHub = {
284277
reponame: manifestUrlSplit[4],
285278
username: manifestUrlSplit[3],
286279
};
287280

288-
getGithubManifestSyncState(
289-
manifestRepo,
290-
config.azure_devops.access_token
291-
)
281+
getGithubManifestSyncState(manifestRepo, initObj.accessToken)
292282
.then((syncCommits: ITag[]) => {
293283
resolve(syncCommits);
294284
})
@@ -310,10 +300,8 @@ export const getClusterSyncStatuses = (
310300
*/
311301
export const initialize = async (): Promise<InitObject> => {
312302
const config = Config();
313-
const key = await config.introspection!.azure!.key;
314303

315304
if (
316-
!key ||
317305
!config.introspection ||
318306
!config.azure_devops ||
319307
!config.introspection.azure ||
@@ -322,9 +310,10 @@ export const initialize = async (): Promise<InitObject> => {
322310
!config.introspection.azure.account_name ||
323311
!config.introspection.azure.table_name ||
324312
!config.introspection.azure.key ||
325-
!config.introspection.azure.partition_key
313+
!config.introspection.azure.partition_key ||
314+
!config.introspection.azure.key
326315
) {
327-
throw new Error(
316+
throw Error(
328317
"You need to run `spk init` and `spk deployment onboard` to configure `spk."
329318
);
330319
}
@@ -336,20 +325,24 @@ export const initialize = async (): Promise<InitObject> => {
336325
false,
337326
config.azure_devops.access_token
338327
),
339-
config,
340328
hldPipeline: new AzureDevOpsPipeline(
341329
config.azure_devops.org,
342330
config.azure_devops.project,
343331
true,
344332
config.azure_devops.access_token
345333
),
346-
key,
334+
key: config.introspection.azure.key,
347335
srcPipeline: new AzureDevOpsPipeline(
348336
config.azure_devops.org,
349337
config.azure_devops.project,
350338
false,
351339
config.azure_devops.access_token
352340
),
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,
353346
};
354347
};
355348

@@ -496,8 +489,8 @@ export const printDeployments = (
496489
deployment.pr.toString() in pullRequests
497490
) {
498491
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);
501494
} else {
502495
deploymentStatus = "Waiting";
503496
row.push("-");
@@ -551,41 +544,43 @@ export const printDeployments = (
551544
};
552545

553546
/**
554-
* Gets PR information for all the deployments
547+
* Gets PR information for all the deployments.
548+
*
555549
* @param deployments all deployments to be displayed
550+
* @param initObj initialization object
556551
*/
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));
563557
};
564558

565559
/**
566560
* Fetches pull request data for deployments that complete merge into HLD
567561
* by merging a PR
562+
*
568563
* @param deployment deployment for which PR has to be fetched
564+
* @param initObj initialization object
569565
*/
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);
586582
}
587-
});
588-
promises.push(promise);
583+
}
589584
};
590585

591586
/**

0 commit comments

Comments
 (0)