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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 24x 24x 1x 23x 23x 23x 11x 9x 9x 2x 13x 13x 12x 1x 5x 5x 1x 4x 1x 3x 3x 1x 2x 14x 14x 1x 13x 13x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 2x 1x 1x 1x | import { execa } from 'execa';
import { DEFAULT_SLDS_SCS_GH_HOST, DEFAULT_SLDS_SCS_REPO } from '../config.js';
import type { Logger } from '../utils/logger.js';
export type SldsScsPrMeta = {
prNumber: number;
title: string;
baseRefName: string;
headRefName: string;
url: string;
newVersion: string;
workItemId: string;
releaseId: string;
targetBranch: string;
gitRemote: string;
};
export interface IPrMetadataService {
resolveSldsScsPr(pr: string): Promise<SldsScsPrMeta>;
}
function parseCoreBase(baseRefName: string): { releaseId: string; line: string; targetBranch: string } {
const match = baseRefName.match(/^core-(\d+)-(patch|main)$/);
if (!match) {
throw new Error(`Could not parse release line from baseRefName: ${baseRefName}`);
}
const releaseId = match[1];
const line = match[2];
return { releaseId, line, targetBranch: `p4/${releaseId}-${line}` };
}
// core-264-patch always maps to p4/264-patch (same release id and line).
export function p4BranchForCoreBase(baseRefName: string): string {
return parseCoreBase(baseRefName).targetBranch;
}
export function assertCoreTargetMatchesPrBase(baseRefName: string, targetBranch: string): void {
const expected = p4BranchForCoreBase(baseRefName);
if (targetBranch !== expected) {
throw new Error(
`Gitcore target ${targetBranch} does not match slds-scs PR base ${baseRefName}; expected ${expected}`,
);
}
}
function parsePrRef(pr: string): number {
const urlMatch = pr.match(/\/pull\/(\d+)/);
if (urlMatch) return Number(urlMatch[1]);
if (/^\d+$/.test(pr.trim())) return Number(pr.trim());
throw new Error(`Could not parse slds-scs PR from: ${pr}`);
}
// Source of truth for the slds-scs version is package.json on the PR head, not the branch name.
function parsePackageVersion(content: string): string {
let parsed: unknown;
try {
parsed = JSON.parse(content);
} catch {
throw new Error('Could not parse package.json from slds-scs PR');
}
if (typeof parsed !== 'object' || parsed === null || !('version' in parsed)) {
throw new Error(`Could not parse version from package.json: ${JSON.stringify(parsed)}`);
}
const { version } = parsed;
if (typeof version !== 'string' || !/^\d+\.\d+\.\d+$/.test(version)) {
throw new Error(`Could not parse version from package.json: ${JSON.stringify(version)}`);
}
return version;
}
function parseMeta(
prNumber: number,
json: {
title: string;
baseRefName: string;
headRefName: string;
url: string;
},
newVersion: string,
): SldsScsPrMeta {
const wiMatch = json.title.match(/@(W-\d+)/i);
if (!wiMatch) {
throw new Error(`Could not parse GUS work item from title: ${json.title}`);
}
const { releaseId, targetBranch } = parseCoreBase(json.baseRefName);
return {
prNumber,
title: json.title,
baseRefName: json.baseRefName,
headRefName: json.headRefName,
url: json.url,
newVersion,
workItemId: wiMatch[1].toUpperCase(),
releaseId,
targetBranch,
gitRemote: releaseId,
};
}
// Read / write slds-scs PRs via gh (host and repo from config).
export class PrMetadataService implements IPrMetadataService {
constructor(
private readonly logger: Logger,
private readonly sldsScsRepo = DEFAULT_SLDS_SCS_REPO,
private readonly sldsScsGhHost = DEFAULT_SLDS_SCS_GH_HOST,
) {}
private async ghApi(path: string): Promise<string> {
const { stdout } = await execa('gh', ['api', path], {
env: { ...process.env, GH_HOST: this.sldsScsGhHost },
});
return stdout;
}
// Read a file from the slds-scs repo at a commit SHA (the PR head).
private async getFile(filePath: string, ref: string): Promise<string> {
const encodedPath = filePath
.split('/')
.map((segment) => encodeURIComponent(segment))
.join('/');
const raw = await this.ghApi(
`repos/${this.sldsScsRepo}/contents/${encodedPath}?ref=${encodeURIComponent(ref)}`,
);
const json = JSON.parse(raw) as { content?: string; encoding?: string };
Iif (!json.content) {
throw new Error(`Unexpected contents response for ${this.sldsScsRepo}:${filePath}@${ref}`);
}
return json.encoding === 'base64'
? Buffer.from(json.content.replace(/\n/g, ''), 'base64').toString('utf8')
: json.content;
}
async resolveSldsScsPr(pr: string): Promise<SldsScsPrMeta> {
const prNumber = parsePrRef(pr);
this.logger.step(`Resolve ${this.sldsScsRepo}#${prNumber}`);
const { stdout } = await execa(
'gh',
[
'pr',
'view',
String(prNumber),
'--repo',
this.sldsScsRepo,
'--json',
'title,baseRefName,headRefName,headRefOid,url',
],
{ env: { ...process.env, GH_HOST: this.sldsScsGhHost } },
);
const json = JSON.parse(stdout) as {
title: string;
baseRefName: string;
headRefName: string;
headRefOid?: string;
url: string;
};
if (!json.headRefOid) {
throw new Error(`Could not resolve head SHA for ${this.sldsScsRepo}#${prNumber}`);
}
const packageJson = await this.getFile('package.json', json.headRefOid);
const newVersion = parsePackageVersion(packageJson);
return parseMeta(prNumber, json, newVersion);
}
}
export { parsePrRef, parseMeta, parsePackageVersion };
|