Skip to content

Commit 85f23c7

Browse files
committed
feat: refactor commit-check logic and add unit tests for new functionality
1 parent cb56efe commit 85f23c7

File tree

2 files changed

+593
-47
lines changed

2 files changed

+593
-47
lines changed

main.py

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -60,65 +60,73 @@ def get_pr_commit_messages() -> list[str]:
6060
return []
6161

6262

63-
def run_commit_check() -> int:
64-
"""Runs the commit-check command and logs the result."""
65-
args = [
66-
"--message",
67-
"--branch",
68-
"--author-name",
69-
"--author-email",
70-
]
71-
args = [
72-
arg
73-
for arg, value in zip(
74-
args,
75-
[
76-
MESSAGE,
77-
BRANCH,
78-
AUTHOR_NAME,
79-
AUTHOR_EMAIL,
80-
],
63+
def build_check_args(
64+
message: str, branch: str, author_name: str, author_email: str
65+
) -> list[str]:
66+
"""Maps 'true'/'false' flag values to CLI argument list."""
67+
flags = ["--message", "--branch", "--author-name", "--author-email"]
68+
values = [message, branch, author_name, author_email]
69+
return [flag for flag, value in zip(flags, values) if value == "true"]
70+
71+
72+
def run_pr_message_checks(pr_messages: list[str], result_file) -> int: # type: ignore[type-arg]
73+
"""Checks each PR commit message individually via commit-check --message.
74+
75+
Returns cumulative returncode across all messages.
76+
"""
77+
total_rc = 0
78+
for msg in pr_messages:
79+
result = subprocess.run(
80+
["commit-check", "--message"],
81+
input=msg,
82+
stdout=result_file,
83+
stderr=subprocess.PIPE,
84+
text=True,
85+
check=False,
8186
)
82-
if value == "true"
83-
]
87+
total_rc += result.returncode
88+
return total_rc
89+
8490

91+
def run_other_checks(args: list[str], result_file) -> int: # type: ignore[type-arg]
92+
"""Runs non-message checks (branch, author) once. Returns 0 if args is empty."""
93+
if not args:
94+
return 0
95+
command = ["commit-check"] + args
96+
print(" ".join(command))
97+
result = subprocess.run(
98+
command, stdout=result_file, stderr=subprocess.PIPE, check=False
99+
)
100+
return result.returncode
101+
102+
103+
def run_default_checks(args: list[str], result_file) -> int: # type: ignore[type-arg]
104+
"""Runs all checks at once (non-PR context or message disabled)."""
105+
command = ["commit-check"] + args
106+
print(" ".join(command))
107+
result = subprocess.run(
108+
command, stdout=result_file, stderr=subprocess.PIPE, check=False
109+
)
110+
return result.returncode
111+
112+
113+
def run_commit_check() -> int:
114+
"""Runs the commit-check command and logs the result."""
115+
args = build_check_args(MESSAGE, BRANCH, AUTHOR_NAME, AUTHOR_EMAIL)
85116
total_rc = 0
86117
with open("result.txt", "w") as result_file:
87118
if MESSAGE == "true":
88119
pr_messages = get_pr_commit_messages()
89120
if pr_messages:
90121
# In PR context: check each commit message individually to avoid
91122
# only validating the synthetic merge commit at HEAD.
92-
for msg in pr_messages:
93-
result = subprocess.run(
94-
["commit-check", "--message"],
95-
input=msg,
96-
stdout=result_file,
97-
stderr=subprocess.PIPE,
98-
text=True,
99-
check=False,
100-
)
101-
total_rc += result.returncode
102-
103-
# Run non-message checks (branch, author) once
123+
total_rc += run_pr_message_checks(pr_messages, result_file)
104124
other_args = [a for a in args if a != "--message"]
105-
if other_args:
106-
command = ["commit-check"] + other_args
107-
print(" ".join(command))
108-
other_result = subprocess.run(
109-
command, stdout=result_file, stderr=subprocess.PIPE, check=False
110-
)
111-
total_rc += other_result.returncode
112-
125+
total_rc += run_other_checks(other_args, result_file)
113126
return total_rc
114-
115127
# Non-PR context or message disabled: run all checks at once
116-
command = ["commit-check"] + args
117-
print(" ".join(command))
118-
default_result = subprocess.run(
119-
command, stdout=result_file, stderr=subprocess.PIPE, check=False
120-
)
121-
return default_result.returncode
128+
total_rc += run_default_checks(args, result_file)
129+
return total_rc
122130

123131

124132
def read_result_file() -> str | None:

0 commit comments

Comments
 (0)