Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0910b3d
feat: implement PR commit message retrieval and validation in commit-…
shenxianpeng Mar 16, 2026
8a8c1f6
fix: correct variable names in subprocess calls for clarity
shenxianpeng Mar 16, 2026
235d397
fix: get original commit message content in get_pr_commit_messages() …
Copilot Mar 16, 2026
44e900c
chore: Update commit-check version to 2.4.3
shenxianpeng Mar 16, 2026
cb56efe
feat: Enable autofix for pull requests in pre-commit config
shenxianpeng Mar 17, 2026
85f23c7
feat: refactor commit-check logic and add unit tests for new function…
shenxianpeng Mar 17, 2026
8d8615e
fix: format list comprehensions for better readability in get_pr_comm…
shenxianpeng Mar 17, 2026
83fd88b
fix: add args to codespell hook to ignore specific words
shenxianpeng Mar 17, 2026
39d41e3
fix: update main_test.py
shenxianpeng Mar 17, 2026
19b205a
Merge branch 'main' into bugfix/issue-184
shenxianpeng Apr 9, 2026
68fd620
fix: improve error handling and return codes in commit message checks
shenxianpeng Apr 20, 2026
dd6ac2c
fix: auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 20, 2026
4bd8a4b
Merge branch 'main' into bugfix/issue-184
shenxianpeng Apr 20, 2026
45c85fa
refactor: clean up PR commit validation flow
shenxianpeng Apr 20, 2026
54dfe61
chore: auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 20, 2026
388b0b1
refactor: clean up PR commit validation flow
shenxianpeng Apr 20, 2026
0cf3f77
chore: auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 20, 2026
784aea0
Merge branch 'main' into bugfix/issue-184
shenxianpeng Apr 20, 2026
b8961db
feat: enhance commit message checks with section separators and no-ba…
shenxianpeng Apr 20, 2026
a379593
fix: update commit message formatting to improve output clarity and c…
shenxianpeng Apr 20, 2026
dbd6305
chore: auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 20, 2026
94942c6
fix: simplify output handling in PR message checks and update test as…
shenxianpeng Apr 20, 2026
7216a50
fix: update output formatting in PR message checks to include commit …
shenxianpeng Apr 20, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# https://pre-commit.com/
ci:
autofix_prs: true
autofix_commit_msg: 'ci: auto fixes from pre-commit.com hooks'
autoupdate_commit_msg: 'ci: pre-commit autoupdate'

Expand Down
62 changes: 58 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ def log_env_vars():
print(f"PR_COMMENTS = {PR_COMMENTS}\n")


def get_pr_commit_messages() -> list[str]:
"""Get all commit messages for the current PR (pull_request event only).

In a pull_request event, actions/checkout checks out a synthetic merge
commit (HEAD = merge of PR branch into base). HEAD^1 is the base branch
tip, HEAD^2 is the PR branch tip. So HEAD^1..HEAD^2 gives all PR commits.
"""
if os.getenv("GITHUB_EVENT_NAME", "") != "pull_request":
return []
try:
result = subprocess.run(
["git", "log", "--pretty=format:%B%x00", "HEAD^1..HEAD^2"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
check=False,
)
if result.returncode == 0 and result.stdout:
return [m.rstrip("\n") for m in result.stdout.split("\x00") if m.rstrip("\n")]
except Exception:
pass
return []
Comment thread
shenxianpeng marked this conversation as resolved.
Outdated
Comment thread
shenxianpeng marked this conversation as resolved.


def run_commit_check() -> int:
"""Runs the commit-check command and logs the result."""
args = [
Expand All @@ -58,13 +82,43 @@ def run_commit_check() -> int:
if value == "true"
]

command = ["commit-check"] + args
print(" ".join(command))
total_rc = 0
with open("result.txt", "w") as result_file:
result = subprocess.run(
if MESSAGE == "true":
pr_messages = get_pr_commit_messages()
if pr_messages:
# In PR context: check each commit message individually to avoid
# only validating the synthetic merge commit at HEAD.
for msg in pr_messages:
result = subprocess.run(
["commit-check", "--message"],
input=msg,
stdout=result_file,
stderr=subprocess.PIPE,
text=True,
check=False,
)
total_rc += result.returncode

# Run non-message checks (branch, author) once
other_args = [a for a in args if a != "--message"]
if other_args:
command = ["commit-check"] + other_args
print(" ".join(command))
other_result = subprocess.run(
command, stdout=result_file, stderr=subprocess.PIPE, check=False
)
total_rc += other_result.returncode

return total_rc
Comment thread
shenxianpeng marked this conversation as resolved.
Outdated

# Non-PR context or message disabled: run all checks at once
command = ["commit-check"] + args
print(" ".join(command))
default_result = subprocess.run(
command, stdout=result_file, stderr=subprocess.PIPE, check=False
)
return result.returncode
return default_result.returncode


def read_result_file() -> str | None:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Install commit-check CLI
# For details please see: https://github.com/commit-check/commit-check
commit-check==2.4.2
commit-check==2.4.3
# Interact with the GitHub API.
PyGithub==2.8.1
Loading