Skip to content

Commit a379593

Browse files
committed
fix: update commit message formatting to improve output clarity and consistency
1 parent b8961db commit a379593

2 files changed

Lines changed: 37 additions & 22 deletions

File tree

main.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
SUCCESS_TITLE = "# Commit-Check ✔️"
1111
FAILURE_TITLE = "# Commit-Check ❌"
1212
COMMIT_MESSAGE_DELIMITER = "\x00"
13-
COMMIT_SECTION_SEPARATOR = "\n" + ("-" * 72) + "\n"
13+
COMMIT_SECTION_SEPARATOR = "\n---\n"
1414

1515
# Environment variables
1616
MESSAGE = os.getenv("MESSAGE", "false")
@@ -144,7 +144,8 @@ def run_check_command(
144144
if result.stdout:
145145
if output_prefix:
146146
result_file.write(output_prefix)
147-
result_file.write(result.stdout)
147+
result_file.write(result.stdout.rstrip("\n"))
148+
result_file.write("\n")
148149
return result.returncode
149150

150151

@@ -154,30 +155,30 @@ def run_pr_message_checks(pr_messages: list[str], result_file: TextIO) -> int:
154155
Returns 1 if any message fails, 0 if all pass.
155156
"""
156157
has_failure = False
158+
emitted_failure_output = False
157159
total_messages = len(pr_messages)
158160
for index, msg in enumerate(pr_messages, start=1):
159161
subject = msg.splitlines()[0] if msg else "<empty commit message>"
160162
command_args = ["--message"]
161-
if index > 1:
163+
if emitted_failure_output:
162164
command_args.append("--no-banner")
163165

164-
output_prefix = f"\n--- Commit {index}/{total_messages}: {subject}\n"
165-
if index > 1:
166+
output_prefix = f"--- Commit {index}/{total_messages}: {subject}\n"
167+
if emitted_failure_output:
166168
output_prefix = (
167169
f"{COMMIT_SECTION_SEPARATOR}"
168170
f"--- Commit {index}/{total_messages}: {subject}\n"
169171
)
170172

171-
has_failure = (
172-
run_check_command(
173-
command_args,
174-
result_file,
175-
input_text=msg,
176-
output_prefix=output_prefix,
177-
)
178-
!= 0
179-
or has_failure
173+
return_code = run_check_command(
174+
command_args,
175+
result_file,
176+
input_text=msg,
177+
output_prefix=output_prefix,
180178
)
179+
if return_code != 0:
180+
has_failure = True
181+
emitted_failure_output = True
181182
return 1 if has_failure else 0
182183

183184

main_test.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,38 +126,52 @@ def test_empty_list(self):
126126
self.assertEqual(rc, 0)
127127
mock_run.assert_not_called()
128128

129-
def test_second_and_later_messages_use_no_banner(self):
129+
def test_first_failure_keeps_banner_and_later_failures_use_no_banner(self):
130130
results = [
131+
MagicMock(returncode=0, stdout=""),
131132
MagicMock(returncode=1, stdout="Commit rejected.\n"),
132133
MagicMock(returncode=1, stdout="Type subject_imperative check failed\n"),
133134
]
134135
with patch("main.subprocess.run", side_effect=results) as mock_run:
135-
main.run_pr_message_checks(["bad first", "bad second"], io.StringIO())
136+
main.run_pr_message_checks(["ok first", "bad second", "bad third"], io.StringIO())
136137

137138
self.assertEqual(
138139
mock_run.call_args_list[0][0][0], ["commit-check", "--message"]
139140
)
140141
self.assertEqual(
141142
mock_run.call_args_list[1][0][0],
143+
["commit-check", "--message"],
144+
)
145+
self.assertEqual(
146+
mock_run.call_args_list[2][0][0],
142147
["commit-check", "--message", "--no-banner"],
143148
)
144149

145-
def test_second_message_prefix_uses_separator(self):
150+
def test_later_failure_prefix_uses_short_separator_without_extra_blank_lines(self):
146151
results = [
152+
MagicMock(returncode=0, stdout=""),
147153
MagicMock(returncode=1, stdout="Commit rejected.\n"),
148-
MagicMock(returncode=1, stdout="Type subject_imperative check failed\n"),
154+
MagicMock(
155+
returncode=1,
156+
stdout=(
157+
"Type subject_imperative check failed ==> bad third\n"
158+
"Commit message should use imperative mood\n"
159+
"Suggest: Use imperative mood\n\n"
160+
),
161+
),
149162
]
150163
result_file = io.StringIO()
151164
with patch("main.subprocess.run", side_effect=results):
152-
main.run_pr_message_checks(["bad first", "bad second"], result_file)
165+
main.run_pr_message_checks(["ok first", "bad second", "bad third"], result_file)
153166

154167
output = result_file.getvalue()
155-
self.assertIn("\n--- Commit 1/2: bad first\nCommit rejected.\n", output)
168+
self.assertIn("--- Commit 2/3: bad second\nCommit rejected.\n", output)
156169
self.assertIn(
157-
f"{main.COMMIT_SECTION_SEPARATOR}--- Commit 2/2: bad second\n",
170+
f"{main.COMMIT_SECTION_SEPARATOR}--- Commit 3/3: bad third\n",
158171
output,
159172
)
160-
self.assertIn("Type subject_imperative check failed\n", output)
173+
self.assertNotIn("------------------------------------------------------------------------", output)
174+
self.assertNotIn("\n\n\n", output)
161175

162176

163177
class TestRunOtherChecks(unittest.TestCase):

0 commit comments

Comments
 (0)