---
name: Request Reviewers

on:
  pull_request_target:
    types: [opened, ready_for_review, reopened, synchronize]
    paths:
      - 'modules/**'

defaults:
  run:
    shell: bash

jobs:
  request-reviewers:
    runs-on: ubuntu-24.04
    if: vars.APP_ID
    steps:
      - uses: actions/checkout@v6
        with:
          ref: ${{ github.base_ref }}

      - uses: cachix/install-nix-action@v31

      - uses: cachix/cachix-action@v17
        with:
          name: nix-community
          authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
        continue-on-error: true

      - uses: actions/create-github-app-token@v3
        id: app-token
        with:
          app-id: ${{ vars.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}
          permission-members: read
          permission-contents: read
          permission-pull-requests: write

      - run: nix build .#subsystem-maintainers

      - id: get-reviewers
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          MAX_REVIEWERS: 5
          PR_AUTHOR: ${{ github.event.pull_request.user.login }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
        run: |
          printf \
            'reviewers=%s\n' \
            "$(
              gh pr diff --name-only "$PR_NUMBER" |
                jq \
                  --arg pr_author "$PR_AUTHOR" \
                  --argjson max_reviewers "$MAX_REVIEWERS" \
                  --raw-input \
                  --raw-output \
                  --slurp \
                  --slurpfile maintainers result '
                    ($maintainers[0] | keys_unsorted) as $subsystems |
                    split("\n") as $changed_files |
                    [
                      $changed_files[] as $changed_file |
                      (
                        $maintainers[0][
                          $subsystems[] |
                          select(
                            . as $subsystem |
                            $changed_file |
                            startswith($subsystem)
                          )
                        ]
                      )[]
                    ] |
                    unique |
                    map(select(. != $pr_author)) |
                    if length <= $max_reviewers then
                      . | join(",")
                    else
                      ""
                    end
                  '
            )" \
            >>"$GITHUB_OUTPUT"

      - uses: AveryCameronUofR/add-reviewer-gh-action@1.0.4
        with:
          token: ${{ steps.app-token.outputs.token }}
          reviewers: ${{ steps.get-reviewers.outputs.reviewers }}
