-
Notifications
You must be signed in to change notification settings - Fork 162
80 lines (76 loc) · 3.35 KB
/
request_codeowners_review.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
---
name: Request Code Owners Review
on:
pull_request_target:
types: ["opened", "synchronize"]
jobs:
auto_request_review:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.base_ref }}
fetch-depth: 0
- name: Fetch the PR's head ref
run: |
git fetch origin ${{ github.event.pull_request.head.sha }}:${{ github.event.pull_request.head.ref }}
git checkout ${{ github.event.pull_request.head.ref }}
- name: Auto request review
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
reviewers=""
# Get changed files in this pull request
echo "Checking base: ${{ github.event.pull_request.base.sha }}"
echo "Checking head: ${{ github.event.pull_request.head.sha }}"
for changed_file in $(git diff --name-only ${{ github.event.pull_request.base.sha }}...HEAD); do
echo "Checking $changed_file"
while read -r line; do
# Get pattern of the files owned
pattern=$(echo "$line" | awk '{print $1}')
# Remove leading / from pattern and changed_file for comparison
pattern="${pattern#/}"
# Get owners of the files owned
owners=$(echo "$line" | awk '{$1=""; print $0}' | xargs)
# Remove leading / from changed_file for comparison
changed_file="${changed_file#/}"
if [[ "$changed_file" == $pattern* ]]; then
# Add owners to reviewers
echo "Found owners for $changed_file: $owners"
reviewers="$reviewers $owners"
fi
done < .github/CODEOWNERS
done
# Remove duplicates and format for JSON
reviewers=$(echo "$reviewers" | xargs -n1 | sort -u | xargs)
# Remove @ from reviewers (it stays in the beginning of the username)
reviewers_cleaned=${reviewers//@/}
IFS=' ' read -ra reviewers_array <<< "$reviewers_cleaned"
# Get username of PR author to exclude from reviewers
pr_author="${{ github.event.pull_request.user.login }}"
# Remove PR author from reviewers
filtered_reviewers=()
for reviewer in ${reviewers_array[@]}; do
if [[ "$reviewer" != "$pr_author" ]]; then
filtered_reviewers+=("$reviewer")
fi
done
if [ ${#filtered_reviewers[@]} -gt 0 ]; then
json_array=$(printf ',\"%s\"' "${filtered_reviewers[@]}")
json_array="[${json_array:1}]" # Remove the leading comma
echo "JSON array: $json_array"
echo "Requesting review from: ${filtered_reviewers[*]}"
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" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers \
-d "{\"reviewers\": $json_array}"
else
echo "No reviewers found for the changed files."
fi