name: "Preview on downstream repo"
on:
  workflow_call:
    inputs:
      repository:
        required: true
        type: string
      directory:
        required: false
        type: string
        default: .
      cleanup:
        required: true
        type: boolean

# Cancel any in-progress runs of a caller workflow on the same pull request.
concurrency:
  group: ${{ github.workflow }}-${{ github.event.number }}
  cancel-in-progress: true

env:
  branch: nextstrain-bot/test-auspice-pr/${{ github.event.number }}

jobs:
  sync:
    name: Create or update downstream PR
    if: ${{ !inputs.cleanup }}
    runs-on: ubuntu-latest

    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Checkout downstream repo
        uses: actions/checkout@v4
        with:
          repository: ${{ inputs.repository }}
          token: ${{ secrets.GH_TOKEN_NEXTSTRAIN_BOT_REPO }}

      - name: Install Auspice from PRs HEAD commit
        shell: bash
        working-directory: ${{ inputs.directory }}
        env:
          NODE_ENV: production
        run: |
          npm config set ignore-scripts true
          npm ci
          npm install nextstrain/auspice#${{ github.sha }}
          git add package.json package-lock.json

          git config user.name "${{ vars.GIT_USER_NAME_NEXTSTRAIN_BOT }}"
          git config user.email "${{ vars.GIT_USER_EMAIL_NEXTSTRAIN_BOT }}"
          git commit --message="[testing only] Upgrade Auspice to ${{ github.sha }}"

      - name: Create Pull Request
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN_NEXTSTRAIN_BOT_REPO }}
          title: '[bot] [DO NOT MERGE] Test Auspice PR ${{ github.event.number }}: ${{ github.event.pull_request.title }}'
          body: |
            This PR has been created to test this project running Auspice with changes from https://github.com/nextstrain/auspice/pull/${{ github.event.number }}.

            Note that Auspice has been installed with changes from both the PR's source and target branches.
            This will surface any issues that can arise from merging the PR in Auspice. To address these issues locally, update the source branch (e.g. with a git rebase).

            This message PR is maintained by a [GitHub Actions workflow](https://github.com/nextstrain/auspice/blob/-/.github/workflows/preview_on_downstream_repo.yaml).
            It will be automatically kept up to date and closed when the upstream PR has been merged or closed.
        run: |
          git switch -c "$branch"
          git push --force origin HEAD
          pr_url=$(gh pr list --head "$branch" --json url | jq -r '.[0].url')

          if [ "$pr_url" = "null" ]; then
            pr_url="$(gh pr create --head "$branch" --title "$title" --body "$body" --draft)"
            echo "Pull request created: $pr_url" >> "$GITHUB_STEP_SUMMARY"
          else
            echo "Pull request updated: $pr_url" >> "$GITHUB_STEP_SUMMARY"
          fi

  cleanup:
    name: Cleanup downstream PR
    if: inputs.cleanup
    runs-on: ubuntu-latest

    steps:
      - name: Checkout downstream repo
        uses: actions/checkout@v4
        with:
          repository: ${{ inputs.repository }}
          token: ${{ secrets.GH_TOKEN_NEXTSTRAIN_BOT_REPO }}

      - env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN_NEXTSTRAIN_BOT_REPO }}
        run: |
          gh pr close "$branch"
          git push --delete origin "$branch"
