Skip to content

Commit c802fe7

Browse files
szymon-czaprackisjanc
authored andcommitted
ci: Auto label PR's with needs-ci-approval
Default github status for PR's workflows might be misleading as not all jobs were run, but github still marks checks as passed. Add a workflow that labels PRs if they still need jobs to be run. Skips labeling PR's for users with write priveleges.
1 parent 8ec5464 commit c802fe7

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

.github/workflows/add_ci_label.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
name: Informative CI status
21+
22+
on:
23+
pull_request_target:
24+
types: [opened, ready_for_review, reopened]
25+
26+
permissions:
27+
contents: read
28+
issues: write
29+
pull-requests: write
30+
31+
jobs:
32+
add-label:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/github-script@v7
36+
with:
37+
script: |
38+
const pr = context.payload.pull_request;
39+
40+
// Get author's effective repo permission: admin|maintain|write|triage|read|none
41+
let permission = 'unknown';
42+
try {
43+
const { data } = await github.rest.repos.getCollaboratorPermissionLevel({
44+
...context.repo,
45+
username: pr.user.login,
46+
});
47+
permission = data.permission || 'unknown';
48+
} catch (e) {
49+
permission = 'none';
50+
core.warning(`Could not fetch collaborator permission: ${e.status || ''} ${e.message}`);
51+
}
52+
53+
const trusted = ['admin','maintain','write'].includes(permission);
54+
55+
const info = {
56+
number: pr.number,
57+
title: pr.title,
58+
author: pr.user.login,
59+
author_association: pr.author_association,
60+
author_permission: permission,
61+
trusted_by_permission: trusted,
62+
base_repo: pr.base.repo.full_name,
63+
head_repo: pr.head.repo.full_name,
64+
is_fork: !!pr.head.repo.fork,
65+
};
66+
core.info('PR author info:\n' + JSON.stringify(info, null, 2));
67+
68+
// Only add the label if the author does NOT have write-level permission
69+
if (!trusted) {
70+
const label = 'needs-ci-approval';
71+
try {
72+
// Ensure the label exists (422 = already exists)
73+
try {
74+
await github.request('POST /repos/{owner}/{repo}/labels', {
75+
...context.repo,
76+
name: label,
77+
color: 'E3650b',
78+
});
79+
} catch (e) {
80+
if (e.status !== 422) throw e;
81+
}
82+
83+
await github.rest.issues.addLabels({
84+
...context.repo,
85+
issue_number: context.issue.number,
86+
labels: [label],
87+
});
88+
core.info(`Added '${label}' to PR #${context.issue.number}`);
89+
} catch (e) {
90+
core.setFailed(`Failed to label PR: ${e.status || ''} ${e.message}`);
91+
}
92+
} else {
93+
core.info('Author has write-level permission; not adding label.');
94+
}

0 commit comments

Comments
 (0)