| ... | ... | @@ -0,0 +1,175 @@ |
| 1 | name: PR Vouch |
| 2 | |
| 3 | on: |
| 4 | pull_request_target: |
| 5 | types: [opened, reopened, synchronize, ready_for_review] |
| 6 | issue_comment: |
| 7 | types: [created] |
| 8 | push: |
| 9 | branches: |
| 10 | - main |
| 11 | paths: |
| 12 | - .github/VOUCHED.td |
| 13 | - .github/workflows/pr-vouch.yml |
| 14 | |
| 15 | permissions: |
| 16 | contents: read |
| 17 | issues: write |
| 18 | pull-requests: write |
| 19 | |
| 20 | jobs: |
| 21 | collect-targets: |
| 22 | name: Collect PR targets |
| 23 | runs-on: ubuntu-24.04 |
| 24 | outputs: |
| 25 | targets: ${{ steps.collect.outputs.targets }} |
| 26 | steps: |
| 27 | - id: collect |
| 28 | uses: actions/github-script@v7 |
| 29 | with: |
| 30 | script: | |
| 31 | if (context.eventName === "pull_request_target") { |
| 32 | const pr = context.payload.pull_request; |
| 33 | core.setOutput("targets", JSON.stringify([{ number: pr.number, user: pr.user.login }])); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | if (context.eventName === "issue_comment") { |
| 38 | const issue = context.payload.issue; |
| 39 | const body = context.payload.comment?.body ?? ""; |
| 40 | if (!issue?.pull_request || !body.includes("/recheck-vouch")) { |
| 41 | core.setOutput("targets", "[]"); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | core.setOutput( |
| 46 | "targets", |
| 47 | JSON.stringify([{ number: issue.number, user: issue.user.login }]), |
| 48 | ); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | const pulls = await github.paginate(github.rest.pulls.list, { |
| 53 | owner: context.repo.owner, |
| 54 | repo: context.repo.repo, |
| 55 | state: "open", |
| 56 | per_page: 100, |
| 57 | }); |
| 58 | |
| 59 | const targets = pulls.map((pull) => ({ |
| 60 | number: pull.number, |
| 61 | user: pull.user.login, |
| 62 | })); |
| 63 | core.setOutput("targets", JSON.stringify(targets)); |
| 64 | |
| 65 | label: |
| 66 | name: Label PR ${{ matrix.target.number }} |
| 67 | needs: collect-targets |
| 68 | if: ${{ needs.collect-targets.outputs.targets != '[]' }} |
| 69 | runs-on: ubuntu-24.04 |
| 70 | concurrency: |
| 71 | group: pr-vouch-${{ matrix.target.number }} |
| 72 | cancel-in-progress: true |
| 73 | strategy: |
| 74 | fail-fast: false |
| 75 | matrix: |
| 76 | target: ${{ fromJson(needs.collect-targets.outputs.targets) }} |
| 77 | steps: |
| 78 | - id: vouch |
| 79 | name: Check PR author trust |
| 80 | uses: mitchellh/vouch/action/check-user@v1 |
| 81 | with: |
| 82 | user: ${{ matrix.target.user }} |
| 83 | allow-fail: true |
| 84 | env: |
| 85 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 86 | |
| 87 | - name: Sync PR labels |
| 88 | uses: actions/github-script@v7 |
| 89 | env: |
| 90 | PR_NUMBER: ${{ matrix.target.number }} |
| 91 | VOUCH_STATUS: ${{ steps.vouch.outputs.status }} |
| 92 | with: |
| 93 | script: | |
| 94 | const issueNumber = Number(process.env.PR_NUMBER); |
| 95 | const status = process.env.VOUCH_STATUS; |
| 96 | const managedLabels = [ |
| 97 | { |
| 98 | name: "vouch:trusted", |
| 99 | color: "1f883d", |
| 100 | description: "PR author is trusted by repo permissions or the VOUCHED list.", |
| 101 | }, |
| 102 | { |
| 103 | name: "vouch:unvouched", |
| 104 | color: "fbca04", |
| 105 | description: "PR author is not yet trusted in the VOUCHED list.", |
| 106 | }, |
| 107 | { |
| 108 | name: "vouch:denounced", |
| 109 | color: "d1242f", |
| 110 | description: "PR author is explicitly blocked by the VOUCHED list.", |
| 111 | }, |
| 112 | ]; |
| 113 | |
| 114 | const managedLabelNames = managedLabels.map((label) => label.name); |
| 115 | |
| 116 | for (const label of managedLabels) { |
| 117 | try { |
| 118 | const { data: existing } = await github.rest.issues.getLabel({ |
| 119 | owner: context.repo.owner, |
| 120 | repo: context.repo.repo, |
| 121 | name: label.name, |
| 122 | }); |
| 123 | |
| 124 | if ( |
| 125 | existing.color !== label.color || |
| 126 | (existing.description ?? "") !== label.description |
| 127 | ) { |
| 128 | await github.rest.issues.updateLabel({ |
| 129 | owner: context.repo.owner, |
| 130 | repo: context.repo.repo, |
| 131 | name: label.name, |
| 132 | color: label.color, |
| 133 | description: label.description, |
| 134 | }); |
| 135 | } |
| 136 | } catch (error) { |
| 137 | if (error.status !== 404) { |
| 138 | throw error; |
| 139 | } |
| 140 | |
| 141 | await github.rest.issues.createLabel({ |
| 142 | owner: context.repo.owner, |
| 143 | repo: context.repo.repo, |
| 144 | name: label.name, |
| 145 | color: label.color, |
| 146 | description: label.description, |
| 147 | }); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | const nextLabelName = |
| 152 | status === "denounced" |
| 153 | ? "vouch:denounced" |
| 154 | : ["bot", "collaborator", "vouched"].includes(status) |
| 155 | ? "vouch:trusted" |
| 156 | : "vouch:unvouched"; |
| 157 | |
| 158 | const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ |
| 159 | owner: context.repo.owner, |
| 160 | repo: context.repo.repo, |
| 161 | issue_number: issueNumber, |
| 162 | }); |
| 163 | |
| 164 | const preservedLabels = currentLabels |
| 165 | .map((label) => label.name) |
| 166 | .filter((name) => !managedLabelNames.includes(name)); |
| 167 | |
| 168 | await github.rest.issues.setLabels({ |
| 169 | owner: context.repo.owner, |
| 170 | repo: context.repo.repo, |
| 171 | issue_number: issueNumber, |
| 172 | labels: [...preservedLabels, nextLabelName], |
| 173 | }); |
| 174 | |
| 175 | core.info(`PR #${issueNumber}: ${status} -> ${nextLabelName}`); |