Skip to content

Commit

Permalink
github/actions: Update the codeowners workflow to use pull_request_ta…
Browse files Browse the repository at this point in the history
…rget and streamline JSON handling.

- Change trigger event from pull_request to pull_request_target to allow
  review requests on PRs from forked repos with correct permissions.
- Simplify JSON array construction for reviewer usernames to eliminate
  unnecessary steps like removing '@' and converting space-separated
  usernames into a Bash array.
- Remove explicit GitHub API version header as it is no longer required.

- Enhance comments to better explain the steps for future maintenance.
- Adjust permission alignment in the workflow YAML for consistency.

Signed-off-by: Nikolay Martyanov <[email protected]>
  • Loading branch information
OhmSpectator authored and rene committed Nov 6, 2023
1 parent c63dfe0 commit 3f7b663
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions .github/workflows/request_codeowners_review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
name: Request Code Owners Review

on:
pull_request:
pull_request_target:
types: ["opened", "synchronize"]

jobs:
auto_request_review:
runs-on: ubuntu-latest
permissions:
pull-requests: write
pull-requests: write
steps:
- name: Calculate fetch-depth
id: calc
Expand Down Expand Up @@ -46,24 +46,21 @@ jobs:
fi
done < .github/CODEOWNERS
done
# Remove duplicates
# Remove duplicates and format for JSON
reviewers=$(echo "$reviewers" | xargs -n1 | sort -u | xargs)
IFS=' ' read -ra reviewers_array <<< "$reviewers"
json_array=$(printf ',\"%s\"' "${reviewers_array[@]}")
json_array="[${json_array:1}]" # Remove the leading comma
echo "JSON array: $json_array"
if [ -n "$reviewers" ]; then
# Remove @ from reviewers (it stays in the beginning of the username)
reviewers_cleaned=${reviewers//@/}
# Convert the space-separated usernames into a Bash array
IFS=' ' read -ra reviewers_array <<< "$reviewers_cleaned"
# Convert the Bash array into a JSON array string
json_array="[\"$(echo "${reviewers_array[@]}" | tr ' ' '","' )\"]"
echo "Requesting review from: ${reviewers_array[*]}"
echo "JSON array: $json_array"
echo https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers
curl -L \
-D - \
-X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers \
-d "{\"reviewers\": $json_array}"
else
Expand Down

0 comments on commit 3f7b663

Please sign in to comment.