Open History to see the last commit
1name: PR Vouch
2
3on:
4 pull_request_target:
5 types: [opened, reopened, synchronize, ready_for_review, converted_to_draft]
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
15permissions:
16 contents: read
17
18# Per-PR events supersede each other at the run level, in arrival order (a job-level group
19# is joined when the job starts, so an older run's slower job could cancel the newer one).
20# Other comments and pushes get a unique group so they never cancel a PR's run.
21concurrency:
22 group: >-
23 ${{ (github.event_name == 'pull_request_target' ||
24 (github.event_name == 'issue_comment' && contains(github.event.comment.body, '/recheck-vouch')))
25 && format('pr-vouch-run-{0}', github.event.pull_request.number || github.event.issue.number)
26 || format('pr-vouch-run-{0}', github.run_id) }}
27 cancel-in-progress: true
28
29jobs:
30 collect-targets:
31 name: Collect PR targets
32 # Most comments are not recheck commands; skip them before a runner starts.
33 if: >-
34 github.event_name != 'issue_comment' ||
35 (github.event.issue.pull_request && contains(github.event.comment.body, '/recheck-vouch'))
36 runs-on: ubuntu-24.04
37 permissions:
38 pull-requests: read
39 outputs:
40 targets: ${{ steps.collect.outputs.targets }}
41 steps:
42 - id: collect
43 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
44 with:
45 script: |
46 if (context.eventName === "pull_request_target") {
47 const pr = context.payload.pull_request;
48 core.setOutput("targets", JSON.stringify([{ number: pr.number, user: pr.user.login }]));
49 return;
50 }
51
52 if (context.eventName === "issue_comment") {
53 const issue = context.payload.issue;
54 const body = context.payload.comment?.body ?? "";
55 if (!issue?.pull_request || !body.includes("/recheck-vouch")) {
56 core.setOutput("targets", "[]");
57 return;
58 }
59
60 core.setOutput(
61 "targets",
62 JSON.stringify([{ number: issue.number, user: issue.user.login }]),
63 );
64 return;
65 }
66
67 const pulls = await github.paginate(github.rest.pulls.list, {
68 owner: context.repo.owner,
69 repo: context.repo.repo,
70 state: "open",
71 per_page: 100,
72 });
73
74 const targets = pulls.map((pull) => ({
75 number: pull.number,
76 user: pull.user.login,
77 }));
78 core.setOutput("targets", JSON.stringify(targets));
79
80 label:
81 name: Label PR ${{ matrix.target.number }}
82 needs: collect-targets
83 if: ${{ needs.collect-targets.outputs.targets != '[]' }}
84 runs-on: ubuntu-24.04
85 permissions:
86 contents: read
87 pull-requests: write
88 strategy:
89 fail-fast: false
90 matrix:
91 target: ${{ fromJson(needs.collect-targets.outputs.targets) }}
92 steps:
93 - id: vouch
94 name: Check PR author trust
95 uses: mitchellh/vouch/action/check-user@d66fa29a64600490892131ad87597c30c91fcac4 # v1.5.0
96 with:
97 user: ${{ matrix.target.user }}
98 allow-fail: true
99 env:
100 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
101
102 - name: Sync PR labels
103 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
104 env:
105 PR_NUMBER: ${{ matrix.target.number }}
106 VOUCH_STATUS: ${{ steps.vouch.outputs.status }}
107 with:
108 script: |
109 const issueNumber = Number(process.env.PR_NUMBER);
110 const status = process.env.VOUCH_STATUS;
111 const managedLabels = [
112 {
113 name: "vouch:trusted",
114 color: "1f883d",
115 description: "Author is trusted by repo permissions or the VOUCHED list.",
116 },
117 {
118 name: "vouch:unvouched",
119 color: "fbca04",
120 description: "Author is not yet trusted in the VOUCHED list.",
121 },
122 {
123 name: "vouch:denounced",
124 color: "d1242f",
125 description: "Author is explicitly blocked by the VOUCHED list.",
126 },
127 ];
128
129 const managedLabelNames = new Set(managedLabels.map((label) => label.name));
130
131 for (const label of managedLabels) {
132 try {
133 const { data: existing } = await github.rest.issues.getLabel({
134 owner: context.repo.owner,
135 repo: context.repo.repo,
136 name: label.name,
137 });
138
139 if (
140 existing.color !== label.color ||
141 (existing.description ?? "") !== label.description
142 ) {
143 await github.rest.issues.updateLabel({
144 owner: context.repo.owner,
145 repo: context.repo.repo,
146 name: label.name,
147 color: label.color,
148 description: label.description,
149 });
150 }
151 } catch (error) {
152 if (error.status !== 404) {
153 throw error;
154 }
155
156 try {
157 await github.rest.issues.createLabel({
158 owner: context.repo.owner,
159 repo: context.repo.repo,
160 name: label.name,
161 color: label.color,
162 description: label.description,
163 });
164 } catch (createError) {
165 if (createError.status !== 422) {
166 throw createError;
167 }
168 }
169 }
170 }
171
172 const nextLabelName =
173 status === "denounced"
174 ? "vouch:denounced"
175 : ["bot", "collaborator", "vouched"].includes(status)
176 ? "vouch:trusted"
177 : "vouch:unvouched";
178
179 const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
180 owner: context.repo.owner,
181 repo: context.repo.repo,
182 issue_number: issueNumber,
183 per_page: 100,
184 });
185
186 for (const label of currentLabels) {
187 if (!managedLabelNames.has(label.name) || label.name === nextLabelName) {
188 continue;
189 }
190
191 try {
192 await github.rest.issues.removeLabel({
193 owner: context.repo.owner,
194 repo: context.repo.repo,
195 issue_number: issueNumber,
196 name: label.name,
197 });
198 } catch (removeError) {
199 if (removeError.status !== 404) {
200 throw removeError;
201 }
202 }
203 }
204
205 if (!currentLabels.some((label) => label.name === nextLabelName)) {
206 await github.rest.issues.addLabels({
207 owner: context.repo.owner,
208 repo: context.repo.repo,
209 issue_number: issueNumber,
210 labels: [nextLabelName],
211 });
212 }
213
214 core.info(`PR #${issueNumber}: ${status} -> ${nextLabelName}`);