Skip to content

Commit 51ebfa6

Browse files
[SL-ONLY] Simplify require admin workflow (#261)
Co-authored-by: Junior Martinez <[email protected]>
1 parent 9d297f3 commit 51ebfa6

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed

.github/workflows/silabs-require-admin-action-check.yaml

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,83 @@ permissions:
1717

1818
jobs:
1919
check-label:
20+
name: Fail CI if sl-require-admin-action label is present
2021
runs-on: ubuntu-latest
22+
if:
23+
${{ contains(github.event.pull_request.labels.*.name,
24+
'sl-require-admin-action') }}
2125
steps:
22-
- name: Check for sl-require-admin-action label
26+
- name: Add comment to PR
27+
uses: actions/github-script@v7
28+
with:
29+
script: |
30+
const prNumber = context.payload.pull_request.number;
31+
const comment = "The check for `sl-require-admin-action` label CI failure for this job is normal. An admin must do the merge.";
32+
const { data: comments } = await github.rest.issues.listComments({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
issue_number: prNumber,
36+
});
37+
const existingComment = comments.find(c => c.body.includes(comment));
38+
if (!existingComment) {
39+
await github.rest.issues.createComment({
40+
owner: context.repo.owner,
41+
repo: context.repo.repo,
42+
issue_number: prNumber,
43+
body: comment,
44+
});
45+
}
46+
47+
- name: Trigger CI failure
2348
run: |
24-
PR_NUMBER=${{ github.event.pull_request.number }}
25-
LABELS=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json labels --jq '.labels[].name')
26-
if echo "$LABELS" | grep -q "sl-require-admin-action"; then
27-
echo "The sl-require-admin-action label is present. Failing the job."
28-
COMMENTS=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json comments --jq '.comments[].body')
29-
if ! echo "$COMMENTS" | grep -q "The CI failure for this job is normal. An admin must do the merge."; then
30-
gh pr comment $PR_NUMBER --repo ${{ github.repository }} --body "The CI failure for this job is normal. An admin must do the merge."
31-
fi
32-
exit 1
33-
else
34-
echo "The sl-require-admin-action label is not present. Passing the job."
35-
fi
49+
echo "The sl-require-admin-action label is present. Failing the job."
50+
process.exit(1);
3651
env:
3752
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
3853

3954
prevent-label-removal:
55+
name: Prevent sl-require-admin-action label from being removed
4056
runs-on: ubuntu-latest
57+
if:
58+
github.event.action == 'unlabeled' && github.event.label.name ==
59+
'sl-require-admin-action'
4160
steps:
42-
- name: Prevent sl-require-admin-action label removal
43-
if: github.event.action == 'unlabeled'
61+
- name: Add comment to PR
62+
uses: actions/github-script@v7
63+
with:
64+
script: |
65+
const prNumber = context.payload.pull_request.number;
66+
const comment = "The `sl-require-admin-action` label cannot be removed once it has been added.";
67+
const { data: comments } = await github.rest.issues.listComments({
68+
owner: context.repo.owner,
69+
repo: context.repo.repo,
70+
issue_number: prNumber,
71+
});
72+
const existingComment = comments.find(c => c.body.includes(comment));
73+
if (!existingComment) {
74+
await github.rest.issues.createComment({
75+
owner: context.repo.owner,
76+
repo: context.repo.repo,
77+
issue_number: prNumber,
78+
body: comment,
79+
});
80+
}
81+
82+
- name: Re-add sl-require-admin-action label
83+
uses: actions/github-script@v7
84+
with:
85+
script: |
86+
const prNumber = context.payload.pull_request.number;
87+
await github.rest.issues.addLabels({
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
issue_number: prNumber,
91+
labels: ['sl-require-admin-action'],
92+
});
93+
94+
- name: Trigger CI failure
4495
run: |
45-
PR_NUMBER=${{ github.event.pull_request.number }}
46-
REMOVED_LABEL=${{ github.event.label.name }}
47-
if [ "$REMOVED_LABEL" == "sl-require-admin-action" ]; then
48-
echo "The sl-require-admin-action label cannot be removed. Failing the job."
49-
COMMENTS=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json comments --jq '.comments[].body')
50-
if ! echo "$COMMENTS" | grep -q "The sl-require-admin-action label cannot be removed once it has been added."; then
51-
gh pr comment $PR_NUMBER --repo ${{ github.repository }} --body "The sl-require-admin-action label cannot be removed once it has been added."
52-
fi
53-
exit 1
54-
else
55-
echo "A different label was removed. Passing the job."
56-
fi
96+
echo "The sl-require-admin-action label cannot be removed. Failing the job."
97+
process.exit(1);
5798
env:
5899
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

0 commit comments

Comments
 (0)