Skip to content

Commit d617085

Browse files
authored
safe-to-test worflow: some updates (#110)
* Review the way collaborator are retrieved * minor updates * Rewrite multi line condition
1 parent 832a912 commit d617085

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

.github/workflows/safe-to-test.yml

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,40 @@ jobs:
1616

1717
- name: Check if the PR author is a collaborator
1818
id: authorization
19-
run: 'gh api -H "Accept: application/vnd.github.v3+json" $API_URL'
20-
continue-on-error: true
19+
run: |
20+
user_role=$(gh api --jq .permission -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" $GH_API_URL)
21+
roles=("write maintain admin")
22+
[[ "${roles[*]} " =~ "${user_role} " ]] && collaborator=true || collaborator=false
23+
echo "collaborator=${collaborator}" >> $GITHUB_OUTPUT
2124
env:
22-
API_URL: /repos/${{ github.repository }}/collaborators/${{ github.event.pull_request.user.login }}
23-
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
24-
if: github.event.label.name != 'safe to test'
25+
GH_API_URL: "/repos/${{ github.repository }}/collaborators/${{ github.event.pull_request.user.login }}/permission"
2526

26-
- name: If collaborator, add the label
27-
run: gh pr edit $PR_NUMBER --add-label "safe to test"
27+
# Add 'safe to test' label for collaborators
28+
- name: Add safe label for User with required roles
29+
run: gh pr edit ${{ github.event.number }} --add-label "safe to test"
30+
if: ${{ steps.authorization.outputs.collaborator == 'true' }}
31+
32+
# Remove 'safe to test' for non collaborators
33+
- name: Get pull request labels
34+
id: read-label
35+
run: |
36+
SAFE_LABEL=$(gh api --jq '.[] | select(.name == "safe to test") | .name' -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" $GH_API_URL)
37+
echo "safe_label=$SAFE_LABEL" >> $GITHUB_OUTPUT
2838
env:
29-
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
30-
PR_NUMBER: ${{ github.event.number }}
31-
if: steps.authorization.outcome == 'success'
39+
GH_API_URL: /repos/${{ github.repository }}/issues/${{ github.event.number }}/labels
40+
if: ${{ steps.authorization.outputs.collaborator == 'false' }}
3241

3342
- name: Remove the 'safe to test', not a collaborator, PR was updated or not just added
34-
id: removed
35-
run: gh pr edit $PR_NUMBER --remove-label "safe to test"
36-
env:
37-
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
38-
PR_NUMBER: ${{ github.event.number }}
43+
run: gh pr edit ${{ github.event.number }} --remove-label "safe to test"
3944
if: >-
40-
steps.authorization.outcome != 'success' &&
45+
steps.authorization.outputs.collaborator == 'false' &&
46+
steps.read-label.outputs.safe_label != '' &&
4147
github.event.label.name != 'safe to test' &&
42-
( github.event.action == 'synchronize' || github.event.action == 'reopened' )
48+
(github.event.action == 'synchronize' || github.event.action == 'reopened')
4349
4450
- name: Fail if not now labeled
4551
run: >-
4652
gh api -H "Accept: application/vnd.github.v3+json" $API_URL
4753
--jq .labels | grep 'safe to test'
4854
env:
49-
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
5055
API_URL: /repos/${{ github.repository }}/issues/${{ github.event.number }}

0 commit comments

Comments
 (0)