Skip to content

Commit

Permalink
added oliver's feedback + unit test demonstrating regex glitch and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jaiveerk committed Dec 12, 2022
1 parent f507264 commit 86aca60
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
31 changes: 13 additions & 18 deletions src/strategyHelpers/deploymentHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,20 @@ export async function annotateAndLabelResources(

const deploymentConfig = await getDeploymentConfig()
const annotationKeyLabel = getWorkflowAnnotationKeyLabel()
try {
await annotateResources(
files,
kubectl,
resourceTypes,
allPods,
annotationKeyLabel,
workflowFilePath,
deploymentConfig
)
} catch (ex) {
core.warning(`Failed to annotate resources: ${ex} `)
}

try {
await labelResources(files, kubectl, annotationKeyLabel)
} catch (ex) {
core.warning(`Failed to label resources: ${ex}`)
}
await annotateResources(
files,
kubectl,
resourceTypes,
allPods,
annotationKeyLabel,
workflowFilePath,
deploymentConfig
).catch((err) => core.warning(`Failed to annotate resources: ${err} `))

await labelResources(files, kubectl, annotationKeyLabel).catch((err) =>
core.warning(`Failed to label resources: ${err}`)
)
}

async function annotateResources(
Expand Down
17 changes: 9 additions & 8 deletions src/types/kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,18 @@ export class Kubectl {
core.debug('stdout from getNewReplicaSet is ' + JSON.stringify(stdout))
stdout.forEach((line: string) => {
const newreplicaset = 'newreplicaset'
if (line && line.toLowerCase().indexOf(newreplicaset) > -1)
if (line && line.toLowerCase().indexOf(newreplicaset) > -1) {
core.debug(
`found string of interest for replicaset, line is ${line}`
)
core.debug(
`substring is ${line.substring(newreplicaset.length).trim()}`
)
newReplicaSet = line
.substring(newreplicaset.length)
.trim()
.split(' ')[0]
core.debug(
`substring is ${line.substring(newreplicaset.length).trim()}`
)
newReplicaSet = line
.substring(newreplicaset.length)
.trim()
.split(' ')[0]
}
})
}

Expand Down
13 changes: 13 additions & 0 deletions src/utilities/workflowAnnotationUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,18 @@ describe('WorkflowAnnotationUtils', () => {
cleanLabel('Workflow Name / With Slashes / And Spaces')
).toEqual('Workflow_Name_-_With_Slashes_-_And_Spaces')
})
it('should return a blank string when regex fails (https://github.com/Azure/k8s-deploy/issues/266)', () => {
const label = '持续部署'
expect(cleanLabel(label)).toEqual('')

let removedInvalidChars = label
.replace(/\s/gi, '_')
.replace(/[\/\\\|]/gi, '-')
.replace(/[^-A-Za-z0-9_.]/gi, '')

const regex = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/
const regexResult = regex.exec(removedInvalidChars)
expect(regexResult).toBe(null)
})
})
})

0 comments on commit 86aca60

Please sign in to comment.