Skip to content

Action test

Action test #59

name: Assign Unique Reviewer
on:
pull_request:
types: [opened, synchronize]
permissions:
issues: write
pull-requests: write
jobs:
assign:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2
- 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: Select random collaborator
id: select-random
run: |
# Convert collaborators to array
COLLABORATORS_ARRAY=($COLLABORATORS)
# Convert existing reviewers to array
EXISTING_REVIEWERS_ARRAY=($EXISTING_REVIEWERS)
# Remove PR author 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} " ]]; 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"