Skip to content

Commit 9a6f47d

Browse files
szymon-czaprackisjanc
authored andcommitted
ci: Change ci workflow conditions
Change condition from checking the role to checking the permissions of the user. Printout information before check, that way we don't skip the workflow on wrong conditions.
1 parent 793e95c commit 9a6f47d

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

.github/workflows/add_ci_label.yml

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,55 +30,65 @@ permissions:
3030

3131
jobs:
3232
add-label:
33-
if: ${{ github.event.pull_request.author_association != 'CONTRIBUTOR' }}
3433
runs-on: ubuntu-latest
3534
steps:
3635
- uses: actions/github-script@v7
3736
with:
3837
script: |
39-
const pr = context.payload.pull_request;
38+
const pr = context.payload.pull_request;
4039
41-
// Fetch author's repository permission level: admin|maintain|write|triage|read|none
40+
// Get author's effective repo permission: admin|maintain|write|triage|read|none
4241
let permission = 'unknown';
4342
try {
4443
const { data } = await github.rest.repos.getCollaboratorPermissionLevel({
4544
...context.repo,
4645
username: pr.user.login,
4746
});
48-
permission = data.permission;
47+
permission = data.permission || 'unknown';
4948
} catch (e) {
49+
permission = 'none';
5050
core.warning(`Could not fetch collaborator permission: ${e.status || ''} ${e.message}`);
5151
}
5252
53+
const trusted = ['admin','maintain','write'].includes(permission);
54+
5355
const info = {
5456
number: pr.number,
5557
title: pr.title,
5658
author: pr.user.login,
5759
author_association: pr.author_association,
5860
author_permission: permission,
61+
trusted_by_permission: trusted,
5962
base_repo: pr.base.repo.full_name,
6063
head_repo: pr.head.repo.full_name,
6164
is_fork: !!pr.head.repo.fork,
6265
};
6366
core.info('PR author info:\n' + JSON.stringify(info, null, 2));
64-
const label = 'needs-ci-approval';
65-
try {
67+
68+
// Only add the label if the author does NOT have write-level permission
69+
if (!trusted) {
70+
const label = 'needs-ci-approval';
6671
try {
67-
await github.request('POST /repos/{owner}/{repo}/labels', {
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({
6884
...context.repo,
69-
name: label,
70-
color: 'E3650b'
85+
issue_number: context.issue.number,
86+
labels: [label],
7187
});
88+
core.info(`Added '${label}' to PR #${context.issue.number}`);
7289
} catch (e) {
73-
if (e.status !== 422) throw e; // already exists
90+
core.setFailed(`Failed to label PR: ${e.status || ''} ${e.message}`);
7491
}
75-
76-
await github.rest.issues.addLabels({
77-
...context.repo,
78-
issue_number: context.issue.number,
79-
labels: [label],
80-
});
81-
core.info(`Added '${label}' to PR #${context.issue.number}`);
82-
} catch (e) {
83-
core.setFailed(`Failed to label PR: ${e.status || ''} ${e.message}`);
92+
} else {
93+
core.info('Author has write-level permission; not adding label.');
8494
}

0 commit comments

Comments
 (0)