Skip to content

Commit e8a841d

Browse files
authored
Fixing Regex Issue + Adding Check for Failures Connecting to Github Repos (#271)
* changed ubuntu runner * changed minikube action * Version formatting * nonedriveR * update kube version * installing conntrack' * updated other actions * update bg ingress api version * prettify * updated ingress backend for new api version * Added path type * prettify * added logging * added try catch logic to prevent future failures if annotations fail since failing annotations shouldn't affect users * added nullcheck * Added fallback filename if workflow fails to get github filepath due to runner issues * cleanup * added oliver's feedback + unit test demonstrating regex glitch and fix * no longer using blank string for failed regex
1 parent da1e907 commit e8a841d

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

src/strategyHelpers/deploymentHelper.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,15 @@ export async function annotateAndLabelResources(
150150
resourceTypes: Resource[],
151151
allPods: any
152152
) {
153+
const defaultWorkflowFileName = 'k8s-deploy-failed-workflow-annotation'
153154
const githubToken = core.getInput('token')
154-
const workflowFilePath = await getWorkflowFilePath(githubToken)
155+
let workflowFilePath
156+
try {
157+
workflowFilePath = await getWorkflowFilePath(githubToken)
158+
} catch (ex) {
159+
core.warning(`Failed to extract workflow file name: ${ex}`)
160+
workflowFilePath = defaultWorkflowFileName
161+
}
155162

156163
const deploymentConfig = await getDeploymentConfig()
157164
const annotationKeyLabel = getWorkflowAnnotationKeyLabel()
@@ -164,8 +171,11 @@ export async function annotateAndLabelResources(
164171
annotationKeyLabel,
165172
workflowFilePath,
166173
deploymentConfig
174+
).catch((err) => core.warning(`Failed to annotate resources: ${err} `))
175+
176+
await labelResources(files, kubectl, annotationKeyLabel).catch((err) =>
177+
core.warning(`Failed to label resources: ${err}`)
167178
)
168-
await labelResources(files, kubectl, annotationKeyLabel)
169179
}
170180

171181
async function annotateResources(

src/types/kubectl.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,21 @@ export class Kubectl {
7070
let newReplicaSet = ''
7171
if (result?.stdout) {
7272
const stdout = result.stdout.split('\n')
73+
core.debug('stdout from getNewReplicaSet is ' + JSON.stringify(stdout))
7374
stdout.forEach((line: string) => {
7475
const newreplicaset = 'newreplicaset'
75-
if (line && line.toLowerCase().indexOf(newreplicaset) > -1)
76+
if (line && line.toLowerCase().indexOf(newreplicaset) > -1) {
77+
core.debug(
78+
`found string of interest for replicaset, line is ${line}`
79+
)
80+
core.debug(
81+
`substring is ${line.substring(newreplicaset.length).trim()}`
82+
)
7683
newReplicaSet = line
7784
.substring(newreplicaset.length)
7885
.trim()
7986
.split(' ')[0]
87+
}
8088
})
8189
}
8290

src/utilities/workflowAnnotationUtils.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,18 @@ describe('WorkflowAnnotationUtils', () => {
1616
cleanLabel('Workflow Name / With Slashes / And Spaces')
1717
).toEqual('Workflow_Name_-_With_Slashes_-_And_Spaces')
1818
})
19+
it('should return a blank string when regex fails (https://github.com/Azure/k8s-deploy/issues/266)', () => {
20+
const label = '持续部署'
21+
expect(cleanLabel(label)).toEqual('github-workflow-file')
22+
23+
let removedInvalidChars = label
24+
.replace(/\s/gi, '_')
25+
.replace(/[\/\\\|]/gi, '-')
26+
.replace(/[^-A-Za-z0-9_.]/gi, '')
27+
28+
const regex = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/
29+
const regexResult = regex.exec(removedInvalidChars)
30+
expect(regexResult).toBe(null)
31+
})
1932
})
2033
})

src/utilities/workflowAnnotationUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,8 @@ export function cleanLabel(label: string): string {
4343
.replace(/[^-A-Za-z0-9_.]/gi, '')
4444

4545
const regex = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/
46-
return regex.exec(removedInvalidChars)[0] || ''
46+
const regexResult = regex.exec(removedInvalidChars) || [
47+
'github-workflow-file'
48+
]
49+
return regexResult[0]
4750
}

0 commit comments

Comments
 (0)