--- name: "Backfill Language Labels on Open PRs" on: workflow_dispatch: permissions: contents: read pull-requests: write issues: write jobs: backfill: runs-on: ubuntu-latest steps: - name: "Scan open PRs and apply lang:* labels" uses: actions/github-script@v8 with: script: | const owner = context.repo.owner; const repo = context.repo.repo; const LanguageFileRegex = /(?
sentences|responses|tests)\/(?[a-z]{2}(-[a-zA-Z]{2,4})?)\/(?.+)\.yaml/; async function ensureLabelExists(label) { try { await github.rest.issues.getLabel({ owner, repo, name: label }); } catch (e) { if (e.status === 404) { core.info(`Creating label: ${label}`); await github.rest.issues.createLabel({ owner, repo, name: label, color: 'ededed', description: 'PR touches files for this language' }); } else { throw e; } } } // Get all open PRs const openPRs = []; for await (const res of github.paginate.iterator( github.rest.pulls.list, { owner, repo, state: 'open', per_page: 100 } )) { openPRs.push(...res.data); } let changed = 0; for (const pr of openPRs) { const prNumber = pr.number; // List changed files const files = []; for await (const resp of github.paginate.iterator( github.rest.pulls.listFiles, { owner, repo, pull_number: prNumber, per_page: 100 } )) { for (const f of resp.data) files.push(f.filename); } // Extract languages per your regex const langs = new Set(); for (const path of files) { const m = LanguageFileRegex.exec(path); if (m?.groups?.language_code) langs.add(m.groups.language_code.toLowerCase()); } const desired = new Set([...langs].map(code => `lang: ${code}`)); // Current labels const { data: currentLabelsData } = await github.rest.issues.listLabelsOnIssue({ owner, repo, issue_number: prNumber, per_page: 100 }); const current = new Set(currentLabelsData.map(l => l.name)); // 🚫 Skip this PR if it already has *any* lang:* label if ([...current].some(l => l.startsWith('lang:'))) { core.info(`Skipping PR #${prNumber} (already has lang label)`); continue; } // Add const toAdd = [...desired].filter(l => !current.has(l)); for (const label of toAdd) { await ensureLabelExists(label); } if (toAdd.length) { core.info(`PR #${prNumber}: adding ${toAdd.join(', ')}`); await github.rest.issues.addLabels({ owner, repo, issue_number: prNumber, labels: toAdd }); } if (toAdd.length) changed++; } core.summary .addHeading('Backfill complete') .addList([ `Processed open PRs: ${openPRs.length}`, `PRs changed: ${changed}` ]) .write();