name: Auto-update spec repo links
on:
  schedule:
    # hourly at minute 46
    - cron: "46 * * * *"
  workflow_dispatch:

permissions:
  contents: read

jobs:
  check-versions:
    runs-on: ubuntu-latest
    if: github.repository_owner == 'open-telemetry'
    outputs:
      current-version: ${{ steps.check-versions.outputs.current-version }}
      latest-version: ${{ steps.check-versions.outputs.latest-version }}
      already-opened: ${{ steps.check-versions.outputs.already-opened }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - id: check-versions
        name: Check versions
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          current_version=$(grep "PREVIOUS_SPECIFICATION_VERSION=v.*" \
                                 .github/scripts/update-spec-repo-links.sh \
                            | sed "s/PREVIOUS_SPECIFICATION_VERSION=//")
          latest_version=$(gh release view \
                               --repo open-telemetry/opentelemetry-specification \
                               --json tagName \
                               --jq .tagName)

          matches=$(gh pr list \
                        --author otelbot[bot] \
                        --state open \
                        --search "in:title \"Update spec repo links to $latest_version\"")
          if [ ! -z "$matches" ]
          then
            already_opened=true
          fi

          echo "current-version=$current_version" >> $GITHUB_OUTPUT
          echo "latest-version=$latest_version" >> $GITHUB_OUTPUT
          echo "already-opened=$already_opened" >> $GITHUB_OUTPUT

  update-spec-repo-links:
    permissions:
      contents: write # required for pushing changes
    runs-on: ubuntu-latest
    if: |
      needs.check-versions.outputs.current-version != needs.check-versions.outputs.latest-version &&
      needs.check-versions.outputs.already-opened != 'true'
    needs:
      - check-versions
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Use CLA approved github bot
        run: .github/scripts/use-cla-approved-github-bot.sh

      - name: Update version
        env:
          VERSION: ${{ needs.check-versions.outputs.latest-version }}
        run: |
          .github/scripts/update-spec-repo-links.sh $VERSION
          sed -i "s/^PREVIOUS_SPECIFICATION_VERSION=.*/PREVIOUS_SPECIFICATION_VERSION=$VERSION/" .github/scripts/update-spec-repo-links.sh

      - uses: actions/create-github-app-token@a8d616148505b5069dccd32f177bb87d7f39123b # v2.1.1
        id: otelbot-token
        with:
          app-id: ${{ vars.OTELBOT_APP_ID }}
          private-key: ${{ secrets.OTELBOT_PRIVATE_KEY }}

      - name: Create pull request
        env:
          VERSION: ${{ needs.check-versions.outputs.latest-version }}
          # not using secrets.GITHUB_TOKEN since pull requests from that token do not run workflows
          GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
        run: |
          message="Update spec repo links to $VERSION"
          body="Update spec repo links to \`$VERSION\`."
          branch="otelbot/update-spec-repo-links-to-$VERSION"

          git checkout -b $branch
          git commit -a -m "$message"
          git push --set-upstream origin $branch
          gh pr create --title "[chore] $message" \
                       --body "$body" \
                       --base main
