Skip to content

Commit 68fd620

Browse files
committed
fix: improve error handling and return codes in commit message checks
1 parent 19b205a commit 68fd620

3 files changed

Lines changed: 15 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
venv/
22
.venv/
3+
__pycache__/

main.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ def get_pr_commit_messages() -> list[str]:
5757
return [
5858
m.rstrip("\n") for m in result.stdout.split("\x00") if m.rstrip("\n")
5959
]
60-
except Exception:
61-
pass
60+
except Exception as e:
61+
print(
62+
f"::warning::Failed to retrieve PR commit messages: {e}",
63+
file=sys.stderr,
64+
)
6265
return []
6366

6467

@@ -74,9 +77,9 @@ def build_check_args(
7477
def run_pr_message_checks(pr_messages: list[str], result_file) -> int: # type: ignore[type-arg]
7578
"""Checks each PR commit message individually via commit-check --message.
7679
77-
Returns cumulative returncode across all messages.
80+
Returns 1 if any message fails, 0 if all pass.
7881
"""
79-
total_rc = 0
82+
has_failure = False
8083
for msg in pr_messages:
8184
result = subprocess.run(
8285
["commit-check", "--message"],
@@ -86,8 +89,8 @@ def run_pr_message_checks(pr_messages: list[str], result_file) -> int: # type:
8689
text=True,
8790
check=False,
8891
)
89-
total_rc += result.returncode
90-
return total_rc
92+
has_failure = has_failure or (result.returncode != 0)
93+
return 1 if has_failure else 0
9194

9295

9396
def run_other_checks(args: list[str], result_file) -> int: # type: ignore[type-arg]
@@ -97,7 +100,7 @@ def run_other_checks(args: list[str], result_file) -> int: # type: ignore[type-
97100
command = ["commit-check"] + args
98101
print(" ".join(command))
99102
result = subprocess.run(
100-
command, stdout=result_file, stderr=subprocess.PIPE, check=False
103+
command, stdout=result_file, stderr=subprocess.PIPE, text=True, check=False
101104
)
102105
return result.returncode
103106

@@ -107,7 +110,7 @@ def run_default_checks(args: list[str], result_file) -> int: # type: ignore[typ
107110
command = ["commit-check"] + args
108111
print(" ".join(command))
109112
result = subprocess.run(
110-
command, stdout=result_file, stderr=subprocess.PIPE, check=False
113+
command, stdout=result_file, stderr=subprocess.PIPE, text=True, check=False
111114
)
112115
return result.returncode
113116

main_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_multiple_messages_all_fail(self):
7878
results = [MagicMock(returncode=1), MagicMock(returncode=1)]
7979
with patch("main.subprocess.run", side_effect=results):
8080
rc = main.run_pr_message_checks(["bad1", "bad2"], self._make_file())
81-
self.assertEqual(rc, 2)
81+
self.assertEqual(rc, 1)
8282

8383
def test_empty_list(self):
8484
with patch("main.subprocess.run") as mock_run:
@@ -191,11 +191,11 @@ def test_pr_path_rc_accumulation(self):
191191
patch("main.AUTHOR_NAME", "false"),
192192
patch("main.AUTHOR_EMAIL", "false"),
193193
patch("main.get_pr_commit_messages", return_value=["bad msg"]),
194-
patch("main.run_pr_message_checks", return_value=2),
194+
patch("main.run_pr_message_checks", return_value=1),
195195
patch("main.run_other_checks", return_value=1),
196196
):
197197
rc = main.run_commit_check()
198-
self.assertEqual(rc, 3)
198+
self.assertEqual(rc, 2)
199199

200200
def test_non_pr_path_uses_default_checks(self):
201201
with (

0 commit comments

Comments
 (0)