diff --git a/scripts/lib/github-coordination/actions.js b/scripts/lib/github-coordination/actions.js index 06cd0d383..b9a3104ae 100644 --- a/scripts/lib/github-coordination/actions.js +++ b/scripts/lib/github-coordination/actions.js @@ -84,7 +84,24 @@ function applySync(repo, options = {}, context = {}) { assertValidRepo(repo); const policy = context.policy || loadPolicy(context.rootDir || process.cwd(), options.configPath); const store = context.store || null; - const issues = listIssues(repo, { ...options, state: options.state || 'all', limit: options.limit || 100 }); + const labelIssues = listIssues(repo, { + ...options, + state: options.state || 'all', + limit: options.limit || 100, + label: policy.labels && policy.labels.epic, + }); + const marker = policy.sectionMarker || 'ecc-coordination'; + const coordinatedIssues = listIssues(repo, { + ...options, + state: options.state || 'all', + limit: options.limit || 100, + search: `in:body "${marker}:start"`, + }); + const issues = Array.from( + new Map( + [...labelIssues, ...coordinatedIssues].map(issue => [String(issue.number), issue]) + ).values() + ); const syncedAt = new Date().toISOString(); const results = []; diff --git a/scripts/lib/github-coordination/gh-api.js b/scripts/lib/github-coordination/gh-api.js index d7669cdb7..dd2cb4397 100644 --- a/scripts/lib/github-coordination/gh-api.js +++ b/scripts/lib/github-coordination/gh-api.js @@ -102,7 +102,7 @@ function listIssues(repo, options = {}) { const { owner, name } = normalizeRepo(repo); const limit = Number.isFinite(options.limit) ? options.limit : 100; const state = options.state || 'all'; - return runGhJson([ + const args = [ 'issue', 'list', '--repo', @@ -111,9 +111,12 @@ function listIssues(repo, options = {}) { state, '--limit', String(limit), + ...(options.label ? ['--label', options.label] : []), + ...(options.search ? ['--search', options.search] : []), '--json', 'number,title,body,url,state,labels,author,updatedAt,assignees', - ], options) || []; + ]; + return runGhJson(args, options) || []; } function editIssue(repo, issueNumber, options = {}) { diff --git a/scripts/lib/github-coordination/policy.js b/scripts/lib/github-coordination/policy.js index dc0b0d549..6d93e88b2 100644 --- a/scripts/lib/github-coordination/policy.js +++ b/scripts/lib/github-coordination/policy.js @@ -74,10 +74,14 @@ function loadPolicy(rootDir = process.cwd(), configPath = null) { const branchModel = typeof parsed.branchModel === 'object' && parsed.branchModel !== null && !Array.isArray(parsed.branchModel) ? parsed.branchModel : {}; const project = typeof parsed.project === 'object' && parsed.project !== null && !Array.isArray(parsed.project) ? parsed.project : {}; const fieldNames = typeof project.fieldNames === 'object' && project.fieldNames !== null && !Array.isArray(project.fieldNames) ? project.fieldNames : {}; + const mergedLabels = { ...DEFAULT_LABELS, ...labels }; + if (typeof mergedLabels.epic !== 'string' || !mergedLabels.epic.trim()) { + throw new Error(`Policy file ${resolvedPath} must define labels.epic as a non-empty string`); + } return { ...DEFAULT_POLICY, ...parsed, - labels: { ...DEFAULT_LABELS, ...labels }, + labels: mergedLabels, review: { ...DEFAULT_POLICY.review, ...review }, validation: { ...DEFAULT_POLICY.validation, ...validation }, branchModel: { ...DEFAULT_POLICY.branchModel, ...branchModel }, diff --git a/tests/lib/github-coordination-policy.test.js b/tests/lib/github-coordination-policy.test.js index 0420789ef..352e537b0 100644 --- a/tests/lib/github-coordination-policy.test.js +++ b/tests/lib/github-coordination-policy.test.js @@ -128,6 +128,13 @@ if (test('merges labels when parsed.labels is a plain object', () => { }); })) passed++; else failed++; +if (test('rejects an empty epic label', () => { + withTempDir(tmpDir => { + writeConfig(tmpDir, { labels: { epic: ' ' } }); + assert.throws(() => loadPolicy(tmpDir), /labels\.epic.*non-empty string/); + }); +})) passed++; else failed++; + if (test('falls back to empty labels when parsed.labels is null', () => { withTempDir(tmpDir => { writeConfig(tmpDir, { labels: null }); diff --git a/tests/scripts/github-coordination.test.js b/tests/scripts/github-coordination.test.js index 158fc7c91..73394a1ef 100644 --- a/tests/scripts/github-coordination.test.js +++ b/tests/scripts/github-coordination.test.js @@ -243,6 +243,87 @@ async function runTests() { passed++; else failed++; + if ( + await test('sync filters the issue list to the configured epic label', async () => { + const rootDir = createTempDir('github-coordination-sync-'); + const dbPath = path.join(rootDir, 'state.db'); + + try { + const epicIssue = { + number: 12, + title: 'Ship GitHub-native coordination', + body: '# Ship GitHub-native coordination', + url: 'https://github.com/affaan-m/ECC/issues/12', + state: 'OPEN', + labels: [{ name: 'epic' }], + author: { login: 'maintainer' }, + updatedAt: '2026-06-01T12:00:00Z' + }; + const shim = writeGhShim(rootDir, { + 'issue list --repo affaan-m/ECC --state all --limit 100 --label epic --json number,title,body,url,state,labels,author,updatedAt,assignees': [epicIssue], + 'issue list --repo affaan-m/ECC --state all --limit 100 --search in:body "ecc-coordination:start" --json number,title,body,url,state,labels,author,updatedAt,assignees': [] + }); + + const result = run(['sync', '--repo', 'affaan-m/ECC', '--db', dbPath, '--dry-run', '--json'], { + cwd: rootDir, + env: { + ECC_GH_SHIM: shim.shimPath, + ECC_GH_SHIM_LOG: shim.logPath + } + }); + assert.strictEqual(result.status, 0, result.stderr); + const payload = parseJson(result.stdout); + assert.strictEqual(payload.count, 1); + assert.strictEqual(payload.items[0].issueNumber, 12); + } finally { + cleanup(rootDir); + } + }) + ) + passed++; + else failed++; + + if ( + await test('sync recovers coordinated issues whose epic label drifted', async () => { + const rootDir = createTempDir('github-coordination-sync-drift-'); + const dbPath = path.join(rootDir, 'state.db'); + + try { + const driftedIssue = { + number: 13, + title: 'Recover label drift', + body: '\n```json\n{}\n```\n', + url: 'https://github.com/affaan-m/ECC/issues/13', + state: 'OPEN', + labels: [{ name: 'coordination:synced' }], + author: { login: 'maintainer' }, + updatedAt: '2026-06-01T12:00:00Z' + }; + const shim = writeGhShim(rootDir, { + 'issue list --repo affaan-m/ECC --state all --limit 100 --label epic --json number,title,body,url,state,labels,author,updatedAt,assignees': [], + 'issue list --repo affaan-m/ECC --state all --limit 100 --search in:body "ecc-coordination:start" --json number,title,body,url,state,labels,author,updatedAt,assignees': [driftedIssue] + }); + + const result = run(['sync', '--repo', 'affaan-m/ECC', '--db', dbPath, '--dry-run', '--json'], { + cwd: rootDir, + env: { + ECC_GH_SHIM: shim.shimPath, + ECC_GH_SHIM_LOG: shim.logPath + } + }); + assert.strictEqual(result.status, 0, result.stderr); + const payload = parseJson(result.stdout); + assert.strictEqual(payload.count, 1); + assert.strictEqual(payload.items[0].issueNumber, 13); + assert.ok(payload.items[0].labels.includes('epic')); + } finally { + cleanup(rootDir); + } + }) + ) + passed++; + else failed++; + process.stdout.write(`\nResults: Passed: ${passed}, Failed: ${failed}\n`); process.exit(failed > 0 ? 1 : 0); }