@@ -11,56 +11,55 @@ jobs:
1111 - name : Checkout repository
1212 uses : actions/checkout@v4
1313
14- - name : Extract PR metadata
14+ - name : Extract and sanitize PR metadata
1515 id : pr_meta
1616 env :
1717 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
1818 run : |
19+ # Extract base values
1920 PR_NUMBER=${{ github.event.pull_request.number }}
2021 AUTHOR_LOGIN="${{ github.event.pull_request.user.login }}"
2122 REVIEWER_LOGIN="${{ github.event.review.user.login }}"
2223 REVIEW_STATE="${{ github.event.review.state }}"
23-
24- # Get sanitized requested reviewers using curl
24+
25+ # Sanitize inputs
26+ SANITIZED_AUTHOR=$(echo "$AUTHOR_LOGIN" | sed 's/[^a-zA-Z0-9_-]//g')
27+ SANITIZED_REVIEWER=$(echo "$REVIEWER_LOGIN" | sed 's/[^a-zA-Z0-9_-]//g')
28+
29+ # Get requested reviewers using GitHub API
2530 REQUESTED_REVIEWERS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
2631 "https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER" | \
27- grep -E '"login":' | \
32+ grep -Eo '"login": "[^"]+" ' | \
2833 awk -F'"' '{print $4}' | \
2934 tr '\n' ' ' | \
30- sed 's/ *$//' | \
31- sed 's/ /","/g' | \
32- sed 's/^/["/;s/$/"]/')
33-
35+ sed 's/ /","/g; s/^/["/; s/$/"]/')
36+
3437 echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
35- echo "author_login=$(printf '%s' "$AUTHOR_LOGIN" | sed 's/"/\\"/g') " >> $GITHUB_OUTPUT
36- echo "reviewer_login=$(printf '%s' "$REVIEWER_LOGIN" | sed 's/"/\\"/g') " >> $GITHUB_OUTPUT
37- echo "review_state=$(printf '%s' "$ REVIEW_STATE" | sed 's/"/\\"/g') " >> $GITHUB_OUTPUT
38+ echo "author_login=$SANITIZED_AUTHOR " >> $GITHUB_OUTPUT
39+ echo "reviewer_login=$SANITIZED_REVIEWER " >> $GITHUB_OUTPUT
40+ echo "review_state=$REVIEW_STATE" >> $GITHUB_OUTPUT
3841 echo "requested_reviewers=$REQUESTED_REVIEWERS" >> $GITHUB_OUTPUT
3942
4043 - name : Handle Approval
4144 if : steps.pr_meta.outputs.review_state == 'approved'
4245 env :
4346 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
4447 run : |
45- # Get all approvals using curl
48+ # Get current approvals
4649 APPROVALS=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
4750 "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.pr_meta.outputs.pr_number }}/reviews" | \
4851 grep -B 3 '"state": "APPROVED"' | \
4952 grep '"login":' | \
5053 awk -F'"' '{print $4}' | \
5154 sort -u)
5255
53- # Convert requested reviewers to space-separated list
54- REQUESTED=$(echo ${{ steps.pr_meta.outputs.requested_reviewers }} | \
55- sed 's/[][]//g' | \
56- tr ',' ' ' | \
57- tr -d '"')
56+ # Process requested reviewers
57+ REQUESTED=$(echo ${{ steps.pr_meta.outputs.requested_reviewers }} | tr -d '[]"' | tr ',' ' ')
5858
59- # Check approval status
6059 ALL_APPROVED=true
6160 PENDING_REVIEWERS=()
6261 for reviewer in $REQUESTED; do
63- if ! echo "$APPROVALS" | grep -q "^${ reviewer}$ "; then
62+ if ! echo "$APPROVALS" | grep -qxF "$ reviewer"; then
6463 ALL_APPROVED=false
6564 PENDING_REVIEWERS+=("$reviewer")
6665 fi
7978 "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/assignees" \
8079 -d '{"assignees":["'"${{ steps.pr_meta.outputs.author_login }}"'"]}'
8180 else
82- # Re-request pending reviews
81+ # Request pending reviews
8382 for reviewer in "${PENDING_REVIEWERS[@]}"; do
8483 curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
8584 -H "Content-Type: application/json" \
@@ -95,16 +94,21 @@ jobs:
9594 run : |
9695 # Remove LGTM label
9796 curl -s -X DELETE -H "Authorization: Bearer $GITHUB_TOKEN" \
98- "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/labels/LGTM"
97+ "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/labels/LGTM" || true
9998
100- # Remove reviewer assignment
101- curl -s -X DELETE -H "Authorization: Bearer $GITHUB_TOKEN" \
99+ # Unassign reviewer
100+ curl_response=$(curl -s -w "%{http_code}" -X DELETE \
101+ -H "Authorization: Bearer $GITHUB_TOKEN" \
102102 -H "Content-Type: application/json" \
103103 "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/assignees" \
104- -d '{"assignees":["'"${{ steps.pr_meta.outputs.reviewer_login }}"'"]}'
104+ -d '{"assignees":["'"${{ steps.pr_meta.outputs.reviewer_login }}"'"]}')
105105
106106 # Assign author
107107 curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
108108 -H "Content-Type: application/json" \
109109 "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.pr_meta.outputs.pr_number }}/assignees" \
110110 -d '{"assignees":["'"${{ steps.pr_meta.outputs.author_login }}"'"]}'
111+
112+ # Debug output
113+ echo "Unassignment response code: ${curl_response: -3}"
114+ echo "Unassignment response body: ${curl_response%???}"
0 commit comments