name: Bump Manifest Version

on:
  push:
    branches:
      - main
      - dev

permissions:
  contents: write
  pull-requests: read

jobs:
  bump-manifest-version:
    if: ${{ github.actor != 'github-actions[bot]' && !contains(github.event.head_commit.message, '[skip ci]') }}
    runs-on: ubuntu-latest

    steps:
      - name: Detect merged pull request push
        id: pr-check
        uses: actions/github-script@v7
        with:
          script: |
            const { owner, repo } = context.repo;
            const sha = context.sha;
            const response = await github.rest.repos.listPullRequestsAssociatedWithCommit({
              owner,
              repo,
              commit_sha: sha,
            });
            const mergedPr = (response.data || []).find(
              (pr) => pr?.merged_at && pr?.base?.ref === context.ref.replace('refs/heads/', ''),
            );
            const shouldSkip = Boolean(mergedPr);
            core.setOutput('skip', shouldSkip ? 'true' : 'false');
            if (mergedPr) {
              core.notice(`Skip manifest bump for merged PR #${mergedPr.number}: ${mergedPr.title}`);
            }

      - name: Checkout
        if: ${{ steps.pr-check.outputs.skip != 'true' }}
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Node.js
        if: ${{ steps.pr-check.outputs.skip != 'true' }}
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Bump manifest version
        if: ${{ steps.pr-check.outputs.skip != 'true' }}
        run: node scripts/bump-manifest-version.mjs

      - name: Commit version bump
        if: ${{ steps.pr-check.outputs.skip != 'true' }}
        run: |
          if git diff --quiet -- manifest.json; then
            echo "manifest.json version did not change."
            exit 0
          fi
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add manifest.json
          git commit -m "chore: bump manifest version [skip ci]"
          git push
