Skip to content

Commit 5247b68

Browse files
mrseanbainesgithub-actions[bot]macko911
authored
fix: Trim trailing non-word characters for hostname labels (#200)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Matej Vobornik <[email protected]>
1 parent 27b4e70 commit 5247b68

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

actions/cursor-deploy/dist/main/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9737,8 +9737,8 @@ async function getDeploymentHash(deployMode, rollbackCommitHash) {
97379737
return treeHash;
97389738
}
97399739
function branchNameToHostnameLabel(ref) {
9740-
var _a;
9741-
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();
9740+
var _a, _b;
9741+
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();
97429742
if (!hostnameLabel) {
97439743
throw new Error("Could not get a valid hostname label from branch name");
97449744
}

actions/cursor-deploy/index.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,33 @@ describe('Branch Sanitize - branchNameToHostnameLabel', () => {
338338
)
339339
expect(output).toBe('hello-my-very-weird_branch-100-original-whoooohoooooo-lets-d')
340340
})
341+
342+
it(`trims trailing hyphens when the branch name exceeds the max allowed length`, async () => {
343+
const output = branchNameToHostnameLabel(
344+
'refs/heads/feature-hello-0000-the-quick-brown-fox-jumped-over-the-lazy-dog' // the 60th character here (minus 'refs/heads/') is a hyphen
345+
)
346+
expect(output).toBe('feature-hello-0000-the-quick-brown-fox-jumped-over-the-lazy')
347+
})
348+
349+
it(`trims trailing hyphens when the branch name does not exceed the max allowed length`, async () => {
350+
const output = branchNameToHostnameLabel('refs/heads/feature-hello-0000-')
351+
expect(output).toBe('feature-hello-0000')
352+
})
353+
354+
it(`trims multiple hyphens`, async () => {
355+
const output = branchNameToHostnameLabel('refs/heads/feature-hello-0000--')
356+
expect(output).toBe('feature-hello-0000')
357+
})
358+
359+
it(`trims trailing underscores`, async () => {
360+
const output = branchNameToHostnameLabel('refs/heads/feature_hello_0000_')
361+
expect(output).toBe('feature_hello_0000')
362+
})
363+
364+
it(`trims whitespace`, async () => {
365+
const output = branchNameToHostnameLabel(' refs/heads/feature-hello-0000 ')
366+
expect(output).toBe('feature-hello-0000')
367+
})
341368
})
342369

343370
//#region Custom Assertions

actions/cursor-deploy/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function branchNameToHostnameLabel(ref: string) {
153153
.replace(/-{2,}/gi, '-') // get rid of multiple consecutive "-"
154154
.toLowerCase()
155155
.slice(0, 60)
156+
?.replace(/(-|_)$/gi, '') // get rid of trailing "-" and "_"
156157
.trim()
157158

158159
if (!hostnameLabel) {

0 commit comments

Comments
 (0)