Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

abstract methods to avoid drift #273

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/utilities/workflowAnnotationUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {cleanLabel} from '../utilities/workflowAnnotationUtils'
import {
cleanLabel,
removeInvalidLabelCharacters,
VALID_LABEL_REGEX
} from '../utilities/workflowAnnotationUtils'

describe('WorkflowAnnotationUtils', () => {
describe('cleanLabel', () => {
Expand All @@ -20,13 +24,9 @@ describe('WorkflowAnnotationUtils', () => {
const label = '持续部署'
expect(cleanLabel(label)).toEqual('github-workflow-file')

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

const regex = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/
const regexResult = regex.exec(removedInvalidChars)
const regexResult = VALID_LABEL_REGEX.exec(removedInvalidChars)
expect(regexResult).toBe(null)
})
})
Expand Down
17 changes: 11 additions & 6 deletions src/utilities/workflowAnnotationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {DeploymentConfig} from '../types/deploymentConfig'

const ANNOTATION_PREFIX = 'actions.github.com'

export const VALID_LABEL_REGEX = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/

export function getWorkflowAnnotations(
lastSuccessRunSha: string,
workflowFilePath: string,
Expand Down Expand Up @@ -37,14 +39,17 @@ export function getWorkflowAnnotationKeyLabel(): string {
* @returns cleaned label
*/
export function cleanLabel(label: string): string {
let removedInvalidChars = label
.replace(/\s/gi, '_')
.replace(/[\/\\\|]/gi, '-')
.replace(/[^-A-Za-z0-9_.]/gi, '')
let removedInvalidChars = removeInvalidLabelCharacters(label)

const regex = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/
const regexResult = regex.exec(removedInvalidChars) || [
const regexResult = VALID_LABEL_REGEX.exec(removedInvalidChars) || [
'github-workflow-file'
]
return regexResult[0]
}

export function removeInvalidLabelCharacters(label: string): string {
jaiveerk marked this conversation as resolved.
Show resolved Hide resolved
return label
.replace(/\s/gi, '_')
.replace(/[\/\\\|]/gi, '-')
.replace(/[^-A-Za-z0-9_.]/gi, '')
}