name: diff-shades-comment

on:
  workflow_run:
    workflows: [diff-shades]
    types: [completed]

permissions: {}

jobs:
  comment:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          persist-credentials: false
      - uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "3.13"

      - name: Install support dependencies
        run: |
          python -m pip install pip --upgrade
          python -m pip install click packaging urllib3

      - name: Get details from initial workflow run
        id: metadata
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: >
          python scripts/diff_shades_gha_helper.py comment-details
          ${{github.event.workflow_run.id }}

      - name: Try to find pre-existing PR comment
        if: steps.metadata.outputs.needs-comment == 'true'
        id: find-comment
        uses: peter-evans/find-comment@b30e6a3c0ed37e7c023ccd3f1db5c6c0b0c23aad
        with:
          issue-number: ${{ steps.metadata.outputs.pr-number }}
          comment-author: "github-actions[bot]"
          body-includes: "diff-shades"

      - name: Create or update PR comment
        if: steps.metadata.outputs.needs-comment == 'true'
        uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9
        with:
          comment-id: ${{ steps.find-comment.outputs.comment-id }}
          issue-number: ${{ steps.metadata.outputs.pr-number }}
          body: ${{ steps.metadata.outputs.comment-body }}
          edit-mode: replace
