diff --git a/src/commands/deployment/get.test.ts b/src/commands/deployment/get.test.ts index d3351b5e8..5d7ca9af9 100644 --- a/src/commands/deployment/get.test.ts +++ b/src/commands/deployment/get.test.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-non-null-assertion */ import path from "path"; import { duration, IDeployment, status } from "spektate/lib/IDeployment"; import * as Deployment from "spektate/lib/IDeployment"; @@ -28,7 +27,6 @@ import { watchGetDeployments, } from "./get"; import * as get from "./get"; -import { IPullRequest } from "spektate/lib/repository/IPullRequest"; const MOCKED_INPUT_VALUES: CommandOptions = { buildId: "", @@ -337,42 +335,49 @@ describe("Print deployments", () => { "EUROPE", ]; - const matchItems = table!.filter((field) => field[2] === deployment[2]); - expect(matchItems).toHaveLength(1); // one matching row + expect(table).toBeDefined(); - (matchItems[0] as IDeployment[]).forEach((field, i) => { - expect(field).toEqual(deployment[i]); - }); - expect(matchItems[0]).toHaveLength(14); + if (table) { + const matchItems = table.filter((field) => field[2] === deployment[2]); + expect(matchItems).toHaveLength(1); // one matching row - table = printDeployments( - mockedDeps, - processOutputFormat("wide"), - 3, - mockedClusterSyncs - ); - expect(table).toHaveLength(3); + (matchItems[0] as IDeployment[]).forEach((field, i) => { + expect(field).toEqual(deployment[i]); + }); + expect(matchItems[0]).toHaveLength(14); + + table = printDeployments( + mockedDeps, + processOutputFormat("wide"), + 3, + mockedClusterSyncs + ); + expect(table).toHaveLength(3); + } }); }); describe("Cluster sync", () => { test("Verify cluster syncs", async () => { // test a github setup too - if (initObject.config.azure_devops?.manifest_repository) { - initObject.config.azure_devops!.manifest_repository! = "https://github.com/someone/something"; + if (initObject.manifestRepo) { + initObject.manifestRepo = "https://github.com/someone/something"; } const clusterSyncs = await getClusterSyncStatuses(initObject); expect(clusterSyncs).toBeDefined(); expect(clusterSyncs).toHaveLength(5); - expect(clusterSyncs![0].name).toBe("CANADA"); - expect(clusterSyncs![0].commit).toBe("efeeebe"); - expect(clusterSyncs![0].tagger).toBe("Weave Flux"); + + if (clusterSyncs) { + expect(clusterSyncs[0].name).toBe("CANADA"); + expect(clusterSyncs[0].commit).toBe("efeeebe"); + expect(clusterSyncs[0].tagger).toBe("Weave Flux"); + } }); test("Verify cluster syncs - empty", async () => { // test empty manifest scenario - if (initObject.config.azure_devops?.manifest_repository) { - initObject.config.azure_devops!.manifest_repository! = ""; + if (initObject.manifestRepo) { + initObject.manifestRepo = ""; } const clusterSyncs = await getClusterSyncStatuses(initObject); expect(clusterSyncs).toBeUndefined(); @@ -387,9 +392,10 @@ describe("Output formats", () => { undefined, mockedClusterSyncs ); - expect(table).not.toBeUndefined(); - table!.forEach((field) => { - expect(field).toHaveLength(20); - }); + expect(table).toBeDefined(); + + if (table) { + table.forEach((field) => expect(field).toHaveLength(20)); + } }); }); diff --git a/src/commands/deployment/get.ts b/src/commands/deployment/get.ts index 5407730c6..3eea9c783 100644 --- a/src/commands/deployment/get.ts +++ b/src/commands/deployment/get.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-non-null-assertion */ /* eslint-disable @typescript-eslint/no-use-before-define */ import Table from "cli-table"; import commander from "commander"; @@ -24,7 +23,6 @@ import { Config } from "../../config"; import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder"; import { isIntegerString } from "../../lib/validator"; import { logger } from "../../logger"; -import { ConfigYaml } from "../../types"; import decorator from "./get.decorator.json"; import { IPullRequest } from "spektate/lib/repository/IPullRequest"; @@ -45,11 +43,15 @@ export enum OUTPUT_FORMAT { * process */ export interface InitObject { - config: ConfigYaml; + accountName: string; + tableName: string; + partitionKey: string; clusterPipeline: AzureDevOpsPipeline; hldPipeline: AzureDevOpsPipeline; key: string; srcPipeline: AzureDevOpsPipeline; + manifestRepo?: string; + accessToken?: string; } /** @@ -170,13 +172,12 @@ export const getDeployments = ( initObj: InitObject, values: ValidatedOptions ): Promise => { - const config = initObj.config; const syncStatusesPromise = getClusterSyncStatuses(initObj); const deploymentsPromise = getDeploymentsBasedOnFilters( - config.introspection!.azure!.account_name!, + initObj.accountName, initObj.key, - config.introspection!.azure!.table_name!, - config.introspection!.azure!.partition_key!, + initObj.tableName, + initObj.partitionKey, initObj.srcPipeline, initObj.hldPipeline, initObj.clusterPipeline, @@ -195,7 +196,8 @@ export const getDeployments = ( const displayedDeployments = await displayDeployments( values, deployments, - syncStatuses + syncStatuses, + initObj ); resolve(displayedDeployments); }) @@ -209,16 +211,18 @@ export const getDeployments = ( * Displays the deployments based on output format requested and top n * @param values validated command line values * @param deployments list of deployments to display - * @param syncStatuses cluster sync statuses + * @param syncStatuses cluster sync statuses, + * @param initObj initialization object */ export const displayDeployments = ( values: ValidatedOptions, deployments: IDeployment[] | undefined, - syncStatuses: ITag[] | undefined + syncStatuses: ITag[] | undefined, + initObj: InitObject ): Promise => { return new Promise((resolve, reject) => { if (values.outputFormat === OUTPUT_FORMAT.WIDE) { - getPRs(deployments); + getPRs(deployments, initObj); } if (values.outputFormat === OUTPUT_FORMAT.JSON) { console.log(JSON.stringify(deployments, null, 2)); @@ -248,25 +252,16 @@ export const displayDeployments = ( export const getClusterSyncStatuses = ( initObj: InitObject ): Promise => { - const config = initObj.config; return new Promise((resolve, reject) => { try { - if ( - config.azure_devops?.manifest_repository && - config.azure_devops?.manifest_repository.includes("azure.com") - ) { - const manifestUrlSplit = config.azure_devops?.manifest_repository.split( - "/" - ); + if (initObj.manifestRepo && initObj.manifestRepo.includes("azure.com")) { + const manifestUrlSplit = initObj.manifestRepo.split("/"); const manifestRepo: IAzureDevOpsRepo = { org: manifestUrlSplit[3], project: manifestUrlSplit[4], repo: manifestUrlSplit[6], }; - getAzureManifestSyncState( - manifestRepo, - config.azure_devops.access_token - ) + getAzureManifestSyncState(manifestRepo, initObj.accessToken) .then((syncCommits: ITag[]) => { resolve(syncCommits); }) @@ -274,21 +269,16 @@ export const getClusterSyncStatuses = ( reject(e); }); } else if ( - config.azure_devops?.manifest_repository && - config.azure_devops?.manifest_repository.includes("github.com") + initObj.manifestRepo && + initObj.manifestRepo.includes("github.com") ) { - const manifestUrlSplit = config.azure_devops?.manifest_repository.split( - "/" - ); + const manifestUrlSplit = initObj.manifestRepo.split("/"); const manifestRepo: IGitHub = { reponame: manifestUrlSplit[4], username: manifestUrlSplit[3], }; - getGithubManifestSyncState( - manifestRepo, - config.azure_devops.access_token - ) + getGithubManifestSyncState(manifestRepo, initObj.accessToken) .then((syncCommits: ITag[]) => { resolve(syncCommits); }) @@ -310,10 +300,8 @@ export const getClusterSyncStatuses = ( */ export const initialize = async (): Promise => { const config = Config(); - const key = await config.introspection!.azure!.key; if ( - !key || !config.introspection || !config.azure_devops || !config.introspection.azure || @@ -322,9 +310,10 @@ export const initialize = async (): Promise => { !config.introspection.azure.account_name || !config.introspection.azure.table_name || !config.introspection.azure.key || - !config.introspection.azure.partition_key + !config.introspection.azure.partition_key || + !config.introspection.azure.key ) { - throw new Error( + throw Error( "You need to run `spk init` and `spk deployment onboard` to configure `spk." ); } @@ -336,20 +325,24 @@ export const initialize = async (): Promise => { false, config.azure_devops.access_token ), - config, hldPipeline: new AzureDevOpsPipeline( config.azure_devops.org, config.azure_devops.project, true, config.azure_devops.access_token ), - key, + key: config.introspection.azure.key, srcPipeline: new AzureDevOpsPipeline( config.azure_devops.org, config.azure_devops.project, false, config.azure_devops.access_token ), + accountName: config.introspection.azure.account_name, + tableName: config.introspection.azure.table_name, + partitionKey: config.introspection.azure.partition_key, + manifestRepo: config.azure_devops.manifest_repository, + accessToken: config.azure_devops.access_token, }; }; @@ -496,8 +489,8 @@ export const printDeployments = ( deployment.pr.toString() in pullRequests ) { row.push(deployment.pr); - if (pullRequests[deployment.pr!.toString()].mergedBy) { - row.push(pullRequests[deployment.pr!.toString()].mergedBy?.name); + if (pullRequests[deployment.pr.toString()].mergedBy) { + row.push(pullRequests[deployment.pr.toString()].mergedBy?.name); } else { deploymentStatus = "Waiting"; row.push("-"); @@ -551,41 +544,43 @@ export const printDeployments = ( }; /** - * Gets PR information for all the deployments + * Gets PR information for all the deployments. + * * @param deployments all deployments to be displayed + * @param initObj initialization object */ -export const getPRs = (deployments: IDeployment[] | undefined) => { - if (deployments && deployments.length > 0) { - deployments.forEach((deployment: IDeployment) => { - fetchPRInformation(deployment); - }); - } +export const getPRs = ( + deployments: IDeployment[] | undefined, + initObj: InitObject +): void => { + (deployments || []).forEach((d) => fetchPRInformation(d, initObj)); }; /** * Fetches pull request data for deployments that complete merge into HLD * by merging a PR + * * @param deployment deployment for which PR has to be fetched + * @param initObj initialization object */ -export const fetchPRInformation = (deployment: IDeployment) => { - const config = Config(); - if (!deployment.hldRepo || !deployment.pr) { - return; - } - const repo: IAzureDevOpsRepo | IGitHub | undefined = getRepositoryFromURL( - deployment.hldRepo! - ); - const promise = fetchPR( - repo!, - deployment.pr!.toString(), - config.introspection?.azure?.source_repo_access_token - ); - promise.then((pr: IPullRequest | undefined) => { - if (pr) { - pullRequests[deployment.pr!.toString()] = pr; +export const fetchPRInformation = ( + deployment: IDeployment, + initObj: InitObject +): void => { + if (deployment.hldRepo && deployment.pr) { + const repo = getRepositoryFromURL(deployment.hldRepo); + const strPr = deployment.pr.toString(); + + if (repo) { + const promise = fetchPR(repo, strPr, initObj.accountName); + promise.then((pr) => { + if (pr) { + pullRequests[strPr] = pr; + } + }); + promises.push(promise); } - }); - promises.push(promise); + } }; /**