|
| 1 | +# Adds reviewers upon publishing a PR based on changed files. |
| 2 | +# Works around limitations of GitHub's CODEOWNERS, which can only match one rule, |
| 3 | +# whereas our changes can straddle frontend and backend, for example. |
| 4 | +name: Add reviewers |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + types: [opened, ready_for_review, reopened] |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ${{ github.workflow }}-${{ github.head_ref }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + add-reviewers: |
| 20 | + name: Add reviewers |
| 21 | + runs-on: ubuntu-latest |
| 22 | + # Let tristanlabelle validate this workflow before switching from CODEOWNERS |
| 23 | + if: github.event.pull_request.user.login == 'tristanlabelle' && github.actor != 'dependabot[bot]' && !github.event.pull_request.draft |
| 24 | + steps: |
| 25 | + - uses: dorny/paths-filter@v4 |
| 26 | + id: filter |
| 27 | + with: |
| 28 | + filters: .github/autoreviewers.yml |
| 29 | + |
| 30 | + - name: Add reviewers |
| 31 | + env: |
| 32 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + GH_REPO: ${{ github.repository }} |
| 34 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 35 | + PR_AUTHOR: ${{ github.event.pull_request.user.login }} |
| 36 | + # The "changes" output is a JSON array of filter names. |
| 37 | + FILTER_CHANGES: ${{ steps.filter.outputs.changes }} |
| 38 | + run: | |
| 39 | + # Filter keys are "<name> <reviewer1,reviewer2,...>"; extract reviewers from matched filters. |
| 40 | + MATCHED_REVIEWERS=$(echo "$FILTER_CHANGES" | jq -r '.[] | split(" ")[1] | split(",") | .[]') |
| 41 | +
|
| 42 | + ARGS=() |
| 43 | + while IFS= read -r reviewer; do |
| 44 | + # Ignore empty entries |
| 45 | + [[ -n "$reviewer" ]] || continue |
| 46 | + # The PR author cannot be a reviewer on their own PR |
| 47 | + [[ "$reviewer" == "$PR_AUTHOR" ]] && continue |
| 48 | +
|
| 49 | + echo "Adding reviewer: $reviewer" |
| 50 | + ARGS+=(--add-reviewer "$reviewer") |
| 51 | + done <<< "$MATCHED_REVIEWERS" |
| 52 | +
|
| 53 | + if [[ ${#ARGS[@]} -gt 0 ]]; then |
| 54 | + gh pr edit "$PR_NUMBER" "${ARGS[@]}" |
| 55 | + fi |
0 commit comments