@@ -16,35 +16,40 @@ jobs:
16
16
17
17
- name : Check if the PR author is a collaborator
18
18
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
21
24
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"
25
26
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
28
38
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' }}
32
41
33
42
- 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"
39
44
if : >-
40
- steps.authorization.outcome != 'success' &&
45
+ steps.authorization.outputs.collaborator == 'false' &&
46
+ steps.read-label.outputs.safe_label != '' &&
41
47
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')
43
49
44
50
- name : Fail if not now labeled
45
51
run : >-
46
52
gh api -H "Accept: application/vnd.github.v3+json" $API_URL
47
53
--jq .labels | grep 'safe to test'
48
54
env :
49
- GITHUB_TOKEN : ${{ env.GITHUB_TOKEN }}
50
55
API_URL : /repos/${{ github.repository }}/issues/${{ github.event.number }}
0 commit comments