import { appendFileSync } from 'node:fs' import { execFileSync } from 'node:child_process' import { parseArgs } from 'node:util' const WEB_PREFIXES = [ 'app/', 'components/', 'lib/', 'hooks/', 'prisma/', 'workers/', 'scripts/', '.github/workflows/', ] const WEB_FILES = new Set([ 'package.json', 'package-lock.json', 'tsconfig.json', 'jest.config.js', 'jest.setup.js', 'Dockerfile', ]) const AGENT_PREFIXES = [ 'apps/detail-agent-pi/', 'evals/xiaoyi-golden/', '.github/workflows/', ] const AGENT_FILES = new Set([ 'package.json', 'scripts/check-kb-contract-drift.mjs', ]) const RUST_PREFIXES = [ 'apps/detail-photocraft/', '.github/workflows/', ] function normalizePath(filePath) { return String(filePath || '').replaceAll('\\', '/').replace(/^\.\//, '') } function matchesAny(file, prefixes, exactFiles) { if (exactFiles.has(file)) return true if (file.startsWith('next.config.') && exactFiles === WEB_FILES) return true return prefixes.some(prefix => file.startsWith(prefix)) } export function classifyChangedFiles(files, eventName = 'pull_request') { if (eventName === 'workflow_dispatch') { return { web: true, agent: true, rust: true, security: true } } const normalized = files.map(normalizePath).filter(Boolean) return { web: normalized.some(file => matchesAny(file, WEB_PREFIXES, WEB_FILES)), agent: normalized.some(file => matchesAny(file, AGENT_PREFIXES, AGENT_FILES)), rust: normalized.some(file => matchesAny(file, RUST_PREFIXES, new Set())), security: true, } } export function getChangedFiles({ baseSha, headSha, cwd = process.cwd() }) { if (!baseSha || !headSha) return [] const output = execFileSync( 'git', ['diff', '--name-only', `${baseSha}...${headSha}`], { cwd, encoding: 'utf8' }, ) return output.split(/\r?\n/u).map(line => line.trim()).filter(Boolean) } function main() { const { values } = parseArgs({ options: { event: { type: 'string', default: process.env.GITHUB_EVENT_NAME || 'pull_request' }, base: { type: 'string', default: '' }, head: { type: 'string', default: 'HEAD' }, 'github-output': { type: 'string', default: process.env.GITHUB_OUTPUT || '' }, }, }) const files = values.event === 'workflow_dispatch' ? [] : getChangedFiles({ baseSha: values.base, headSha: values.head }) const result = classifyChangedFiles(files, values.event) const lines = Object.entries(result).map(([key, val]) => `${key}=${val}`) if (values['github-output']) { appendFileSync(values['github-output'], lines.join('\n') + '\n', 'utf8') } console.log(lines.join('\n')) } if (import.meta.url === `file://${process.argv[1]?.replaceAll('\\', '/')}` || process.argv[1]?.endsWith('detect-pr-changes.mjs')) { main() }