Skip to content

Commit 0fbb1f1

Browse files
committed
fix: improve PR title and commit validation logic in workflow
1 parent 965b314 commit 0fbb1f1

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

.github/workflows/pr-conventional-commits.yml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ jobs:
5858
5959
- name: Check PR Title
6060
id: check_title
61+
env:
62+
PR_TITLE: ${{ github.event.pull_request.title }}
6163
run: |
62-
PR_TITLE="${{ github.event.pull_request.title }}"
6364
echo "Checking PR title: $PR_TITLE"
6465
if echo "$PR_TITLE" | npx commitlint; then
6566
echo "title_valid=true" >> "$GITHUB_OUTPUT"
@@ -100,40 +101,48 @@ jobs:
100101
101102
- name: Update PR comment
102103
if: always()
104+
env:
105+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
106+
# Pass values via env (data, not code) so special chars like
107+
# backticks in commit messages are never evaluated by the shell.
108+
TITLE_VALID: ${{ steps.check_title.outputs.title_valid }}
109+
COMMITS_VALID: ${{ steps.check_commits.outputs.commits_valid }}
110+
COMMIT_COUNT: ${{ steps.check_commits.outputs.commit_count }}
111+
INVALID_COMMITS: ${{ steps.check_commits.outputs.invalid_commits }}
112+
PR_NUMBER: ${{ github.event.pull_request.number }}
113+
REPO: ${{ github.repository }}
103114
run: |
104115
BODY="## Conventional Commits Validation\n\n"
105116
106-
if [ "${{ steps.check_title.outputs.title_valid }}" = "true" ]; then
117+
if [ "$TITLE_VALID" = "true" ]; then
107118
BODY="${BODY}**PR Title**: valid\n"
108119
else
109120
BODY="${BODY}**PR Title**: invalid — please use \`type(scope): description\`\n"
110121
fi
111122
112-
if [ "${{ steps.check_commits.outputs.commits_valid }}" = "true" ]; then
113-
BODY="${BODY}**Commits**: all ${{ steps.check_commits.outputs.commit_count }} follow conventional format\n"
123+
if [ "$COMMITS_VALID" = "true" ]; then
124+
BODY="${BODY}**Commits**: all ${COMMIT_COUNT} follow conventional format\n"
114125
else
115126
BODY="${BODY}**Commits**: some do not follow conventional format\n"
116-
BODY="${BODY}${{ steps.check_commits.outputs.invalid_commits }}\n"
127+
BODY="${BODY}${INVALID_COMMITS}\n"
117128
fi
118129
119-
if [ "${{ steps.check_title.outputs.title_valid }}" != "true" ] || \
120-
[ "${{ steps.check_commits.outputs.commits_valid }}" != "true" ]; then
130+
if [ "$TITLE_VALID" != "true" ] || \
131+
[ "$COMMITS_VALID" != "true" ]; then
121132
BODY="${BODY}\n### Format\n\n\`type(scope): description\`\n\n"
122133
BODY="${BODY}**Types**: \`feat\`, \`fix\`, \`docs\`, \`style\`, \`refactor\`, \`test\`, \`chore\`, \`ci\`, \`build\`, \`perf\`, \`revert\`\n"
123134
fi
124135
125-
COMMENT_ID=$(gh pr view ${{ github.event.pull_request.number }} --json comments \
136+
COMMENT_ID=$(gh pr view "$PR_NUMBER" --json comments \
126137
--jq '.comments[] | select(.author.login == "github-actions[bot]" and (.body | test("Conventional Commits Validation"))) | .id' \
127138
| head -n 1)
128139
129140
if [ -n "$COMMENT_ID" ]; then
130-
gh api -X PATCH "repos/${{ github.repository }}/issues/comments/$COMMENT_ID" \
141+
gh api -X PATCH "repos/${REPO}/issues/comments/$COMMENT_ID" \
131142
-f body="$(echo -e "$BODY")"
132143
else
133-
gh pr comment ${{ github.event.pull_request.number }} --body "$(echo -e "$BODY")"
144+
gh pr comment "$PR_NUMBER" --body "$(echo -e "$BODY")"
134145
fi
135-
env:
136-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
137146
138147
- name: Set commit status
139148
if: always()

0 commit comments

Comments
 (0)