From f9acc4f77259e4a9e0f2d07605894c7b0008a118 Mon Sep 17 00:00:00 2001 From: Koushik Dey Date: Fri, 28 Aug 2020 14:42:55 +0530 Subject: [PATCH] WokflowFileName added in annotations for View All Runs in UX --- __tests__/run.test.ts | 8 ++++---- lib/constants.js | 3 ++- lib/utilities/strategy-helpers/deployment-helper.js | 6 +++--- src/constants.ts | 3 ++- src/utilities/strategy-helpers/deployment-helper.ts | 6 +++--- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/__tests__/run.test.ts b/__tests__/run.test.ts index a130584b..3b7437fb 100644 --- a/__tests__/run.test.ts +++ b/__tests__/run.test.ts @@ -293,7 +293,7 @@ test("deployment - deploy() - deploy force flag on", async () => { }); test("deployment - deploy() - Annotate & label resources", async () => { - let annotationKeyValStr = getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW) + '=' + getWorkflowAnnotationsJson('currentCommit'); + let annotationKeyValStr = getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW) + '=' + getWorkflowAnnotationsJson('currentCommit', '.github/workflows/workflow.yml'); const KubernetesManifestUtilityMock = mocked(KubernetesManifestUtility, true); KubernetesManifestUtilityMock.checkManifestStability = jest.fn().mockReturnValue(""); const KubernetesObjectUtilityMock = mocked(KubernetesObjectUtility, true); @@ -320,8 +320,8 @@ test("deployment - deploy() - Annotate & label resources", async () => { }); test("deployment - deploy() - Annotate & label resources for a new workflow", async () => { - process.env.GITHUB_WORKFLOW = 'NewWorkflow'; - let annotationKeyValStr = getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW) + '=' + getWorkflowAnnotationsJson('NA'); + process.env.GITHUB_WORKFLOW = '.github/workflows/NewWorkflow.yml'; + let annotationKeyValStr = getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW) + '=' + getWorkflowAnnotationsJson('NA', '.github/workflows/NewWorkflow.yml'); const KubernetesManifestUtilityMock = mocked(KubernetesManifestUtility, true); KubernetesManifestUtilityMock.checkManifestStability = jest.fn().mockReturnValue(""); const KubernetesObjectUtilityMock = mocked(KubernetesObjectUtility, true); @@ -344,7 +344,7 @@ test("deployment - deploy() - Annotate & label resources for a new workflow", as expect(kubeCtl.annotateFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"], annotationKeyValStr); expect(kubeCtl.annotate).toBeCalledTimes(2); expect(kubeCtl.labelFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"], - [`workflowFriendlyName=${process.env.GITHUB_WORKFLOW}`, `workflow=${getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW)}`]); + [`workflowFriendlyName=NewWorkflow.yml`, `workflow=${getWorkflowAnnotationKeyLabel(process.env.GITHUB_WORKFLOW)}`]); }); test("deployment - deploy() - Annotate resources failed", async () => { diff --git a/lib/constants.js b/lib/constants.js index 1ec3b50f..9ee05150 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -25,11 +25,12 @@ ServiceTypes.clusterIP = 'ClusterIP'; exports.deploymentTypes = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset']; exports.workloadTypes = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset', 'job', 'cronjob']; exports.workloadTypesWithRolloutStatus = ['deployment', 'daemonset', 'statefulset']; -function getWorkflowAnnotationsJson(lastSuccessRunSha) { +function getWorkflowAnnotationsJson(lastSuccessRunSha, workflowFilePath) { return `{` + `'run': '${process.env.GITHUB_RUN_ID}',` + `'repository': '${process.env.GITHUB_REPOSITORY}',` + `'workflow': '${process.env.GITHUB_WORKFLOW}',` + + `'workflowFileName': '${workflowFilePath.replace(".github/workflows/", "")}',` + `'jobName': '${process.env.GITHUB_JOB}',` + `'createdBy': '${process.env.GITHUB_ACTOR}',` + `'runUri': 'https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}',` diff --git a/lib/utilities/strategy-helpers/deployment-helper.js b/lib/utilities/strategy-helpers/deployment-helper.js index d482e853..b317e2b9 100644 --- a/lib/utilities/strategy-helpers/deployment-helper.js +++ b/lib/utilities/strategy-helpers/deployment-helper.js @@ -114,14 +114,14 @@ function annotateAndLabelResources(files, kubectl, resourceTypes, allPods) { return __awaiter(this, void 0, void 0, function* () { const workflowFilePath = yield utility_1.getWorkflowFilePath(TaskInputParameters.githubToken); const annotationKeyLabel = models.getWorkflowAnnotationKeyLabel(workflowFilePath); - annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel); + annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath); labelResources(files, kubectl, annotationKeyLabel); }); } -function annotateResources(files, kubectl, resourceTypes, allPods, annotationKey) { +function annotateResources(files, kubectl, resourceTypes, allPods, annotationKey, workflowFilePath) { const annotateResults = []; const lastSuccessSha = utility_1.getLastSuccessfulRunSha(kubectl, TaskInputParameters.namespace, annotationKey); - let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha); + let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha, workflowFilePath); annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, annotationKeyValStr)); annotateResults.push(kubectl.annotateFiles(files, annotationKeyValStr)); resourceTypes.forEach(resource => { diff --git a/src/constants.ts b/src/constants.ts index d29d3235..62d1bdf0 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -25,11 +25,12 @@ export const deploymentTypes: string[] = ['deployment', 'replicaset', 'daemonset export const workloadTypes: string[] = ['deployment', 'replicaset', 'daemonset', 'pod', 'statefulset', 'job', 'cronjob']; export const workloadTypesWithRolloutStatus: string[] = ['deployment', 'daemonset', 'statefulset']; -export function getWorkflowAnnotationsJson(lastSuccessRunSha: string): string { +export function getWorkflowAnnotationsJson(lastSuccessRunSha: string, workflowFilePath: string): string { return `{` + `'run': '${process.env.GITHUB_RUN_ID}',` + `'repository': '${process.env.GITHUB_REPOSITORY}',` + `'workflow': '${process.env.GITHUB_WORKFLOW}',` + + `'workflowFileName': '${workflowFilePath.replace(".github/workflows/", "")}',` + `'jobName': '${process.env.GITHUB_JOB}',` + `'createdBy': '${process.env.GITHUB_ACTOR}',` + `'runUri': 'https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}',` diff --git a/src/utilities/strategy-helpers/deployment-helper.ts b/src/utilities/strategy-helpers/deployment-helper.ts index 68c746a9..b162c6b3 100644 --- a/src/utilities/strategy-helpers/deployment-helper.ts +++ b/src/utilities/strategy-helpers/deployment-helper.ts @@ -115,14 +115,14 @@ async function checkManifestStability(kubectl: Kubectl, resources: Resource[]): async function annotateAndLabelResources(files: string[], kubectl: Kubectl, resourceTypes: Resource[], allPods: any) { const workflowFilePath = await getWorkflowFilePath(TaskInputParameters.githubToken); const annotationKeyLabel = models.getWorkflowAnnotationKeyLabel(workflowFilePath); - annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel); + annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath); labelResources(files, kubectl, annotationKeyLabel); } -function annotateResources(files: string[], kubectl: Kubectl, resourceTypes: Resource[], allPods: any, annotationKey: string) { +function annotateResources(files: string[], kubectl: Kubectl, resourceTypes: Resource[], allPods: any, annotationKey: string, workflowFilePath: string) { const annotateResults: IExecSyncResult[] = []; const lastSuccessSha = getLastSuccessfulRunSha(kubectl, TaskInputParameters.namespace, annotationKey); - let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha); + let annotationKeyValStr = annotationKey + '=' + models.getWorkflowAnnotationsJson(lastSuccessSha, workflowFilePath); annotateResults.push(kubectl.annotate('namespace', TaskInputParameters.namespace, annotationKeyValStr)); annotateResults.push(kubectl.annotateFiles(files, annotationKeyValStr)); resourceTypes.forEach(resource => {