mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
Use the changed paths to keep native jobs enabled for changes outside the web host allowlist.
322 lines
8.7 KiB
JavaScript
322 lines
8.7 KiB
JavaScript
#!/usr/bin/env node
|
|
// Zero-dependency classifier for pure web-host diffs.
|
|
//
|
|
// Determines whether a Git diff touches only the frozen allowlist of pure
|
|
// frontend web-host static files and browser/unit tests. Any unknown, empty,
|
|
// trunk, forced, or unreachable-base input conservatively yields `false`.
|
|
|
|
import { execFileSync } from 'node:child_process';
|
|
import { appendFileSync } from 'node:fs';
|
|
import { pathToFileURL } from 'node:url';
|
|
|
|
export const ZERO_SHA = '0000000000000000000000000000000000000000';
|
|
|
|
export const TRUNK_REFS = Object.freeze(
|
|
new Set([
|
|
'darksky/np/mobile',
|
|
'canary',
|
|
'darksky/native-playground',
|
|
'refs/heads/darksky/np/mobile',
|
|
'refs/heads/canary',
|
|
'refs/heads/darksky/native-playground',
|
|
])
|
|
);
|
|
|
|
export const WEB_HOST_ONLY_ALLOWLIST = Object.freeze(
|
|
new Set([
|
|
'packages/frontend/hosts/web/src/idb-storage.js',
|
|
'packages/frontend/hosts/web/desktop/start.js',
|
|
'packages/frontend/hosts/web/desktop/index.html',
|
|
'packages/frontend/hosts/web/tests/idb-storage.test.mjs',
|
|
'packages/frontend/hosts/web/tests/desktop.test.mjs',
|
|
'packages/frontend/hosts/web/tests/dual-shell-save.browser.mjs',
|
|
])
|
|
);
|
|
|
|
export function isTrunkRef(ref) {
|
|
return typeof ref === 'string' && TRUNK_REFS.has(ref.trim());
|
|
}
|
|
|
|
export function isValidBaseSha(baseSha) {
|
|
if (typeof baseSha !== 'string') return false;
|
|
const trimmed = baseSha.trim();
|
|
return /^[0-9a-f]{7,64}$/i.test(trimmed) && !/^0+$/.test(trimmed);
|
|
}
|
|
|
|
function parseNulTokens(raw) {
|
|
if (raw === '') return { valid: true, entries: [] };
|
|
const tokens = raw.split('\0');
|
|
if (tokens[tokens.length - 1] === '') {
|
|
tokens.pop();
|
|
}
|
|
const entries = [];
|
|
let i = 0;
|
|
while (i < tokens.length) {
|
|
const status = tokens[i++];
|
|
if (/^[RC]\d*$/.test(status)) {
|
|
const oldPath = tokens[i++];
|
|
const newPath = tokens[i++];
|
|
if (!oldPath || !newPath) {
|
|
return { valid: false, entries: [] };
|
|
}
|
|
entries.push({ status, paths: [oldPath, newPath] });
|
|
} else if (/^[AMDT]$/.test(status)) {
|
|
const filePath = tokens[i++];
|
|
if (!filePath) {
|
|
return { valid: false, entries: [] };
|
|
}
|
|
entries.push({ status, paths: [filePath] });
|
|
} else {
|
|
return { valid: false, entries: [] };
|
|
}
|
|
}
|
|
return { valid: true, entries };
|
|
}
|
|
|
|
function parseTabLines(lines) {
|
|
const entries = [];
|
|
for (const rawLine of lines) {
|
|
if (typeof rawLine !== 'string') {
|
|
return { valid: false, entries: [] };
|
|
}
|
|
if (rawLine.length === 0) continue;
|
|
const parts = rawLine.split('\t');
|
|
const status = parts[0];
|
|
if (/^[RC]\d*$/.test(status)) {
|
|
if (parts.length !== 3 || !parts[1] || !parts[2]) {
|
|
return { valid: false, entries: [] };
|
|
}
|
|
entries.push({ status, paths: [parts[1], parts[2]] });
|
|
} else if (/^[AMDT]$/.test(status)) {
|
|
if (parts.length !== 2 || !parts[1]) {
|
|
return { valid: false, entries: [] };
|
|
}
|
|
entries.push({ status, paths: [parts[1]] });
|
|
} else {
|
|
return { valid: false, entries: [] };
|
|
}
|
|
}
|
|
return { valid: true, entries };
|
|
}
|
|
|
|
export function parseNameStatus(input) {
|
|
if (Buffer.isBuffer(input)) {
|
|
return parseNulTokens(input.toString('utf8'));
|
|
}
|
|
if (typeof input === 'string') {
|
|
if (input.includes('\0')) {
|
|
return parseNulTokens(input);
|
|
}
|
|
return parseTabLines(input.split(/\r?\n/));
|
|
}
|
|
if (Array.isArray(input)) {
|
|
return parseTabLines(input);
|
|
}
|
|
return { valid: false, entries: [] };
|
|
}
|
|
|
|
export function classifyWebHostOnly(options = {}) {
|
|
const {
|
|
nameStatus,
|
|
nameStatusLines,
|
|
forceAll = false,
|
|
baseSha,
|
|
ref = '',
|
|
} = options;
|
|
|
|
if (forceAll === true || forceAll === 'true') {
|
|
return { webHostOnly: false, reason: 'force_all', checkedPaths: [] };
|
|
}
|
|
if (isTrunkRef(ref)) {
|
|
return { webHostOnly: false, reason: 'trunk_ref', checkedPaths: [] };
|
|
}
|
|
if (baseSha !== undefined && !isValidBaseSha(baseSha)) {
|
|
return { webHostOnly: false, reason: 'invalid_base', checkedPaths: [] };
|
|
}
|
|
|
|
const rawInput = nameStatus !== undefined ? nameStatus : nameStatusLines;
|
|
if (rawInput === undefined || rawInput === null) {
|
|
return { webHostOnly: false, reason: 'missing_diff', checkedPaths: [] };
|
|
}
|
|
|
|
const parsed = parseNameStatus(rawInput);
|
|
if (!parsed.valid) {
|
|
return {
|
|
webHostOnly: false,
|
|
reason: 'invalid_name_status',
|
|
checkedPaths: [],
|
|
};
|
|
}
|
|
if (parsed.entries.length === 0) {
|
|
return { webHostOnly: false, reason: 'empty_diff', checkedPaths: [] };
|
|
}
|
|
|
|
const checkedPaths = parsed.entries.flatMap(entry => entry.paths);
|
|
const allAllowed = checkedPaths.every(filePath =>
|
|
WEB_HOST_ONLY_ALLOWLIST.has(filePath)
|
|
);
|
|
return {
|
|
webHostOnly: allAllowed,
|
|
reason: allAllowed ? 'allowlist_only' : 'non_allowlist_path',
|
|
checkedPaths,
|
|
};
|
|
}
|
|
|
|
function runGitDiff(cwd, args) {
|
|
return execFileSync('git', ['diff', '--name-status', '-z', ...args], {
|
|
cwd,
|
|
encoding: 'buffer',
|
|
maxBuffer: 64 * 1024 * 1024,
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
});
|
|
}
|
|
|
|
export function classifyGitDiff({
|
|
baseSha,
|
|
head = 'HEAD',
|
|
forceAll = false,
|
|
ref = '',
|
|
cwd = process.cwd(),
|
|
} = {}) {
|
|
if (forceAll === true || forceAll === 'true') {
|
|
return { webHostOnly: false, reason: 'force_all', checkedPaths: [] };
|
|
}
|
|
if (isTrunkRef(ref)) {
|
|
return { webHostOnly: false, reason: 'trunk_ref', checkedPaths: [] };
|
|
}
|
|
if (!isValidBaseSha(baseSha)) {
|
|
return { webHostOnly: false, reason: 'invalid_base', checkedPaths: [] };
|
|
}
|
|
|
|
const trimmedBase = baseSha.trim();
|
|
const gitOpts = {
|
|
cwd,
|
|
encoding: 'buffer',
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
};
|
|
|
|
try {
|
|
execFileSync(
|
|
'git',
|
|
['rev-parse', '--verify', '--quiet', `${trimmedBase}^{commit}`],
|
|
gitOpts
|
|
);
|
|
execFileSync(
|
|
'git',
|
|
['rev-parse', '--verify', '--quiet', `${head}^{commit}`],
|
|
gitOpts
|
|
);
|
|
execFileSync('git', ['merge-base', trimmedBase, head], gitOpts);
|
|
} catch {
|
|
return {
|
|
webHostOnly: false,
|
|
reason: 'unreachable_base',
|
|
checkedPaths: [],
|
|
};
|
|
}
|
|
|
|
const diffRange = `${trimmedBase}...${head}`;
|
|
let diffBuf;
|
|
try {
|
|
diffBuf = runGitDiff(cwd, [diffRange]);
|
|
} catch {
|
|
return { webHostOnly: false, reason: 'git_diff_error', checkedPaths: [] };
|
|
}
|
|
|
|
const initial = classifyWebHostOnly({
|
|
nameStatus: diffBuf,
|
|
forceAll,
|
|
baseSha: trimmedBase,
|
|
ref,
|
|
});
|
|
if (!initial.webHostOnly) {
|
|
return initial;
|
|
}
|
|
|
|
const parsed = parseNameStatus(diffBuf);
|
|
if (!parsed.entries.some(entry => entry.status === 'A')) {
|
|
return initial;
|
|
}
|
|
|
|
// `git diff --name-status` does not detect copies from unmodified files
|
|
// without `-C --find-copies-harder`. Re-check only when a candidate
|
|
// web-host-only diff adds a new file so copying from outside the allowlist
|
|
// cannot sneak in as `A <allowlisted>`.
|
|
let copyDiffBuf;
|
|
try {
|
|
copyDiffBuf = runGitDiff(cwd, ['-C', '--find-copies-harder', diffRange]);
|
|
} catch {
|
|
return { webHostOnly: false, reason: 'git_diff_error', checkedPaths: [] };
|
|
}
|
|
|
|
return classifyWebHostOnly({
|
|
nameStatus: copyDiffBuf,
|
|
forceAll,
|
|
baseSha: trimmedBase,
|
|
ref,
|
|
});
|
|
}
|
|
|
|
function parseCliArgs(argv) {
|
|
const args = {
|
|
baseSha: process.env.BASE ?? '',
|
|
head: 'HEAD',
|
|
forceAll: process.env.FORCE_ALL ?? 'false',
|
|
ref: process.env.GITHUB_REF ?? process.env.GITHUB_REF_NAME ?? '',
|
|
githubOutput: process.env.GITHUB_OUTPUT ?? '',
|
|
cwd: process.cwd(),
|
|
};
|
|
|
|
for (let i = 0; i < argv.length; i++) {
|
|
const flag = argv[i];
|
|
const value = argv[i + 1];
|
|
if (flag === '--base' && value !== undefined) {
|
|
args.baseSha = value;
|
|
i++;
|
|
} else if (flag === '--head' && value !== undefined) {
|
|
args.head = value;
|
|
i++;
|
|
} else if (flag === '--force-all' && value !== undefined) {
|
|
args.forceAll = value;
|
|
i++;
|
|
} else if (flag === '--ref' && value !== undefined) {
|
|
args.ref = value;
|
|
i++;
|
|
} else if (flag === '--github-output' && value !== undefined) {
|
|
args.githubOutput = value;
|
|
i++;
|
|
} else if (flag === '--cwd' && value !== undefined) {
|
|
args.cwd = value;
|
|
i++;
|
|
} else {
|
|
throw new Error(`Unknown or incomplete argument: ${flag}`);
|
|
}
|
|
}
|
|
return args;
|
|
}
|
|
|
|
function main() {
|
|
const args = parseCliArgs(process.argv.slice(2));
|
|
const result = classifyGitDiff({
|
|
baseSha: args.baseSha,
|
|
head: args.head,
|
|
forceAll: args.forceAll,
|
|
ref: args.ref,
|
|
cwd: args.cwd,
|
|
});
|
|
|
|
if (args.githubOutput) {
|
|
appendFileSync(args.githubOutput, `web_host_only=${result.webHostOnly}\n`);
|
|
}
|
|
const summary =
|
|
`web_host_only=${result.webHostOnly} ` +
|
|
`(${result.reason}, ${result.checkedPaths.length} path(s))`;
|
|
console.log(summary);
|
|
}
|
|
|
|
const isMain =
|
|
process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;
|
|
if (isMain) {
|
|
main();
|
|
}
|