From b3381a2f947e6e14f0d53835002006f993a13b7e Mon Sep 17 00:00:00 2001 From: Koushik Dey Date: Thu, 2 Jul 2020 18:05:13 +0530 Subject: [PATCH] Addressed review comments --- lib/utilities/utility.js | 24 ++++++++++++------------ src/utilities/utility.ts | 26 +++++++++++++------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/utilities/utility.js b/lib/utilities/utility.js index cb2cada0..58f160a3 100644 --- a/lib/utilities/utility.js +++ b/lib/utilities/utility.js @@ -40,7 +40,7 @@ function checkForErrors(execResults, warnIfError) { } }); if (stderr.length > 0) { - if (!!warnIfError) { + if (warnIfError) { core.warning(stderr.trim()); } else { @@ -56,10 +56,10 @@ function annotateChildPods(kubectl, resourceType, resourceName, allPods) { if (resourceType.toLowerCase().indexOf('deployment') > -1) { owner = kubectl.getNewReplicaSet(resourceName); } - if (!!allPods && !!allPods.items && allPods.items.length > 0) { + if (allPods && allPods.items && allPods.items.length > 0) { allPods.items.forEach((pod) => { const owners = pod.metadata.ownerReferences; - if (!!owners) { + if (owners) { owners.forEach(ownerRef => { if (ownerRef.name === owner) { commandExecutionResults.push(kubectl.annotate('pod', pod.metadata.name, constants_1.workflowAnnotations, true)); @@ -73,21 +73,21 @@ function annotateChildPods(kubectl, resourceType, resourceName, allPods) { exports.annotateChildPods = annotateChildPods; function annotateNamespace(kubectl, namespaceName) { const result = kubectl.getResource('namespace', namespaceName); - if (result == null) { + if (!result) { return { code: -1, stderr: 'Failed to get resource' }; } - else if (!!result && !!result.stderr) { + else if (result && result.stderr) { return result; } - if (!!result && !!result.stdout) { + if (result && result.stdout) { const annotationsSet = JSON.parse(result.stdout).metadata.annotations; - if (!!annotationsSet && !!annotationsSet.runUri && annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) { - core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`); - return { code: 0, stdout: '' }; - } - else { - return kubectl.annotate('namespace', namespaceName, constants_1.workflowAnnotations, true); + if (annotationsSet && annotationsSet.runUri) { + if (annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) { + core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`); + return { code: 0, stdout: '' }; + } } + return kubectl.annotate('namespace', namespaceName, constants_1.workflowAnnotations, true); } } exports.annotateNamespace = annotateNamespace; diff --git a/src/utilities/utility.ts b/src/utilities/utility.ts index 398d4a4a..16e5018b 100644 --- a/src/utilities/utility.ts +++ b/src/utilities/utility.ts @@ -41,7 +41,7 @@ export function checkForErrors(execResults: IExecSyncResult[], warnIfError?: boo } }); if (stderr.length > 0) { - if (!!warnIfError) { + if (warnIfError) { core.warning(stderr.trim()); } else { throw new Error(stderr.trim()); @@ -57,10 +57,10 @@ export function annotateChildPods(kubectl: Kubectl, resourceType: string, resour owner = kubectl.getNewReplicaSet(resourceName); } - if (!!allPods && !!allPods.items && allPods.items.length > 0) { + if (allPods && allPods.items && allPods.items.length > 0) { allPods.items.forEach((pod) => { const owners = pod.metadata.ownerReferences; - if (!!owners) { + if (owners) { owners.forEach(ownerRef => { if (ownerRef.name === owner) { commandExecutionResults.push(kubectl.annotate('pod', pod.metadata.name, workflowAnnotations, true)); @@ -75,22 +75,22 @@ export function annotateChildPods(kubectl: Kubectl, resourceType: string, resour export function annotateNamespace(kubectl: Kubectl, namespaceName: string): IExecSyncResult { const result = kubectl.getResource('namespace', namespaceName); - if (result == null) { - return { code: -1, stderr: 'Failed to get resource' }; + if (!result) { + return { code: -1, stderr: 'Failed to get resource' } as IExecSyncResult; } - else if (!!result && !!result.stderr) { + else if (result && result.stderr) { return result; } - if (!!result && !!result.stdout) { + if (result && result.stdout) { const annotationsSet = JSON.parse(result.stdout).metadata.annotations; - if (!!annotationsSet && !!annotationsSet.runUri && annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) { - core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`); - return { code: 0, stdout: '' }; - } - else { - return kubectl.annotate('namespace', namespaceName, workflowAnnotations, true); + if (annotationsSet && annotationsSet.runUri) { + if (annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) { + core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`); + return { code: 0, stdout: '' } as IExecSyncResult; + } } + return kubectl.annotate('namespace', namespaceName, workflowAnnotations, true); } }