name: API Changelog Monitor

on:
  schedule:
    - cron: "0 6 * * *" # daily 06:00 UTC
  workflow_dispatch: # manual trigger

concurrency:
  group: api-changelog
  cancel-in-progress: false

permissions:
  contents: write
  issues: write

jobs:
  check-changelogs:
    name: Check provider changelogs
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - name: Check changelogs for breaking keywords
        id: check
        run: |
          chmod +x scripts/check-api-changelogs.sh
          set +e
          output="$(bash scripts/check-api-changelogs.sh --update 2>&1)"
          exit_code=$?
          set -e

          echo "$output"

          # Use a random delimiter to prevent injection from external content
          delimiter="EOF_$(openssl rand -hex 16)"
          {
            echo "report<<$delimiter"
            echo "$output"
            echo "$delimiter"
          } >> "$GITHUB_OUTPUT"

          if [[ $exit_code -eq 1 ]]; then
            echo "has_matches=true" >> "$GITHUB_OUTPUT"
          else
            echo "has_matches=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Commit updated state file
        if: steps.check.outputs.has_matches == 'true'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add tests/api_contracts/.changelog-state.json
          git diff --cached --quiet && exit 0
          git commit -m "chore: update changelog monitor state"
          git push origin HEAD:${{ github.ref_name }}

      - name: Check for existing open issue
        if: steps.check.outputs.has_matches == 'true'
        id: existing
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          issue_number=$(gh issue list \
            --repo "${{ github.repository }}" \
            --label api-changelog \
            --state open \
            --limit 1 \
            --json number \
            --jq '.[0].number // empty')
          if [[ -n "$issue_number" ]]; then
            echo "exists=true" >> "$GITHUB_OUTPUT"
            echo "number=$issue_number" >> "$GITHUB_OUTPUT"
          else
            echo "exists=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Create issue for new matches
        if: steps.check.outputs.has_matches == 'true' && steps.existing.outputs.exists == 'false'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPORT: ${{ steps.check.outputs.report }}
        run: |
          body="## API Changelog Monitor

          The daily changelog scan found new keyword matches that may indicate upcoming API changes.

          ### Matches

          \`\`\`
          $REPORT
          \`\`\`

          _Generated by [api-changelog.yml](../../.github/workflows/api-changelog.yml)_"

          gh issue create \
            --repo "${{ github.repository }}" \
            --title "API changelog alert: potential breaking changes detected" \
            --label "api-changelog" \
            --body "$body"

      - name: Comment on existing issue
        if: steps.check.outputs.has_matches == 'true' && steps.existing.outputs.exists == 'true'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPORT: ${{ steps.check.outputs.report }}
          ISSUE_NUMBER: ${{ steps.existing.outputs.number }}
        run: |
          gh issue comment "$ISSUE_NUMBER" \
            --repo "${{ github.repository }}" \
            --body "## New matches detected

          \`\`\`
          $REPORT
          \`\`\`"
