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

fix: Trim trailing non-word characters for hostname labels #200

Merged
merged 5 commits into from
Mar 12, 2024
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
4 changes: 2 additions & 2 deletions actions/cursor-deploy/dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9737,8 +9737,8 @@ async function getDeploymentHash(deployMode, rollbackCommitHash) {
return treeHash;
}
function branchNameToHostnameLabel(ref) {
var _a;
const hostnameLabel = (_a = ref == null ? void 0 : ref.split("refs/heads/").pop()) == null ? void 0 : _a.replace(/[^\w]/gi, "-").replace(/-{2,}/gi, "-").toLowerCase().slice(0, 60).trim();
var _a, _b;
const hostnameLabel = (_b = (_a = ref == null ? void 0 : ref.split("refs/heads/").pop()) == null ? void 0 : _a.replace(/[^\w]/gi, "-").replace(/-{2,}/gi, "-").toLowerCase().slice(0, 60)) == null ? void 0 : _b.replace(/(-|_)$/gi, "").trim();
if (!hostnameLabel) {
throw new Error("Could not get a valid hostname label from branch name");
}
Expand Down
27 changes: 27 additions & 0 deletions actions/cursor-deploy/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,33 @@ describe('Branch Sanitize - branchNameToHostnameLabel', () => {
)
expect(output).toBe('hello-my-very-weird_branch-100-original-whoooohoooooo-lets-d')
})

it(`trims trailing hyphens 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 hyphens 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 hyphens`, async () => {
const output = branchNameToHostnameLabel('refs/heads/feature-hello-0000--')
expect(output).toBe('feature-hello-0000')
})

it(`trims trailing underscores`, 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(/(-|_)$/gi, '') // get rid of trailing "-" and "_"
.trim()

if (!hostnameLabel) {
Expand Down
Loading