|
| 1 | +name: Issue Vouch |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened, reopened] |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - main |
| 9 | + paths: |
| 10 | + - .github/VOUCHED.td |
| 11 | + - .github/workflows/issue-vouch.yml |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + issues: write |
| 17 | + |
| 18 | +concurrency: |
| 19 | + group: issue-vouch-${{ github.event.issue.number || 'all' }} |
| 20 | + cancel-in-progress: true |
| 21 | + |
| 22 | +jobs: |
| 23 | + label: |
| 24 | + name: Label issues by author trust |
| 25 | + runs-on: ubuntu-24.04 |
| 26 | + steps: |
| 27 | + - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 28 | + with: |
| 29 | + script: | |
| 30 | + const { owner, repo } = context.repo; |
| 31 | + // Only listed authors get a label; open issues number in the thousands, |
| 32 | + // so unvouched is the default and stays unlabelled. |
| 33 | + const labelByStatus = { vouched: "vouch:trusted", denounced: "vouch:denounced" }; |
| 34 | + const managedLabelNames = new Set(["vouch:trusted", "vouch:unvouched", "vouch:denounced"]); |
| 35 | +
|
| 36 | + const { data: file } = await github.rest.repos.getContent({ |
| 37 | + owner, |
| 38 | + repo, |
| 39 | + path: ".github/VOUCHED.td", |
| 40 | + ref: context.payload.repository.default_branch, |
| 41 | + }); |
| 42 | + const statusByUser = new Map(); |
| 43 | + for (const raw of Buffer.from(file.content, "base64").toString("utf8").split("\n")) { |
| 44 | + const match = /^(-?)github:(\S+)/.exec(raw.trim()); |
| 45 | + if (match) statusByUser.set(match[2].toLowerCase(), match[1] ? "denounced" : "vouched"); |
| 46 | + } |
| 47 | +
|
| 48 | + const issues = |
| 49 | + context.eventName === "issues" |
| 50 | + ? [context.payload.issue] |
| 51 | + : ( |
| 52 | + await github.paginate(github.rest.issues.listForRepo, { |
| 53 | + owner, |
| 54 | + repo, |
| 55 | + state: "open", |
| 56 | + per_page: 100, |
| 57 | + }) |
| 58 | + ).filter((issue) => !issue.pull_request); |
| 59 | +
|
| 60 | + for (const issue of issues) { |
| 61 | + const status = statusByUser.get(issue.user.login.toLowerCase()); |
| 62 | + const next = labelByStatus[status]; |
| 63 | + const current = issue.labels.map((label) => label.name).filter((name) => managedLabelNames.has(name)); |
| 64 | +
|
| 65 | + for (const name of current) { |
| 66 | + if (name === next) continue; |
| 67 | + try { |
| 68 | + await github.rest.issues.removeLabel({ owner, repo, issue_number: issue.number, name }); |
| 69 | + } catch (error) { |
| 70 | + if (error.status !== 404) throw error; |
| 71 | + } |
| 72 | + } |
| 73 | + if (next && !current.includes(next)) { |
| 74 | + await github.rest.issues.addLabels({ owner, repo, issue_number: issue.number, labels: [next] }); |
| 75 | + core.info(`Issue #${issue.number} (${issue.user.login}): ${status} -> ${next}`); |
| 76 | + } |
| 77 | + } |
0 commit comments