Skip to content

Commit a336bfd

Browse files
Update pr_review.yml
1 parent 010df2c commit a336bfd

File tree

1 file changed

+189
-161
lines changed

1 file changed

+189
-161
lines changed

.github/workflows/pr_review.yml

Lines changed: 189 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,204 +1,232 @@
1+
# # name: PR Review Handling
2+
3+
# # on:
4+
# # pull_request_review:
5+
# # types: [submitted]
6+
7+
# # permissions: {}
8+
9+
# # jobs:
10+
# # handle_review:
11+
# # runs-on: ubuntu-latest
12+
13+
# # steps:
14+
# # - name: Checkout repository
15+
# # uses: actions/checkout@v4
16+
17+
# # - name: Extract and sanitize PR metadata
18+
# # id: pr_meta
19+
# # env:
20+
# # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
# # run: |
22+
# # # Extract base values
23+
# # PR_NUMBER=${{ github.event.pull_request.number }}
24+
# # AUTHOR_LOGIN="${{ github.event.pull_request.user.login }}"
25+
# # REVIEWER_LOGIN="${{ github.event.review.user.login }}"
26+
# # REVIEW_STATE="${{ github.event.review.state }}"
27+
28+
# # # Sanitize inputs
29+
# # SANITIZED_AUTHOR=$(echo "$AUTHOR_LOGIN" | sed 's/[^a-zA-Z0-9_-]//g')
30+
# # SANITIZED_REVIEWER=$(echo "$REVIEWER_LOGIN" | sed 's/[^a-zA-Z0-9_-]//g')
31+
32+
# # # Get requested reviewers using GitHub API
33+
# # REQUESTED_REVIEWERS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
34+
# # "https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER" | \
35+
# # grep -Eo '"login": "[^"]+"' | \
36+
# # awk -F'"' '{print $4}' | \
37+
# # tr '\n' ' ' | \
38+
# # sed 's/ /","/g; s/^/["/; s/$/"]/')
39+
40+
# # echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
41+
# # echo "author_login=$SANITIZED_AUTHOR" >> $GITHUB_OUTPUT
42+
# # echo "reviewer_login=$SANITIZED_REVIEWER" >> $GITHUB_OUTPUT
43+
# # echo "review_state=$REVIEW_STATE" >> $GITHUB_OUTPUT
44+
# # echo "requested_reviewers=$REQUESTED_REVIEWERS" >> $GITHUB_OUTPUT
45+
46+
# # - name: Handle Approval
47+
# # if: steps.pr_meta.outputs.review_state == 'approved'
48+
# # env:
49+
# # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
# # run: |
51+
# # # Get current approvals
52+
# # APPROVALS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
53+
# # "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.pr_meta.outputs.pr_number }}/reviews" | \
54+
# # grep -B 3 '"state": "APPROVED"' | \
55+
# # grep '"login":' | \
56+
# # awk -F'"' '{print $4}' | \
57+
# # sort -u)
58+
59+
# # # Process requested reviewers
60+
# # REQUESTED=$(echo ${{ steps.pr_meta.outputs.requested_reviewers }} | tr -d '[]"' | tr ',' ' ')
61+
62+
# # ALL_APPROVED=true
63+
# # PENDING_REVIEWERS=()
64+
# # for reviewer in $REQUESTED; do
65+
# # if ! echo "$APPROVALS" | grep -qxF "$reviewer"; then
66+
# # ALL_APPROVED=false
67+
# # PENDING_REVIEWERS+=("$reviewer")
68+
# # fi
69+
# # done
70+
71+
# # if $ALL_APPROVED; then
72+
# # # Add LGTM label
73+
# # curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
74+
# # -H "Content-Type: application/json" \
75+
# # "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/labels" \
76+
# # -d '{"labels":["LGTM"]}'
77+
78+
# # # Assign author
79+
# # curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
80+
# # -H "Content-Type: application/json" \
81+
# # "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/assignees" \
82+
# # -d '{"assignees":["'"${{ steps.pr_meta.outputs.author_login }}"'"]}'
83+
# # else
84+
# # # Request pending reviews
85+
# # for reviewer in "${PENDING_REVIEWERS[@]}"; do
86+
# # curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
87+
# # -H "Content-Type: application/json" \
88+
# # "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.pr_meta.outputs.pr_number }}/requested_reviewers" \
89+
# # -d '{"reviewers":["'"$reviewer"'"]}'
90+
# # done
91+
# # fi
92+
93+
# # - name: Handle Changes Requested
94+
# # if: steps.pr_meta.outputs.review_state == 'changes_requested'
95+
# # env:
96+
# # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
97+
# # run: |
98+
# # # Remove LGTM label
99+
# # curl -s -X DELETE -H "Authorization: Bearer $GITHUB_TOKEN" \
100+
# # "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/labels/LGTM" || true
101+
102+
# # # Unassign reviewer
103+
# # curl_response=$(curl -s -w "%{http_code}" -X DELETE \
104+
# # -H "Authorization: Bearer $GITHUB_TOKEN" \
105+
# # -H "Content-Type: application/json" \
106+
# # "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/assignees" \
107+
# # -d '{"assignees":["'"${{ steps.pr_meta.outputs.reviewer_login }}"'"]}')
108+
109+
# # # Assign author
110+
# # curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
111+
# # -H "Content-Type: application/json" \
112+
# # "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/assignees" \
113+
# # -d '{"assignees":["'"${{ steps.pr_meta.outputs.author_login }}"'"]}'
114+
115+
# # # Debug output
116+
# # echo "Unassignment response code: ${curl_response: -3}"
117+
# # echo "Unassignment response body: ${curl_response%???}"
1118
# name: PR Review Handling
2119

3120
# on:
4121
# pull_request_review:
5122
# types: [submitted]
6123

7-
# permissions: {}
8-
9124
# jobs:
10125
# handle_review:
11126
# runs-on: ubuntu-latest
12-
127+
# permissions:
128+
# pull-requests: write
129+
# issues: write
13130
# steps:
14-
# - name: Checkout repository
15-
# uses: actions/checkout@v4
16-
17-
# - name: Extract and sanitize PR metadata
18-
# id: pr_meta
131+
# - name: Handle PR Review
19132
# env:
20133
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21134
# run: |
22-
# # Extract base values
135+
# # Extract PR details and sanitize inputs
23136
# PR_NUMBER=${{ github.event.pull_request.number }}
24137
# AUTHOR_LOGIN="${{ github.event.pull_request.user.login }}"
25138
# REVIEWER_LOGIN="${{ github.event.review.user.login }}"
26139
# REVIEW_STATE="${{ github.event.review.state }}"
27140

28-
# # Sanitize inputs
29141
# SANITIZED_AUTHOR=$(echo "$AUTHOR_LOGIN" | sed 's/[^a-zA-Z0-9_-]//g')
30142
# SANITIZED_REVIEWER=$(echo "$REVIEWER_LOGIN" | sed 's/[^a-zA-Z0-9_-]//g')
31143

32-
# # Get requested reviewers using GitHub API
144+
# # Get requested reviewers via GitHub API
33145
# REQUESTED_REVIEWERS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
34146
# "https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER" | \
35-
# grep -Eo '"login": "[^"]+"' | \
36-
# awk -F'"' '{print $4}' | \
37-
# tr '\n' ' ' | \
38-
# sed 's/ /","/g; s/^/["/; s/$/"]/')
39-
40-
# echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
41-
# echo "author_login=$SANITIZED_AUTHOR" >> $GITHUB_OUTPUT
42-
# echo "reviewer_login=$SANITIZED_REVIEWER" >> $GITHUB_OUTPUT
43-
# echo "review_state=$REVIEW_STATE" >> $GITHUB_OUTPUT
44-
# echo "requested_reviewers=$REQUESTED_REVIEWERS" >> $GITHUB_OUTPUT
45-
46-
# - name: Handle Approval
47-
# if: steps.pr_meta.outputs.review_state == 'approved'
48-
# env:
49-
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50-
# run: |
51-
# # Get current approvals
52-
# APPROVALS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
53-
# "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.pr_meta.outputs.pr_number }}/reviews" | \
54-
# grep -B 3 '"state": "APPROVED"' | \
55-
# grep '"login":' | \
56-
# awk -F'"' '{print $4}' | \
57-
# sort -u)
58-
59-
# # Process requested reviewers
60-
# REQUESTED=$(echo ${{ steps.pr_meta.outputs.requested_reviewers }} | tr -d '[]"' | tr ',' ' ')
61-
62-
# ALL_APPROVED=true
63-
# PENDING_REVIEWERS=()
64-
# for reviewer in $REQUESTED; do
65-
# if ! echo "$APPROVALS" | grep -qxF "$reviewer"; then
66-
# ALL_APPROVED=false
67-
# PENDING_REVIEWERS+=("$reviewer")
147+
# grep -Eo '"login": "[^"]+"' | awk -F'"' '{print $4}' | \
148+
# tr '\n' ' ' | sed 's/ /","/g; s/^/[\"/; s/$/\"]/')
149+
150+
# if [[ "$REVIEW_STATE" == "APPROVED" ]]; then
151+
# # Retrieve current approvals
152+
# APPROVALS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
153+
# "https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews" | \
154+
# grep -B 3 '"state": "APPROVED"' | grep '"login":' | awk -F'"' '{print $4}' | sort -u)
155+
156+
# ALL_APPROVED=true
157+
# PENDING_REVIEWERS=()
158+
# for reviewer in $(echo $REQUESTED_REVIEWERS | tr -d '[]"' | tr ',' ' '); do
159+
# if ! echo "$APPROVALS" | grep -qxF "$reviewer"; then
160+
# ALL_APPROVED=false
161+
# PENDING_REVIEWERS+=( "$reviewer" )
162+
# fi
163+
# done
164+
165+
# if $ALL_APPROVED; then
166+
# # Add LGTM label and assign the author
167+
# curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
168+
# -H "Content-Type: application/json" \
169+
# "https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/labels" \
170+
# -d '{"labels":["LGTM"]}'
171+
172+
# curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
173+
# -H "Content-Type: application/json" \
174+
# "https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/assignees" \
175+
# -d "{\"assignees\":[\"$SANITIZED_AUTHOR\"]}"
176+
# else
177+
# # Re-request pending reviews
178+
# for reviewer in "${PENDING_REVIEWERS[@]}"; do
179+
# curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
180+
# -H "Content-Type: application/json" \
181+
# "https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/requested_reviewers" \
182+
# -d "{\"reviewers\":[\"$reviewer\"]}"
183+
# done
68184
# fi
69-
# done
70185

71-
# if $ALL_APPROVED; then
72-
# # Add LGTM label
73-
# curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
186+
# elif [[ "$REVIEW_STATE" == "CHANGES_REQUESTED" ]]; then
187+
# # Remove LGTM label, unassign reviewer, and reassign author
188+
# curl -s -X DELETE -H "Authorization: Bearer $GITHUB_TOKEN" \
189+
# "https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/labels/LGTM" || true
190+
191+
# curl_response=$(curl -s -w "%{http_code}" -X DELETE \
192+
# -H "Authorization: Bearer $GITHUB_TOKEN" \
74193
# -H "Content-Type: application/json" \
75-
# "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/labels" \
76-
# -d '{"labels":["LGTM"]}'
194+
# "https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/assignees" \
195+
# -d "{\"assignees\":[\"$SANITIZED_REVIEWER\"]}")
77196

78-
# # Assign author
79197
# curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
80198
# -H "Content-Type: application/json" \
81-
# "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/assignees" \
82-
# -d '{"assignees":["'"${{ steps.pr_meta.outputs.author_login }}"'"]}'
83-
# else
84-
# # Request pending reviews
85-
# for reviewer in "${PENDING_REVIEWERS[@]}"; do
86-
# curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
87-
# -H "Content-Type: application/json" \
88-
# "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.pr_meta.outputs.pr_number }}/requested_reviewers" \
89-
# -d '{"reviewers":["'"$reviewer"'"]}'
90-
# done
91-
# fi
199+
# "https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/assignees" \
200+
# -d "{\"assignees\":[\"$SANITIZED_AUTHOR\"]}"
92201

93-
# - name: Handle Changes Requested
94-
# if: steps.pr_meta.outputs.review_state == 'changes_requested'
95-
# env:
96-
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
97-
# run: |
98-
# # Remove LGTM label
99-
# curl -s -X DELETE -H "Authorization: Bearer $GITHUB_TOKEN" \
100-
# "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/labels/LGTM" || true
101-
102-
# # Unassign reviewer
103-
# curl_response=$(curl -s -w "%{http_code}" -X DELETE \
104-
# -H "Authorization: Bearer $GITHUB_TOKEN" \
105-
# -H "Content-Type: application/json" \
106-
# "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/assignees" \
107-
# -d '{"assignees":["'"${{ steps.pr_meta.outputs.reviewer_login }}"'"]}')
108-
109-
# # Assign author
110-
# curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
111-
# -H "Content-Type: application/json" \
112-
# "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/assignees" \
113-
# -d '{"assignees":["'"${{ steps.pr_meta.outputs.author_login }}"'"]}'
114-
115-
# # Debug output
116-
# echo "Unassignment response code: ${curl_response: -3}"
117-
# echo "Unassignment response body: ${curl_response%???}"
118-
name: PR Review Handling
202+
# echo "Unassignment response code: ${curl_response: -3}"
203+
# echo "Unassignment response body: ${curl_response%???}"
204+
# fi
205+
name: PR Review Handler
119206

120207
on:
121208
pull_request_review:
122209
types: [submitted]
123210

124211
jobs:
125-
handle_review:
212+
handle:
126213
runs-on: ubuntu-latest
127214
permissions:
128215
pull-requests: write
129216
issues: write
130217
steps:
131-
- name: Handle PR Review
218+
- name: Checkout repository
219+
uses: actions/checkout@v4
220+
221+
- name: Set up Python
222+
uses: actions/setup-python@v4
223+
with:
224+
python-version: '3.10'
225+
226+
- name: Install dependencies
227+
run: pip install PyGithub
228+
229+
- name: Run PR handler
132230
env:
133231
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
134-
run: |
135-
# Extract PR details and sanitize inputs
136-
PR_NUMBER=${{ github.event.pull_request.number }}
137-
AUTHOR_LOGIN="${{ github.event.pull_request.user.login }}"
138-
REVIEWER_LOGIN="${{ github.event.review.user.login }}"
139-
REVIEW_STATE="${{ github.event.review.state }}"
140-
141-
SANITIZED_AUTHOR=$(echo "$AUTHOR_LOGIN" | sed 's/[^a-zA-Z0-9_-]//g')
142-
SANITIZED_REVIEWER=$(echo "$REVIEWER_LOGIN" | sed 's/[^a-zA-Z0-9_-]//g')
143-
144-
# Get requested reviewers via GitHub API
145-
REQUESTED_REVIEWERS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
146-
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER" | \
147-
grep -Eo '"login": "[^"]+"' | awk -F'"' '{print $4}' | \
148-
tr '\n' ' ' | sed 's/ /","/g; s/^/[\"/; s/$/\"]/')
149-
150-
if [[ "$REVIEW_STATE" == "APPROVED" ]]; then
151-
# Retrieve current approvals
152-
APPROVALS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
153-
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews" | \
154-
grep -B 3 '"state": "APPROVED"' | grep '"login":' | awk -F'"' '{print $4}' | sort -u)
155-
156-
ALL_APPROVED=true
157-
PENDING_REVIEWERS=()
158-
for reviewer in $(echo $REQUESTED_REVIEWERS | tr -d '[]"' | tr ',' ' '); do
159-
if ! echo "$APPROVALS" | grep -qxF "$reviewer"; then
160-
ALL_APPROVED=false
161-
PENDING_REVIEWERS+=( "$reviewer" )
162-
fi
163-
done
164-
165-
if $ALL_APPROVED; then
166-
# Add LGTM label and assign the author
167-
curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
168-
-H "Content-Type: application/json" \
169-
"https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/labels" \
170-
-d '{"labels":["LGTM"]}'
171-
172-
curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
173-
-H "Content-Type: application/json" \
174-
"https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/assignees" \
175-
-d "{\"assignees\":[\"$SANITIZED_AUTHOR\"]}"
176-
else
177-
# Re-request pending reviews
178-
for reviewer in "${PENDING_REVIEWERS[@]}"; do
179-
curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
180-
-H "Content-Type: application/json" \
181-
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/requested_reviewers" \
182-
-d "{\"reviewers\":[\"$reviewer\"]}"
183-
done
184-
fi
185-
186-
elif [[ "$REVIEW_STATE" == "CHANGES_REQUESTED" ]]; then
187-
# Remove LGTM label, unassign reviewer, and reassign author
188-
curl -s -X DELETE -H "Authorization: Bearer $GITHUB_TOKEN" \
189-
"https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/labels/LGTM" || true
190-
191-
curl_response=$(curl -s -w "%{http_code}" -X DELETE \
192-
-H "Authorization: Bearer $GITHUB_TOKEN" \
193-
-H "Content-Type: application/json" \
194-
"https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/assignees" \
195-
-d "{\"assignees\":[\"$SANITIZED_REVIEWER\"]}")
196-
197-
curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
198-
-H "Content-Type: application/json" \
199-
"https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/assignees" \
200-
-d "{\"assignees\":[\"$SANITIZED_AUTHOR\"]}"
201-
202-
echo "Unassignment response code: ${curl_response: -3}"
203-
echo "Unassignment response body: ${curl_response%???}"
204-
fi
232+
run: python -m scripts.pr_review_handler

0 commit comments

Comments
 (0)