name: Release Blog Scaffold

# Fires on every v* tag push. Computes whether this tag completes the next
# every-5th-release window (source of truth = the latest post with a version:
# field in the sencho-website repo). If yes, generates a complete, TODO-free
# blog post and commits it directly to sencho-website main. No PR is opened.
on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag to scaffold against (e.g. v0.58.0). Must already exist.'
        required: true
        type: string

permissions:
  contents: read

jobs:
  scaffold:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      # Same App-token pattern as release-please.yml. The app's installation
      # must include both Sencho and sencho-website for the push + PR to work.
      - name: Generate GitHub App installation token
        id: app-token
        uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
        with:
          app-id: ${{ secrets.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}
          owner: ${{ github.repository_owner }}
          repositories: |
            Sencho
            sencho-website
          permission-contents: write

      - name: Resolve target tag
        id: target
        run: |
          set -euo pipefail
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            tag="${{ inputs.tag }}"
          else
            tag="${GITHUB_REF_NAME}"
          fi
          echo "tag=${tag}" >> "$GITHUB_OUTPUT"
          echo "Resolved target tag: ${tag}"

      - name: Check out Sencho (full history for tags)
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: Check out sencho-website
        uses: actions/checkout@v6
        with:
          repository: ${{ github.repository_owner }}/sencho-website
          token: ${{ steps.app-token.outputs.token }}
          path: website-repo
          fetch-depth: 1

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: '20'

      - name: Run scaffold script
        id: scaffold
        run: |
          set -euo pipefail
          node .github/scripts/scaffold-release-post.mjs \
            --tag "${{ steps.target.outputs.tag }}" \
            --changelog CHANGELOG.md \
            --website website-repo

      - name: Commit and push release post to sencho-website
        if: steps.scaffold.outputs.scaffold == 'true'
        env:
          TAG: ${{ steps.target.outputs.tag }}
          POST_PATH: ${{ steps.scaffold.outputs.post_path }}
        run: |
          set -euo pipefail
          cd website-repo
          git config user.name  "sencho-release-bot[bot]"
          git config user.email "sencho-release-bot[bot]@users.noreply.github.com"
          git add "${POST_PATH}" src/data/blog/index.ts src/data/blog/meta.ts
          git commit -m "chore(blog): auto-publish release post for ${TAG}"
          git push origin main

      - name: Report skip
        if: steps.scaffold.outputs.scaffold != 'true'
        run: |
          echo "No scaffold this run. Window did not reach the next every-5th-release anchor."
