name: Generate changelog
on:
  workflow_call:
    inputs:
      previous_version:
        type: string
        required: false
        description: 'The release version (semver, git tag, branch or commit) to use for comparison'
      version:
        type: string
        required: true
        description: 'Target release version (semver, git tag, branch or commit)'
      target:
        required: true
        type: string
        description: 'The base branch that these changes are being merged into'
      dry_run:
        required: false
        default: false
        type: boolean
      latest:
        required: false
        default: false
        type: boolean
    secrets:
      GRAFANA_DELIVERY_BOT_APP_ID:
        required: true
      GRAFANA_DELIVERY_BOT_APP_PEM:
        required: true

  workflow_dispatch:
    inputs:
      previous_version:
        type: string
        required: false
        description: 'The release version (semver, git tag, branch or commit) to use for comparison'
      version:
        type: string
        required: true
        description: 'Target release version (semver, git tag, branch or commit)'
      target:
        required: true
        type: string
        description: 'The base branch that these changes are being merged into'
      dry_run:
        required: false
        default: false
        type: boolean
      latest:
        required: false
        default: false
        type: boolean

permissions:
  contents: write
  pull-requests: write

jobs:
  main:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: "Generate token"
        id: generate_token
        uses: tibdex/github-app-token@b62528385c34dbc9f38e5f4225ac829252d1ea92
        with:
          app_id: ${{ secrets.GRAFANA_DELIVERY_BOT_APP_ID }}
          private_key: ${{ secrets.GRAFANA_DELIVERY_BOT_APP_PEM }}
      - name: "Checkout Grafana repo"
        uses: "actions/checkout@v4"
        with:
          ref: main
          sparse-checkout: |
            .github/workflows
            CHANGELOG.md
            .nvmrc
            .prettierignore
            .prettierrc.js
          fetch-depth: 0
          fetch-tags: true
      - name: Setup nodejs environment
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
      - name: "Configure git user"
        run: |
          git config --local user.name "github-actions[bot]"
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local --add --bool push.autoSetupRemote true
      - name: "Create branch"
        run: git checkout -b "changelog/${{ github.run_id }}/${{ inputs.version }}"
      - name: "Generate changelog"
        id: changelog
        uses: ./.github/workflows/actions/changelog
        with:
          previous: ${{ inputs.previous_version }}
          github_token: ${{ steps.generate_token.outputs.token }}
          target: v${{ inputs.version }}
          output_file: changelog_items.md
      - name: "Patch CHANGELOG.md"
        run: |
          # Prepare CHANGELOG.md content with version delimiters
          (
            echo
            echo "# ${{ inputs.version}} ($(date '+%F'))"
            echo
            cat changelog_items.md
          ) > CHANGELOG.part

          # Check if a version exists in the changelog
          if grep -q "<!-- ${{ inputs.version}} START" CHANGELOG.md ; then
            # Replace the content between START and END delimiters
            echo "Version ${{ inputs.version }} is found in the CHANGELOG.md, patching contents..."
            sed -i -e '/${{ inputs.version }} START/,/${{ inputs.version }} END/{//!d;}' \
                   -e '/${{ inputs.version }} START/r CHANGELOG.part' CHANGELOG.md
          else
            # Prepend changelog part to the main changelog file
            echo "Version ${{ inputs.version }} not found in the CHANGELOG.md"
            (
              echo "<!-- ${{ inputs.version }} START -->"
              cat CHANGELOG.part
              echo "<!-- ${{ inputs.version }} END -->"
              cat CHANGELOG.md
            ) > CHANGELOG.tmp
            mv CHANGELOG.tmp CHANGELOG.md
          fi

          git diff CHANGELOG.md

      - name: "Prettify CHANGELOG.md"
        run: npx prettier --write CHANGELOG.md
      - name: "Commit changelog changes"
        run: git add CHANGELOG.md && git commit --allow-empty -m "Update changelog" CHANGELOG.md
      - name: "git push"
        if: ${{ inputs.dry_run }} != true
        run: git push
      - name: "Create changelog PR"
        run: >
          gh pr create \
            --dry-run=${{ inputs.dry_run }} \
            --label "no-backport" \
            --label "no-changelog" \
            -B "${{ inputs.target }}" \
            --title "Release: update changelog for ${{ inputs.version }}" \
            --body "Changelog changes for release ${{ inputs.version }}"
        env:
          GH_TOKEN: ${{ steps.generate_token.outputs.token }}
