| 1 |
name: PR bump preview |
| 2 |
|
| 3 |
on: |
| 4 |
pull_request_target: |
| 5 |
types: [opened, reopened, synchronize, ready_for_review] |
| 6 |
|
| 7 |
permissions: |
| 8 |
contents: read |
| 9 |
pull-requests: write |
| 10 |
|
| 11 |
jobs: |
| 12 |
bump-preview: |
| 13 |
# Skip drafts, and skip fork PRs entirely. `pull_request_target` runs with |
| 14 |
# the base repo's GITHUB_TOKEN (write access to PR comments). `cz bump` |
| 15 |
# can render Jinja templates from the checked-out workspace whenever |
| 16 |
# `update_changelog_on_bump` is set in config, and the renderer is not |
| 17 |
# sandboxed (FileSystemLoader('.')) — running it against fork-controlled |
| 18 |
# files would risk RCE / token exfiltration. Same-repo PRs are written by |
| 19 |
# collaborators who already have push access, so the same risk doesn't |
| 20 |
# apply. |
| 21 |
if: > |
| 22 |
${{ |
| 23 |
github.event.pull_request.draft == false && |
| 24 |
github.event.pull_request.head.repo.full_name == |
| 25 |
github.event.pull_request.base.repo.full_name |
| 26 |
}} |
| 27 |
runs-on: ubuntu-latest |
| 28 |
steps: |
| 29 |
- name: Check out PR head |
| 30 |
uses: actions/checkout@v7 |
| 31 |
with: |
| 32 |
ref: ${{ github.event.pull_request.head.sha }} |
| 33 |
fetch-depth: 0 |
| 34 |
fetch-tags: true |
| 35 |
# Defense in depth: don't write the workflow token to .git/config. |
| 36 |
persist-credentials: false |
| 37 |
|
| 38 |
- name: Set up Commitizen |
| 39 |
uses: commitizen-tools/setup-cz@main |
| 40 |
with: |
| 41 |
set-git-config: false |
| 42 |
|
| 43 |
- name: Run cz bump --dry-run |
| 44 |
id: dry-run |
| 45 |
run: | |
| 46 |
set +e |
| 47 |
output="$(cz bump --dry-run --yes 2>&1)" |
| 48 |
status=$? |
| 49 |
set -e |
| 50 |
{ |
| 51 |
echo "status=${status}" |
| 52 |
echo "output<<__CZ_BUMP_PREVIEW__" |
| 53 |
printf '%s\n' "${output}" |
| 54 |
echo "__CZ_BUMP_PREVIEW__" |
| 55 |
} >> "$GITHUB_OUTPUT" |
| 56 |
|
| 57 |
- name: Build comment body |
| 58 |
env: |
| 59 |
STATUS: ${{ steps.dry-run.outputs.status }} |
| 60 |
OUTPUT: ${{ steps.dry-run.outputs.output }} |
| 61 |
run: | |
| 62 |
{ |
| 63 |
echo "<!-- commitizen-bump-preview -->" |
| 64 |
echo "## 🔍 Commitizen bump preview" |
| 65 |
echo "" |
| 66 |
case "${STATUS}" in |
| 67 |
0) |
| 68 |
echo "Merging this PR will produce the following bump:" |
| 69 |
echo "" |
| 70 |
echo '```' |
| 71 |
printf '%s\n' "${OUTPUT}" |
| 72 |
echo '```' |
| 73 |
;; |
| 74 |
21) |
| 75 |
echo "No commits in this PR are eligible for a version bump." |
| 76 |
;; |
| 77 |
*) |
| 78 |
echo "⚠️ \`cz bump --dry-run\` exited with status \`${STATUS}\`:" |
| 79 |
echo "" |
| 80 |
echo '```' |
| 81 |
printf '%s\n' "${OUTPUT}" |
| 82 |
echo '```' |
| 83 |
;; |
| 84 |
esac |
| 85 |
} > comment.md |
| 86 |
|
| 87 |
# Look up an existing preview comment so the next step edits it in |
| 88 |
# place instead of appending a new one on every run. |
| 89 |
# `peter-evans/create-or-update-comment` does not search by body |
| 90 |
# itself -- it only creates new comments, or updates a specific |
| 91 |
# `comment-id`. |
| 92 |
- name: Find existing preview comment |
| 93 |
id: find-comment |
| 94 |
uses: peter-evans/find-comment@v4 |
| 95 |
with: |
| 96 |
token: ${{ secrets.GITHUB_TOKEN }} |
| 97 |
issue-number: ${{ github.event.pull_request.number }} |
| 98 |
comment-author: "github-actions[bot]" |
| 99 |
body-includes: "<!-- commitizen-bump-preview -->" |
| 100 |
|
| 101 |
- name: Post or update PR comment |
| 102 |
uses: peter-evans/create-or-update-comment@v5 |
| 103 |
with: |
| 104 |
token: ${{ secrets.GITHUB_TOKEN }} |
| 105 |
# When empty (no prior comment found) a new one is created; |
| 106 |
# otherwise the existing comment is edited in place. |
| 107 |
comment-id: ${{ steps.find-comment.outputs.comment-id }} |
| 108 |
issue-number: ${{ github.event.pull_request.number }} |
| 109 |
body-path: comment.md |
| 110 |
edit-mode: replace |