Skip to content

Commit 61a4dce

Browse files
fix: resolve undefined display in PR comment and improve message handling
This commit addresses the issue where 'undefined' could appear in PR comments posted by the scope check CI. Fixes include: - Robust handling of empty PR titles, with specific error messages. - Sanitization of PR titles to remove potential newline characters from jq output. - Improved logic for constructing suggestion messages in comments, ensuring variables are correctly handled even if empty. - Safe multi-line string assignment to GITHUB_ENV for the comment body using heredocs, preventing issues with special characters and newlines. These changes ensure more reliable and informative feedback in PR comments when scope validation fails.
1 parent 2ede1c1 commit 61a4dce

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

.github/workflows/check_pr_scope.yml

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,28 @@ jobs:
2929
- name: Check PR scope
3030
id: check_scope_step # Add id to this step
3131
run: |
32-
pr_title="${{ steps.pr_title.outputs.title }}"
32+
pr_title_raw="${{ steps.pr_title.outputs.title }}"
33+
# Sanitize pr_title_raw by removing CR/LF characters that might come from jq output
34+
pr_title=$(echo "$pr_title_raw" | tr -d '\r\n')
3335
changed_files="${{ steps.changed_files.outputs.files }}"
3436
35-
echo "PR Title: $pr_title"
37+
echo "PR Title: $pr_title" # This should now correctly show the title or be empty
3638
echo "Changed Files:"
3739
echo "$changed_files"
3840
41+
if [ -z "$pr_title" ]; then
42+
error_message="PR title is empty. Please provide a title in the format 'type(scope): message'."
43+
echo "::error::$error_message"
44+
comment_body="**Scope Check Failed!** 🚨\n\n${error_message}\n\nExample: \`feat(my-scope): add new feature\`."
45+
cat <<EOF_COMMENT > comment_file.txt
46+
$comment_body
47+
EOF_COMMENT
48+
echo "comment_body<<EOF_CMT" >> $GITHUB_ENV
49+
cat comment_file.txt >> $GITHUB_ENV
50+
echo "EOF_CMT" >> $GITHUB_ENV
51+
exit 1
52+
fi
53+
3954
# Extract scope from PR title (e.g., "fix(scope1,scope2): ...")
4055
pr_scopes_str=$(echo "$pr_title" | grep -oP '^\w+\(\K[^\)]+' || echo "")
4156
IFS=',' read -r -a pr_scopes <<< "$pr_scopes_str"
@@ -45,8 +60,13 @@ jobs:
4560
if [ ${#pr_scopes[@]} -eq 0 ] && [[ "$pr_scopes_str" != "*" ]]; then
4661
error_message="PR title does not contain a valid scope. Please use the format 'type(scope): message' or 'type(*): message'."
4762
echo "::error::$error_message"
48-
comment_body="**Scope Check Failed!** 🚨\n\n${error_message}\n\nExample: \`feat(my-scope): add new feature\` or \`fix(*): resolve an issue\`."
49-
echo "comment_body=$comment_body" >> $GITHUB_ENV
63+
comment_body="**Scope Check Failed!** 🚨\n\n${error_message}\n\nTitle received: \`$pr_title\`\n\nExample: \`feat(my-scope): add new feature\` or \`fix(*): resolve an issue\`."
64+
cat <<EOF_COMMENT > comment_file.txt
65+
$comment_body
66+
EOF_COMMENT
67+
echo "comment_body<<EOF_CMT" >> $GITHUB_ENV
68+
cat comment_file.txt >> $GITHUB_ENV
69+
echo "EOF_CMT" >> $GITHUB_ENV
5070
exit 1
5171
fi
5272
echo "PR Scopes: ${pr_scopes[@]}"
@@ -115,16 +135,44 @@ jobs:
115135
echo "::error::PR title scopes do not cover all changed files. Missing scopes for: ${missing_scopes[@]}"
116136
# Prepare message for PR comment
117137
comment_body="**Scope Check Failed!** 🚨\n\nPR title scopes do not cover all changed files.\n\n"
118-
comment_body+="Missing required scopes in PR title: \`${missing_scopes[@]}\`\n\n"
119-
comment_body+="Please update your PR title to include these scopes. For example: \`type(${pr_scopes_str:+${pr_scopes_str},}${missing_scopes[@]}): your message\` or \`type(*): your message\` if a wildcard is appropriate.\n\n"
138+
if [ ${#missing_scopes[@]} -gt 0 ]; then
139+
comment_body+="Missing required scopes in PR title: \`${missing_scopes[*]}\`\n\n" # Use [*] for space separated, or loop for comma separated
140+
141+
suggestion_scopes=""
142+
if [ -n "$pr_scopes_str" ] && [[ "$pr_scopes_str" != "*" ]]; then
143+
suggestion_scopes="${pr_scopes_str}"
144+
fi
145+
146+
# Build comma-separated list for suggestion_scopes
147+
for missing_scope in "${missing_scopes[@]}"; do
148+
if [ -n "$suggestion_scopes" ]; then
149+
suggestion_scopes+=","
150+
fi
151+
suggestion_scopes+="$missing_scope"
152+
done
153+
154+
comment_body+="Please update your PR title to include these scopes. For example: \`type($suggestion_scopes): your message\` or \`type(*): your message\` if a wildcard is appropriate.\n\n"
155+
fi
120156
comment_body+="<details><summary>Details</summary>\n"
121-
comment_body+="PR Title Scopes: \`${pr_scopes[@]}\`\n"
157+
# Ensure pr_scopes are displayed correctly, even if empty or just wildcard
158+
pr_scopes_display="${pr_scopes[*]}"
159+
if [ -z "$pr_scopes_display" ] && [ "$pr_scopes_str" == "*" ]; then
160+
pr_scopes_display="*"
161+
elif [ -z "$pr_scopes_display" ]; then
162+
pr_scopes_display="(none)"
163+
fi
164+
comment_body+="PR Title Scopes: \`${pr_scopes_display}\`\n"
122165
comment_body+="Required Scopes from changed files: \`${required_scopes[@]}\`\n"
123166
comment_body+="Changed Files:\n"
124167
comment_body+="\`\`\`\n${changed_files}\n\`\`\`\n"
125168
comment_body+="</details>"
126169

127-
echo "comment_body=$comment_body" >> $GITHUB_ENV
170+
cat <<EOF_COMMENT > comment_file.txt
171+
$comment_body
172+
EOF_COMMENT
173+
echo "comment_body<<EOF_CMT" >> $GITHUB_ENV
174+
cat comment_file.txt >> $GITHUB_ENV
175+
echo "EOF_CMT" >> $GITHUB_ENV
128176
exit 1
129177
else
130178
echo "PR title scopes are valid."

0 commit comments

Comments
 (0)