diff --git a/.github/workflows/create-tag-and-exit.yml b/.github/workflows/create-tag-and-exit.yml index 50b77600..7c2ac35a 100644 --- a/.github/workflows/create-tag-and-exit.yml +++ b/.github/workflows/create-tag-and-exit.yml @@ -9,7 +9,6 @@ on: permissions: contents: write - pull-requests: write jobs: tag: @@ -46,58 +45,3 @@ jobs: --generate-notes \ --title "${{ steps.version.outputs.tag }}" fi - - - name: Update CHANGELOG.md from releases - env: - GH_TOKEN: ${{ github.token }} - run: | - # Generate changelog header (no indentation to avoid leading spaces) - printf '%s\n' \ - "# Changelog" \ - "" \ - "All notable changes to this project will be documented in this file." \ - "The format is based on [Keep a Changelog](https://keepachangelog.com/)." \ - "" \ - "For releases prior to automated changelog generation, please see the" \ - "[GitHub Releases](https://github.com/${{ github.repository }}/releases) page." \ - "" > CHANGELOG.md - - # Append all releases using JSON for reliable parsing (paginate to include all) - gh api --paginate repos/${{ github.repository }}/releases?per_page=100 \ - --jq 'map({tagName: .tag_name, publishedAt: .published_at})' | jq -r '.[] | "\(.tagName) \(.publishedAt)"' | while read -r tag published_at; do - formatted_date=$(date -d "$published_at" +%Y-%m-%d 2>/dev/null || echo "$published_at") - echo "## [$tag] - $formatted_date" - echo "" - # Transform top-level headings (## ) to subheadings (### ) for changelog nesting. - # Uses negative lookahead pattern to avoid double-transforming already-nested headings. - gh release view "$tag" --json body -q '.body' | sed 's/^##\([^#]\)/###\1/' - echo "" - done >> CHANGELOG.md - - - name: Create PR for CHANGELOG.md update - env: - GH_TOKEN: ${{ github.token }} - run: | - git config user.name "github-actions" - git config user.email "github-actions@github.com" - git add CHANGELOG.md - if git diff --staged --quiet; then - echo "No changelog changes to commit" - exit 0 - fi - - BRANCH_NAME="chore/update-changelog-${{ steps.version.outputs.tag }}" - git checkout -B "$BRANCH_NAME" - git commit -m "chore: update changelog for ${{ steps.version.outputs.tag }}" - git push --force-with-lease origin "$BRANCH_NAME" - - # Create PR only if one doesn't already exist for this branch - if gh pr view "$BRANCH_NAME" --json state -q '.state' 2>/dev/null | grep -q "OPEN"; then - echo "PR already exists for $BRANCH_NAME; skipping creation." - else - gh pr create \ - --title "chore: update changelog for ${{ steps.version.outputs.tag }}" \ - --body "Automated changelog update for release ${{ steps.version.outputs.tag }}" \ - --base main \ - --head "$BRANCH_NAME" - fi diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml new file mode 100644 index 00000000..15cdc841 --- /dev/null +++ b/.github/workflows/update-changelog.yml @@ -0,0 +1,74 @@ +name: Update Changelog + +on: + release: + types: [published] + +permissions: + contents: write + pull-requests: write + +jobs: + update-changelog: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Update CHANGELOG.md from releases + env: + GH_TOKEN: ${{ github.token }} + run: | + # Generate changelog header (no indentation to avoid leading spaces) + printf '%s\n' \ + "# Changelog" \ + "" \ + "All notable changes to this project will be documented in this file." \ + "The format is based on [Keep a Changelog](https://keepachangelog.com/)." \ + "" \ + "For releases prior to automated changelog generation, please see the" \ + "[GitHub Releases](https://github.com/${{ github.repository }}/releases) page." \ + "" > CHANGELOG.md + + # Append all releases using JSON for reliable parsing (paginate to include all) + gh api --paginate repos/${{ github.repository }}/releases?per_page=100 \ + | jq -s -r 'add | map({tagName: .tag_name, publishedAt: .published_at}) | .[] | "\(.tagName) \(.publishedAt)"' \ + | while read -r tag published_at; do + formatted_date=$(date -d "$published_at" +%Y-%m-%d 2>/dev/null || echo "$published_at") + echo "## [$tag] - $formatted_date" + echo "" + # Transform top-level headings (## ) to subheadings (### ) for changelog nesting. + # Uses negative lookahead pattern to avoid double-transforming already-nested headings. + gh release view "$tag" --json body -q '.body' | sed 's/^##\([^#]\)/###\1/' + echo "" + done >> CHANGELOG.md + + - name: Create PR for CHANGELOG.md update + env: + GH_TOKEN: ${{ github.token }} + run: | + git config user.name "github-actions" + git config user.email "github-actions@github.com" + git add CHANGELOG.md + if git diff --staged --quiet; then + echo "No changelog changes to commit" + exit 0 + fi + + BRANCH_NAME="chore/update-changelog-${{ github.event.release.tag_name }}" + git checkout -B "$BRANCH_NAME" + git commit -m "chore: update changelog for ${{ github.event.release.tag_name }}" + git push --force-with-lease origin "$BRANCH_NAME" + + # Create PR only if one doesn't already exist for this branch + if gh pr view "$BRANCH_NAME" --json state -q '.state' 2>/dev/null | grep -q "OPEN"; then + echo "PR already exists for $BRANCH_NAME; skipping creation." + else + gh pr create \ + --title "chore: update changelog for ${{ github.event.release.tag_name }}" \ + --body "Automated changelog update for release ${{ github.event.release.tag_name }}" \ + --base main \ + --head "$BRANCH_NAME" + fi