name: Update Textual snapshots from a comment
on:
  issue_comment:
    types: [created]

jobs:
  update-snapshots:
    # Only run if comment is on a PR with the main repo, and if it contains the magic keywords
    if: >
      contains(github.event.comment.html_url, '/pull/') &&
      contains(github.event.comment.body, '@nf-core-bot update snapshots') &&
      github.repository == 'nf-core/tools'
    runs-on: ubuntu-latest
    steps:
      # Use the @nf-core-bot token to check out so we can push later
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        with:
          token: ${{ secrets.nf_core_bot_auth_token }}

      # indication that the command is running
      - name: React on comment
        uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4
        with:
          comment-id: ${{ github.event.comment.id }}
          reactions: eyes

      # Action runs on the issue comment, so we don't get the PR by default
      # Use the gh cli to check out the PR
      - name: Checkout Pull Request
        run: gh pr checkout ${{ github.event.issue.number }}
        env:
          GITHUB_TOKEN: ${{ secrets.nf_core_bot_auth_token }}

      # Install dependencies and run pytest
      - name: Set up Python
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
        with:
          python-version: "3.13"
          cache: "pip"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip -r requirements-dev.txt
          pip install -e .

      - name: Run pytest to update snapshots
        id: pytest
        run: |
          python3 -m pytest tests/pipelines/test_create_app.py --snapshot-update --color=yes  --durations=0
        continue-on-error: true

      # indication that the run has finished
      - name: react if finished succesfully
        if: steps.pytest.outcome == 'success'
        uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4
        with:
          comment-id: ${{ github.event.comment.id }}
          reactions: "+1"

      - name: Commit & push changes
        id: commit-and-push
        if: steps.pytest.outcome == 'failure'
        run: |
          git config user.email "core@nf-co.re"
          git config user.name "nf-core-bot"
          git config push.default upstream
          git add .
          git status
          git commit -m "[automated] Update Textual snapshots"
          git push

      - name: react if snapshots were updated
        id: react-if-updated
        if: steps.commit-and-push.outcome == 'success'
        uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4
        with:
          comment-id: ${{ github.event.comment.id }}
          reactions: hooray

      - name: react if snapshots were not updated
        if: steps.commit-and-push.outcome == 'failure'
        uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4
        with:
          comment-id: ${{ github.event.comment.id }}
          reactions: confused

      - name: react if snapshots were not updated
        if: steps.commit-and-push.outcome  == 'failure'
        uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4
        with:
          issue-number: ${{ github.event.issue.number }}
          body: |
            @${{ github.actor }} I tried to update the snapshots, but it didn't work. Please update them manually.
