Skip to content
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
2 changes: 0 additions & 2 deletions actions/setup/js/normalize_branch_name.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
/**
* Normalizes a branch name to be a valid git branch name.
*
* IMPORTANT: Keep this function in sync with the normalizeBranchName function in upload_assets.cjs
*
* Valid characters: alphanumeric (a-z, A-Z, 0-9), dash (-), underscore (_), forward slash (/), dot (.)
* Max length: 128 characters (before salt is appended)
*
Expand Down
45 changes: 1 addition & 44 deletions actions/setup/js/upload_assets.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,12 @@ const crypto = require("crypto");
const { loadAgentOutput } = require("./load_agent_output.cjs");
const { getErrorMessage } = require("./error_helpers.cjs");
const { ERR_API, ERR_CONFIG, ERR_SYSTEM, ERR_VALIDATION } = require("./error_codes.cjs");
const { normalizeBranchName } = require("./normalize_branch_name.cjs");

/**
* @typedef {{ type: string, fileName: string, sha: string, size: number, targetFileName: string, url?: string }} UploadAssetItem
*/

/**
* Normalizes a branch name to be a valid git branch name.
*
* IMPORTANT: Keep this function in sync with the normalizeBranchName function in normalize_branch_name.cjs
*
* Valid characters: alphanumeric (a-z, A-Z, 0-9), dash (-), underscore (_), forward slash (/), dot (.)
* Max length: 128 characters
*
* The normalization process:
* 1. Replaces invalid characters with a single dash
* 2. Collapses multiple consecutive dashes to a single dash
* 3. Removes leading and trailing dashes
* 4. Truncates to 128 characters
* 5. Removes trailing dashes after truncation
*
* @param {string} branchName - The branch name to normalize
* @returns {string} The normalized branch name
*/
function normalizeBranchName(branchName) {
if (!branchName || typeof branchName !== "string" || branchName.trim() === "") {
return branchName;
}

// Replace any sequence of invalid characters with a single dash
// Valid characters are: a-z, A-Z, 0-9, -, _, /, .
let normalized = branchName.replace(/[^a-zA-Z0-9\-_/.]+/g, "-");

// Collapse multiple consecutive dashes to a single dash
normalized = normalized.replace(/-+/g, "-");

// Remove leading and trailing dashes
normalized = normalized.replace(/^-+|-+$/g, "");

// Truncate to max 128 characters
if (normalized.length > 128) {
normalized = normalized.substring(0, 128);
}

// Ensure it doesn't end with a dash after truncation
normalized = normalized.replace(/-+$/, "");

return normalized;
}

async function main() {
// Check if we're in staged mode
const isStaged = process.env.GH_AW_SAFE_OUTPUTS_STAGED === "true";
Expand Down
Loading