Skip to content

Commit

Permalink
Truncate branchName (if necessary) to meet RFC 1035 criteria (#17)
Browse files Browse the repository at this point in the history
* Truncate branchName (if necessary) to meet RFC 1035 criteria

* increase patch version

* fix: missed re-assignment

* fix: missed the dashes in between in calculations

* fix: remove stagingPrefix when truncated

* fix: remove double semicolon
  • Loading branch information
sebastianroming authored Feb 5, 2024
1 parent 157c873 commit 8a7ddc6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
45 changes: 44 additions & 1 deletion vercel-deployment-task-source/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,50 @@ async function run() {
getStagingPrefix(vercelOrgId, vercelToken),
]);
const escapedBranchName = branchName.replace(/[^a-zA-Z0-9\-]-?/g, '-');
const aliasHostname = `${projectName}-${escapedBranchName}-${stagingPrefix}.vercel.app`;
/**
* Truncating branch name according to RFC 1035 if necessary
* Maximum length is 63 characters.
*
* Read more: https://vercel.com/guides/why-is-my-vercel-deployment-url-being-shortened
*
* projectName has a fixedLength `x`
* stagingPrefix has a fixedLenght `y`
* .vercel.app has a fixedLength `11`
* two dashes
*
* escapedBranchName can have a maximum length of 63-11-2-y-x
*
* This can cause confusion if you have all branches following a scheme, e.g.
* feature/PREFIX-12345-my-feature-branch-name
* feature/PREFIX-12346-my-second-feature-branch-name
*
* which can produce identical branchNames in the alias:
* longer-project-name-feature-prefix-12-staging-prefix.vercel.app
* longer-project-name-feature-prefix-12-staging-prefix.vercel.app
*
* Therefore, if the alias would exceed 63 characters, we remove the
* stagingPrefix to have the longest branchName substring possible:
* longer-project-name-feature-prefix-12345-my-feature.vercel.app
* longer-project-name-feature-prefix-12346-my-second-f.vercel.app
*/
const branchNameAllowedLength = 50-projectName.length-stagingPrefix.length;
let aliasHostname = `${projectName}-${escapedBranchName}-${stagingPrefix}.vercel.app`;

if (escapedBranchName.length > branchNameAllowedLength) {
// Calculate the maximum length of the branchName by removing the stagingPrefix and the dash
const branchNameExtendedLength = branchNameAllowedLength+stagingPrefix.length+1;

let aliasingBranchName = escapedBranchName.substring(0, branchNameExtendedLength);

// If, after truncation, the last character is a dash, remove it
if (aliasingBranchName[branchNameExtendedLength] === '-') {
aliasingBranchName = aliasingBranchName.substring(0, branchNameExtendedLength-1)
}

// Remove the stagingPrefix from the aliasHostname and use the extended aliasingBranchName
aliasHostname = `${projectName}-${aliasingBranchName}.vercel.app`;
}

deployURL = `https://${aliasHostname}`;
vercel = tool(which("vercel", true));
const vercelAliasArgs = [
Expand Down
2 changes: 1 addition & 1 deletion vercel-deployment-task-source/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"version": {
"Major": 1,
"Minor": 2,
"Patch": 3
"Patch": 4
},
"instanceNameFormat": "Deploying $(vercelProject) to Vercel",
"inputs": [
Expand Down
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"manifestVersion": 1,
"id": "vercel-deployment-extension",
"name": "Vercel Deployment Extension",
"version": "1.2.3",
"version": "1.2.4",
"publisher": "Vercel",
"public": true,
"targets": [
Expand Down

0 comments on commit 8a7ddc6

Please sign in to comment.