All files / packages/sds-release-manager/src/utils pin-files.ts

100% Statements 49/49
100% Branches 20/20
100% Functions 8/8
100% Lines 45/45

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109  5x 5x                                     5x 5x 1x   4x         6x 6x 6x 6x 6x 1x   5x       3x 3x 3x         3x 3x 3x         2x 2x 2x 2x 2x 2x   2x 2x 2x 2x         9x 9x 1x   8x                   6x                     12x 12x 12x 12x 12x 54x 54x 54x 19x 19x   12x    
// Paths inside the gitcore repo (repo root has a `core/` directory).
export const GITCORE_PIN_VERSION_PATH = 'core/third_party/dependencies/org_sfs.bzl';
export const GITCORE_PIN_CATALOG_PATH = 'core/third_party/dependencies/pinned_catalog/org_sfs.bzl';
 
export type PinBlock = {
  version: string;
  sha256: string;
  sha1: string;
};
 
export type ApplyPinUpdateInput = {
  oldVersion: string;
  newVersion: string;
  oldSha256: string;
  newSha256: string;
  oldSha1: string;
  newSha1: string;
};
 
// Parse `_SLDS_SCS_VERSION` from org_sfs.bzl content.
export function parsePinVersion(content: string): string {
  const match = content.match(/_SLDS_SCS_VERSION\s*=\s*"([^"]+)"/);
  if (!match) {
    throw new Error(`Could not parse _SLDS_SCS_VERSION from pin file content`);
  }
  return match[1];
}
 
// Parse artifact_sha256 / artifact_sha1 from the slds-scs catalog block.
export function parsePinShas(catalogContent: string): Pick<PinBlock, 'sha256' | 'sha1'> {
  const blockMatch = catalogContent.match(/name\s*=\s*"org_sfs_slds_scs"[\s\S]{0,800}/);
  const block = blockMatch?.[0] ?? catalogContent;
  const sha256Match = block.match(/artifact_sha256\s*=\s*"([^"]+)"/);
  const sha1Match = block.match(/artifact_sha1\s*=\s*"([^"]+)"/);
  if (!sha256Match || !sha1Match) {
    throw new Error(`Could not parse slds-scs SHAs from pinned catalog content`);
  }
  return { sha256: sha256Match[1], sha1: sha1Match[1] };
}
 
export function parsePinBlock(versionFileContent: string, catalogContent: string): PinBlock {
  const version = parsePinVersion(versionFileContent);
  const shas = parsePinShas(catalogContent);
  return { version, ...shas };
}
 
// Replace version constant in org_sfs.bzl.
export function applyPinToVersionFile(content: string, input: ApplyPinUpdateInput): string {
  const before = `_SLDS_SCS_VERSION = "${input.oldVersion}"`;
  const after = `_SLDS_SCS_VERSION = "${input.newVersion}"`;
  return replaceOrThrow(content, before, after);
}
 
// Replace artifact + SHAs for slds-scs in pinned_catalog/org_sfs.bzl.
export function applyPinToCatalogFile(content: string, input: ApplyPinUpdateInput): string {
  const oldArtifact = `artifact = "org.sfs:slds-scs:jar:${input.oldVersion}"`;
  const newArtifact = `artifact = "org.sfs:slds-scs:jar:${input.newVersion}"`;
  const oldSha256 = `artifact_sha256 = "${input.oldSha256}"`;
  const newSha256 = `artifact_sha256 = "${input.newSha256}"`;
  const oldSha1 = `artifact_sha1 = "${input.oldSha1}"`;
  const newSha1 = `artifact_sha1 = "${input.newSha1}"`;
 
  let updated = replaceOrThrow(content, oldArtifact, newArtifact);
  updated = replaceOrThrow(updated, oldSha256, newSha256);
  updated = replaceOrThrow(updated, oldSha1, newSha1);
  return updated;
}
 
// Replace `before` with `after`. Throws when the search string is missing.
function replaceOrThrow(content: string, before: string, after: string): string {
  const updated = content.replace(before, after);
  if (updated === content) {
    throw new Error(`Pin string not found for replace: ${before}`);
  }
  return updated;
}
 
// Simple unified-style preview for the two pin files (not a real git diff).
export function previewPinDiff(
  versionOld: string,
  versionNew: string,
  catalogOld: string,
  catalogNew: string,
): string {
  return [
    `--- ${GITCORE_PIN_VERSION_PATH}`,
    `+++ ${GITCORE_PIN_VERSION_PATH}`,
    ...diffChangedLines(versionOld, versionNew),
    `--- ${GITCORE_PIN_CATALOG_PATH}`,
    `+++ ${GITCORE_PIN_CATALOG_PATH}`,
    ...diffChangedLines(catalogOld, catalogNew),
  ].join('\n');
}
 
function diffChangedLines(oldContent: string, newContent: string): string[] {
  const oldLines = oldContent.split('\n');
  const newLines = newContent.split('\n');
  const out: string[] = [];
  const len = Math.max(oldLines.length, newLines.length);
  for (let i = 0; i < len; i++) {
    const a = oldLines[i] ?? '';
    const b = newLines[i] ?? '';
    if (a === b) continue;
    if (a) out.push(`-${a}`);
    if (b) out.push(`+${b}`);
  }
  return out;
}