Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Action test! #32

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 53 additions & 30 deletions .github/workflows/assign-reviewer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,78 @@ jobs:
steps:
- name: Check out repository
uses: actions/checkout@v2
with:
token: ${{ secrets.PAT }} # PAT를 사용하도록 설정
persist-credentials: false

- name: Fetch organization members
id: fetch-members
- name: Fetch repository collaborators
id: fetch-collaborators
run: |
RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/orgs/Wannabe-Woowa-Article/members")
ORG_MEMBERS=$(echo "$RESPONSE" | jq -r '.[].login' | tr '\n' ' ')
echo "ORG_MEMBERS=$ORG_MEMBERS" >> $GITHUB_ENV
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 assignees
id: fetch-pr-assignees
- name: Fetch existing PR reviewers
id: fetch-pr-reviewers
run: |
RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
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")
EXISTING_ASSIGNEES=$(echo "$RESPONSE" | jq -r '.[].assignees[].login' | sort | uniq | tr '\n' ' ')
echo "EXISTING_ASSIGNEES=$EXISTING_ASSIGNEES" >> $GITHUB_ENV
ALL_REVIEWERS=$(echo "$RESPONSE" | jq -r '.[].requested_reviewers | .[].login' | sort | uniq | tr '\n' ' ')
echo "ALL_REVIEWERS=$ALL_REVIEWERS" >> $GITHUB_ENV

- name: Select random member
- name: Select random collaborator
id: select-random
run: |
# Convert organization members to array
ORG_MEMBERS_ARRAY=($ORG_MEMBERS)
# Convert collaborators to array
COLLABORATORS_ARRAY=($COLLABORATORS)

# Convert existing assignees to array
EXISTING_ASSIGNEES_ARRAY=($EXISTING_ASSIGNEES)
# Convert existing reviewers to array
EXISTING_REVIEWERS_ARRAY=($EXISTING_REVIEWERS)

# Remove PR author from organization members
# 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_MEMBERS=()
for member in "${ORG_MEMBERS_ARRAY[@]}"; do
if [[ "$member" != "$PR_AUTHOR" && ! " ${EXISTING_ASSIGNEES_ARRAY[@]} " =~ " ${member} " ]]; then
FILTERED_MEMBERS+=($member)
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 members left
if [ ${#FILTERED_MEMBERS[@]} -eq 0 ]; then
echo "No eligible members to assign"
# 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 member from filtered members
RANDOM_MEMBER=${FILTERED_MEMBERS[$RANDOM % ${#FILTERED_MEMBERS[@]}]}
echo "SELECTED_MEMBER=$RANDOM_MEMBER" >> $GITHUB_ENV
# 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 selected member
- 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.GITHUB_TOKEN }}" \
-d '{"assignees":["${{ env.SELECTED_MEMBER }}"]}' \
-H "Authorization: token ${{ secrets.PAT }}" \
-d '{"assignees":["'$PR_AUTHOR'"]}' \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/assignees"
1 change: 1 addition & 0 deletions actions-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
action test!
Loading