Skip to content

Commit

Permalink
fix: Trim trailing non-word characters
Browse files Browse the repository at this point in the history
  • Loading branch information
mrseanbaines committed Jan 26, 2024
1 parent 34f4d49 commit 12a1eb2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
22 changes: 22 additions & 0 deletions actions/cursor-deploy/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,28 @@ describe('Branch Sanitize - branchNameToHostnameLabel', () => {
)
expect(output).toBe('hello-my-very-weird_branch-100-original-whoooohoooooo-lets-d')
})

it(`trims trailing non-word characters when the branch name exceeds the max allowed length`, async () => {
const output = branchNameToHostnameLabel(
'refs/heads/feature-hello-0000-the-quick-brown-fox-jumped-over-the-lazy-dog' // the 60th character here (minus 'refs/heads/') is a hyphen
)
expect(output).toBe('feature-hello-0000-the-quick-brown-fox-jumped-over-the-lazy')
})

it(`trims trailing non-word characters when the branch name does not exceed the max allowed length`, async () => {
const output = branchNameToHostnameLabel('refs/heads/feature-hello-0000-')
expect(output).toBe('feature-hello-0000')
})

it(`trims multiple non-word characters`, async () => {
const output = branchNameToHostnameLabel('refs/heads/feature-hello-0000--')
expect(output).toBe('feature-hello-0000')
})

it(`trims whitespace`, async () => {
const output = branchNameToHostnameLabel(' refs/heads/feature-hello-0000 ')
expect(output).toBe('feature-hello-0000')
})
})

//#region Custom Assertions
Expand Down
1 change: 1 addition & 0 deletions actions/cursor-deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export function branchNameToHostnameLabel(ref: string) {
.replace(/-{2,}/gi, '-') // get rid of multiple consecutive "-"
.toLowerCase()
.slice(0, 60)
?.replace(/[^\w]$/gi, '') // get rid of trailing non-word characters
.trim()

if (!hostnameLabel) {
Expand Down

0 comments on commit 12a1eb2

Please sign in to comment.