Skip to content

Commit 19d66d6

Browse files
authored
add clean function (#211)
1 parent 72a09f4 commit 19d66d6

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/strategyHelpers/deploymentHelper.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {parseRouteStrategy, RouteStrategy} from '../types/routeStrategy'
2323
import {ExecOutput} from '@actions/exec'
2424
import {
2525
getWorkflowAnnotationKeyLabel,
26-
getWorkflowAnnotations
26+
getWorkflowAnnotations,
27+
cleanLabel
2728
} from '../utilities/workflowAnnotationUtils'
2829
import {
2930
annotateChildPods,
@@ -214,10 +215,10 @@ async function labelResources(
214215
label: string
215216
) {
216217
const labels = [
217-
`workflowFriendlyName=${normalizeWorkflowStrLabel(
218-
process.env.GITHUB_WORKFLOW
218+
`workflowFriendlyName=${cleanLabel(
219+
normalizeWorkflowStrLabel(process.env.GITHUB_WORKFLOW)
219220
)}`,
220-
`workflow=${label}`
221+
`workflow=${cleanLabel(label)}`
221222
]
222223

223224
checkForErrors([await kubectl.labelFiles(files, labels)], true)

src/utilities/workflowAnnotationUtils.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import {prefixObjectKeys} from '../utilities/workflowAnnotationUtils'
1+
import {
2+
cleanLabel,
3+
prefixObjectKeys
4+
} from '../utilities/workflowAnnotationUtils'
25

36
describe('WorkflowAnnotationUtils', () => {
47
describe('prefixObjectKeys', () => {
@@ -15,4 +18,16 @@ describe('WorkflowAnnotationUtils', () => {
1518
expect(prefixObjectKeys(obj, prefix)).toEqual(expected)
1619
})
1720
})
21+
22+
describe('cleanLabel', () => {
23+
it('should clean label', () => {
24+
const alreadyClean = 'alreadyClean'
25+
expect(cleanLabel(alreadyClean)).toEqual(alreadyClean)
26+
expect(cleanLabel('.startInvalid')).toEqual('startInvalid')
27+
expect(cleanLabel('with%S0ME&invalid#chars')).toEqual(
28+
'withS0MEinvalidchars'
29+
)
30+
expect(cleanLabel('with⚒️emoji')).toEqual('withemoji')
31+
})
32+
})
1833
})

src/utilities/workflowAnnotationUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ export function getWorkflowAnnotationKeyLabel(
4747
.digest('hex')
4848
return `githubWorkflow_${hashKey}`
4949
}
50+
51+
/**
52+
* Cleans label to match valid kubernetes label specification by removing invalid characters
53+
* @param label
54+
* @returns cleaned label
55+
*/
56+
export function cleanLabel(label: string): string {
57+
const removedInvalidChars = label.replace(/[^-A-Za-z0-9_.]/gi, '')
58+
const regex = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/
59+
return regex.exec(removedInvalidChars)[0] || ''
60+
}

0 commit comments

Comments
 (0)