name: equivalence-test-update

on:
  pull_request_target:
    types: [ closed ]

permissions:
  contents: write
  pull-requests: write

jobs:
  check:
    name: "Should run equivalence tests?"
    runs-on: ubuntu-latest
    outputs:
        should_run: ${{ steps.target_branch.outputs.should_run }}
    steps:
      - name: target_branch
        id: target_branch
        run: |
          merged='${{ github.event.pull_request.merged }}'
          target_branch='${{ github.event.pull_request.base.ref }}'

          targets_release_branch=false
          if [ "$target_branch" == "main" ]; then
            targets_release_branch=true
          elif [ "$target_branch" =~ ^v[0-9]+\.[0-9]+$ ]; then
            targets_release_branch=true
          fi

          should_run=false
          if [ "$merged" == "true" ] && [ "$targets_release_branch" == "true" ]; then
            should_run=true
          fi

          echo "should_run=$should_run" >> ${GITHUB_OUTPUT}
  run-equivalence-tests:
    name: "Run equivalence tests"
    needs:
      - check
    if: needs.check.outputs.should_run == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: ${{ inputs.target-branch }}

      - name: Determine Go version
        id: go
        uses: ./.github/actions/go-version

      - name: Install Go toolchain
        uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
        with:
          go-version: ${{ steps.go.outputs.version }}
          cache-dependency-path: go.sum

      - uses: ./.github/actions/equivalence-test
        with:
          target-equivalence-test-version: 0.5.0
          target-os: linux
          target-arch: amd64
          current-branch: ${{ github.event.pull_request.base.ref }}
          new-branch: equivalence-testing/${{ github.event.pull_request.head.ref }}
          reviewers: ${{ github.event.pull_request.merged_by.login }}
          message: "Update equivalence test golden files after ${{ github.event.pull_request.html_url }}."
          github-token: ${{ github.token }}
