All files / scripts/package-release/lib branch.js

0% Statements 0/32
0% Branches 0/15
0% Functions 0/11
0% Lines 0/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                                                                                                                                                               
import { getCurrentBranch } from './git.js';
import { getConfig } from './config.js';
import { error, warn } from './log.js';
 
function branchMatchesPattern(branch, pattern) {
  const reStr = pattern
    .replace(/\./g, '\\.')
    .replace(/\*\*/g, '{{GLOBSTAR}}')
    .replace(/\*/g, '[^/]*')
    .replace(/\{\{GLOBSTAR\}\}/g, '.*')
    .replace(/\[([^\]]+)\]/g, '[$1]');
  const regex = new RegExp(`^${reStr}$`);
  return regex.test(branch);
}
 
function branchMatchesPatternCI(branch, pattern) {
  const reStr = pattern
    .replace(/\./g, '\\.')
    .replace(/\*\*/g, '{{GLOBSTAR}}')
    .replace(/\*/g, '[^/]*')
    .replace(/\{\{GLOBSTAR\}\}/g, '.*')
    .replace(/\[([^\]]+)\]/g, '[$1]');
  const regex = new RegExp(`^${reStr}$`, 'i');
  return regex.test(branch);
}
 
function branchMatchesAny(branch, patterns) {
  return patterns.some((p) => branchMatchesPattern(branch, p));
}
 
function branchMatchesAnyCaseInsensitive(branch, patterns) {
  return patterns.some((p) => branchMatchesPatternCI(branch, p));
}
 
export function enforceBranchGuardrail(subcommand, opts = {}) {
  if (opts.noBranchCheck) return;
 
  const config = getConfig();
  const allowPatterns = config.branches[subcommand] || [];
  const extraPatterns = opts.allowBranch ? [opts.allowBranch] : [];
  const allPatterns = [...allowPatterns, ...extraPatterns];
 
  const branch = getCurrentBranch();
 
  if (branchMatchesAny(branch, allPatterns)) return;
 
  // Case-insensitive miss: the branch name almost matches a pattern but the
  // case is wrong. This catches typos like `Release/main-patch/06-09-26` that
  // would otherwise escape branch-protection rules and confuse downstream
  // tooling. Fail with a specific hint instead of the generic "not allowed".
  if (branchMatchesAnyCaseInsensitive(branch, allPatterns)) {
    error(
      `Current branch "${branch}" matches a release pattern but the case is wrong.\n` +
      `  Allowed patterns are case-sensitive (matching gitcore branch-protection rules):\n` +
      `    ${allPatterns.join('\n    ')}\n` +
      `  Rename the branch to match the canonical case (e.g. "release/" not "Release/").`,
    );
    process.exit(1);
  }
 
  error(
    `Current branch "${branch}" is not allowed for "package-release ${subcommand}".\n` +
    `  Allowed patterns: ${allPatterns.join(', ')}\n` +
    `  Use --allow-branch <pattern> to add a pattern, or --no-branch-check to bypass.`,
  );
  process.exit(1);
}
 
export function isDevelopBranch() {
  const branch = getCurrentBranch();
  return branch === 'develop';
}
 
export function isPatchBranch() {
  const branch = getCurrentBranch();
  return /^develop-\d{3}-patch$/.test(branch);
}
 
export function isReleaseCycleBranch() {
  return isDevelopBranch() || isPatchBranch();
}
 
export function getPatchBranchReleaseId() {
  const branch = getCurrentBranch();
  const match = branch.match(/^develop-(\d{3})-patch$/);
  return match ? match[1] : null;
}