-
Notifications
You must be signed in to change notification settings - Fork 0
91 lines (77 loc) Β· 3.73 KB
/
assign-reviewer.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
name: Assign Unique Reviewer
on:
pull_request:
types: [opened]
permissions:
issues: write
pull-requests: write
jobs:
assign:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2
with:
token: ${{ secrets.PAT }} # PATλ₯Ό μ¬μ©νλλ‘ μ€μ
persist-credentials: false
- name: Fetch repository collaborators
id: fetch-collaborators
run: |
RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.PAT }}" \
"https://api.github.com/repos/${{ github.repository }}/collaborators")
COLLABORATORS=$(echo "$RESPONSE" | jq -r '.[].login' | tr '\n' ' ')
echo "COLLABORATORS=$COLLABORATORS" >> $GITHUB_ENV
- name: Fetch existing PR reviewers
id: fetch-pr-reviewers
run: |
RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.PAT }}" \
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers")
EXISTING_REVIEWERS=$(echo "$RESPONSE" | jq -r '.users[].login' | sort | uniq | tr '\n' ' ')
echo "EXISTING_REVIEWERS=$EXISTING_REVIEWERS" >> $GITHUB_ENV
- name: Fetch all assigned reviewers
id: fetch-all-reviewers
run: |
RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.PAT }}" \
"https://api.github.com/repos/${{ github.repository }}/pulls")
ALL_REVIEWERS=$(echo "$RESPONSE" | jq -r '.[].requested_reviewers | .[].login' | sort | uniq | tr '\n' ' ')
echo "ALL_REVIEWERS=$ALL_REVIEWERS" >> $GITHUB_ENV
- name: Select random collaborator
id: select-random
run: |
# Convert collaborators to array
COLLABORATORS_ARRAY=($COLLABORATORS)
# Convert existing reviewers to array
EXISTING_REVIEWERS_ARRAY=($EXISTING_REVIEWERS)
# Convert all reviewers to array
ALL_REVIEWERS_ARRAY=($ALL_REVIEWERS)
# Remove PR author and already assigned reviewers from collaborators
PR_AUTHOR=${{ github.event.pull_request.user.login }}
FILTERED_COLLABORATORS=()
for collaborator in "${COLLABORATORS_ARRAY[@]}"; do
if [[ "$collaborator" != "$PR_AUTHOR" && ! " ${EXISTING_REVIEWERS_ARRAY[@]} " =~ " ${collaborator} " && ! " ${ALL_REVIEWERS_ARRAY[@]} " =~ " ${collaborator} " ]]; then
FILTERED_COLLABORATORS+=($collaborator)
fi
done
# Check if there are any eligible collaborators left
if [ ${#FILTERED_COLLABORATORS[@]} -eq 0 ]; then
echo "No eligible collaborators to assign"
exit 1
fi
# Select a random collaborator from filtered collaborators
RANDOM_COLLABORATOR=${FILTERED_COLLABORATORS[$RANDOM % ${#FILTERED_COLLABORATORS[@]}]}
echo "SELECTED_COLLABORATOR=$RANDOM_COLLABORATOR" >> $GITHUB_ENV
- name: Assign PR to selected reviewer
run: |
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ secrets.PAT }}" \
-d '{"reviewers":["${{ env.SELECTED_COLLABORATOR }}"]}' \
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers"
- name: Assign PR to the author
run: |
PR_AUTHOR=${{ github.event.pull_request.user.login }}
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ secrets.PAT }}" \
-d '{"assignees":["'$PR_AUTHOR'"]}' \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/assignees"