Vidya reddy/prettier code (#203)

This commit is contained in:
Vidya
2022-06-24 13:57:45 -07:00
committed by GitHub
parent 976c5c4981
commit dcd9bc6b1a
71 changed files with 16044 additions and 15876 deletions
+70 -66
View File
@@ -1,82 +1,86 @@
import * as core from "@actions/core";
import { ExecOutput } from "@actions/exec";
import { Kubectl } from "../types/kubectl";
import * as core from '@actions/core'
import {ExecOutput} from '@actions/exec'
import {Kubectl} from '../types/kubectl'
export function checkForErrors(
execResults: ExecOutput[],
warnIfError?: boolean
execResults: ExecOutput[],
warnIfError?: boolean
) {
let stderr = "";
execResults.forEach((result) => {
if (result?.exitCode !== 0) {
stderr += result?.stderr + " \n";
} else if (result?.stderr) {
core.warning(result.stderr);
}
});
let stderr = ''
execResults.forEach((result) => {
if (result?.exitCode !== 0) {
stderr += result?.stderr + ' \n'
} else if (result?.stderr) {
core.warning(result.stderr)
}
})
if (stderr.length > 0) {
if (warnIfError) {
core.warning(stderr.trim());
} else {
throw new Error(stderr.trim());
}
}
if (stderr.length > 0) {
if (warnIfError) {
core.warning(stderr.trim())
} else {
throw new Error(stderr.trim())
}
}
}
export async function getLastSuccessfulRunSha(
kubectl: Kubectl,
namespaceName: string,
annotationKey: string
kubectl: Kubectl,
namespaceName: string,
annotationKey: string
): Promise<string> {
try {
const result = await kubectl.getResource("namespace", namespaceName);
if (result?.stderr) {
core.warning(result.stderr);
return process.env.GITHUB_SHA;
} else if (result?.stdout) {
const annotationsSet = JSON.parse(result.stdout).metadata.annotations;
if (annotationsSet && annotationsSet[annotationKey]) {
return JSON.parse(annotationsSet[annotationKey].replace(/'/g, '"'))
.commit;
} else {
return "NA";
try {
const result = await kubectl.getResource('namespace', namespaceName)
if (result?.stderr) {
core.warning(result.stderr)
return process.env.GITHUB_SHA
} else if (result?.stdout) {
const annotationsSet = JSON.parse(result.stdout).metadata.annotations
if (annotationsSet && annotationsSet[annotationKey]) {
return JSON.parse(annotationsSet[annotationKey].replace(/'/g, '"'))
.commit
} else {
return 'NA'
}
}
}
} catch (ex) {
core.warning(`Failed to get commits from cluster. ${JSON.stringify(ex)}`);
return "";
}
} catch (ex) {
core.warning(`Failed to get commits from cluster. ${JSON.stringify(ex)}`)
return ''
}
}
export async function annotateChildPods(
kubectl: Kubectl,
resourceType: string,
resourceName: string,
annotationKeyValStr: string,
allPods
kubectl: Kubectl,
resourceType: string,
resourceName: string,
annotationKeyValStr: string,
allPods
): Promise<ExecOutput[]> {
let owner = resourceName;
if (resourceType.toLowerCase().indexOf("deployment") > -1) {
owner = await kubectl.getNewReplicaSet(resourceName);
}
let owner = resourceName
if (resourceType.toLowerCase().indexOf('deployment') > -1) {
owner = await kubectl.getNewReplicaSet(resourceName)
}
const commandExecutionResults = [];
if (allPods?.items && allPods.items?.length > 0) {
allPods.items.forEach((pod) => {
const owners = pod?.metadata?.ownerReferences;
if (owners) {
for (const ownerRef of owners) {
if (ownerRef.name === owner) {
commandExecutionResults.push(
kubectl.annotate("pod", pod.metadata.name, annotationKeyValStr)
);
break;
}
}
}
});
}
const commandExecutionResults = []
if (allPods?.items && allPods.items?.length > 0) {
allPods.items.forEach((pod) => {
const owners = pod?.metadata?.ownerReferences
if (owners) {
for (const ownerRef of owners) {
if (ownerRef.name === owner) {
commandExecutionResults.push(
kubectl.annotate(
'pod',
pod.metadata.name,
annotationKeyValStr
)
)
break
}
}
}
})
}
return await Promise.all(commandExecutionResults);
return await Promise.all(commandExecutionResults)
}