Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 29 additions & 50 deletions .github/workflows/write-changelog.yml
Original file line number Diff line number Diff line change
@@ -1,56 +1,34 @@
name: Update Changelog on merge to main (append)
name: Update Changelog on PR (write to PR branch)

on:
push:
branches:
- main

permissions:
contents: write
pull-requests: read
pull_request:
types: [opened, edited, synchronize, reopened]

jobs:
update-changelog:
if: >
github.event.pull_request.merged != true &&
github.event.pull_request.base.ref == 'main'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: read
steps:
- name: Checkout main
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: main
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0

- name: Determine source PR (if any)
id: prinfo
run: |
# Try to find the pull request that was merged into this push (if the push is a merge commit)
# Look for PRs whose merge_commit_sha matches HEAD
head_sha=$(git rev-parse HEAD)
pr_number=$(gh api repos/${{ github.repository }}/commits/${head_sha}/pulls --jq '.[0].number' 2>/dev/null || true)
if [ -z "$pr_number" ]; then
# fallback: try to find PR by searching merged PRs with HEAD in merge_commit_sha via REST
pr_number=$(gh api repos/${{ github.repository }}/pulls --jq '.[] | select(.merge_commit_sha == env.HEAD_SHA) | .number' --paginate 2>/dev/null || true)
fi
echo "head_sha=$head_sha" >> "$GITHUB_OUTPUT"
echo "pr_number=${pr_number:-}" >> "$GITHUB_OUTPUT"

- name: Gather PR info (if found) or use last commit message
- name: Gather PR info
id: info
run: |
pr_number="${{ steps.prinfo.outputs.pr_number }}"
if [ -n "$pr_number" ]; then
pr_json=$(gh api repos/${{ github.repository }}/pulls/$pr_number)
pr_title=$(printf '%s' "$pr_json" | jq -r .title)
pr_user_login=$(printf '%s' "$pr_json" | jq -r .user.login)
pr_html_url=$(printf '%s' "$pr_json" | jq -r .html_url)
created_at=$(printf '%s' "$pr_json" | jq -r .created_at)
else
# No PR found: use last commit info
pr_title=$(git log -1 --pretty=%s)
pr_user_login=$(git log -1 --pretty=%an)
pr_html_url=""
created_at=$(git log -1 --pretty=%cI)
fi
echo "pr_number=${pr_number:-}" >> "$GITHUB_OUTPUT"
pr_number=$(jq -r .pull_request.number "$GITHUB_EVENT_PATH")
pr_title=$(jq -r .pull_request.title "$GITHUB_EVENT_PATH")
pr_user_login=$(jq -r .pull_request.user.login "$GITHUB_EVENT_PATH")
pr_html_url=$(jq -r .pull_request.html_url "$GITHUB_EVENT_PATH")
created_at=$(jq -r .pull_request.created_at "$GITHUB_EVENT_PATH")
echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT"
echo "pr_title=$pr_title" >> "$GITHUB_OUTPUT"
echo "pr_user_login=$pr_user_login" >> "$GITHUB_OUTPUT"
echo "pr_html_url=$pr_html_url" >> "$GITHUB_OUTPUT"
Expand All @@ -71,11 +49,7 @@ jobs:
pr_number="${{ steps.info.outputs.pr_number }}"
safe_title=$(printf '%s' "$title" | sed 's/|/\\|/g')
safe_author=$(printf '%s' "$author" | sed 's/|/\\|/g')
if [ -n "$pr_number" ]; then
entry="- [${date_short}] ${safe_author}: ${safe_title} [#${pr_number}](${pr_url})"
else
entry="- [${date_short}] ${safe_author}: ${safe_title}"
fi
entry="- [${date_short}] ${safe_author}: ${safe_title} [#${pr_number}](${pr_url}) "
echo "$entry" > changelog_entry.txt
echo "changelog_entry_path=changelog_entry.txt" >> "$GITHUB_OUTPUT"

Expand All @@ -88,10 +62,15 @@ jobs:
echo "" >> "$changelog_file"
fi
entry=$(cat "${{ steps.entry.outputs.changelog_entry_path }}")
# Ensure there is a header; append entry at the end of file separated by a blank line
printf "%s\n\n" "$entry" >> "$changelog_file"
# Se il file termina senza newline, assicurati separazione
tail_char=$(tail -c1 "$changelog_file" 2>/dev/null || true)
if [ -n "$tail_char" ] && [ "$tail_char" != $'\n' ]; then
printf "\n\n%s\n" "$entry" >> "$changelog_file"
else
printf "\n%s\n" "$entry" >> "$changelog_file"
fi

- name: Commit and push changelog to main
- name: Commit and push changelog to PR branch
env:
GIT_AUTHOR_NAME: github-actions
GIT_AUTHOR_EMAIL: actions@github.com
Expand All @@ -103,6 +82,6 @@ jobs:
echo "No changes to commit."
exit 0
fi
commit_msg="chore: append CHANGELOG entry for merge ${GITHUB_SHA::7}"
commit_msg="chore: update CHANGELOG for PR #${{ steps.info.outputs.pr_number }}"
git commit -m "$commit_msg"
git push origin main
git push origin HEAD:${{ github.event.pull_request.head.ref }}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

- [2025-11-01] DamImpr: Change changelog update from merge to PR event [#25](https://github.com/DamImpr/cache-multi-layer/pull/25)

- [2025-11-01] DamImpr: Change changelog entry format in workflow2 [#22](https://github.com/DamImpr/cache-multi-layer/pull/22)\

### 2025-10-31 — Remove blank line from entrypoint.sh\n\n- **Autore:** DamImpr\n- **PR:** [#20](https://github.com/DamImpr/cache-multi-layer/pull/20)\n

- [2025-11-01] DamImpr: Change changelog update from merge to PR event [#25](https://github.com/DamImpr/cache-multi-layer/pull/25)