name: Assign PR to author and reviewers

# This workflow runs on the pull_request_target event:
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target.
#
# So the secrets are accessible even if the PR was opened from an external
# repository. This is done because the GitHub API key needs to be accessed
# to modify the PRs.
#
# NOTE: Only API calls should be made in the actions defined here. The
#       committed code should _NOT_ be touched in any case.

"on":
  pull_request_target:
    types: [ opened, reopened, ready_for_review ]

jobs:

  assign-pr:
    name: Assign PR to author
    runs-on: ubuntu-latest
    steps:
      - uses: toshimaru/auto-author-assign@v2.1.0

  ask-review:
    name: Run pull-review
    runs-on: ubuntu-latest
    if: ${{ !github.event.pull_request.draft && github.event.pull_request.base.ref == 'main' }}
    steps:
      - uses: actions/checkout@v4
      - name: Run pull-review with docker
        env:
            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            OWNER: ${{ github.repository_owner }}
            REPO: ${{ github.event.repository.name }}
            PULL_REQUEST_NUMBER : ${{ github.event.pull_request.number }}
        shell: bash
        run: |
          PR_URL="https://github.com/${OWNER}/${REPO}/pull/${PULL_REQUEST_NUMBER}"
          docker run ghcr.io/imsky/pull-review "$PR_URL" --github-token "${GITHUB_TOKEN}"
