Skip to content

Commit 63e0608

Browse files
authored
fix: never run the checkout's pip.py, reject non-boolean inputs, check empty messages (#331)
Co-authored-by: Xianpeng Shen <3353385+shenxianpeng@users.noreply.github.com>
1 parent dd5f113 commit 63e0608

4 files changed

Lines changed: 206 additions & 21 deletions

File tree

‎.github/workflows/commit-check.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ jobs:
2424
fetch-depth: 0 # Required for merge-base checks
2525
# Nothing after checkout needs authenticated git.
2626
persist-credentials: false
27+
# The install step must never run a pip.py or venv.py from the
28+
# checkout in place of pip or venv. If it did, these stop the job.
29+
- name: Plant pip.py and venv.py in the workspace
30+
shell: bash
31+
run: |
32+
for name in pip venv; do
33+
echo 'raise SystemExit("a workspace module ran in place of pip or venv")' > "$name.py"
34+
done
2735
- uses: ./ # self test
2836
with:
2937
message: true

‎action.yml‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ runs:
7373
7474
# The pinned commit-check needs Python 3.11+. Say so here: on an older
7575
# python3, pip would only report that no matching version exists.
76-
if ! $PYTHON_CMD -c 'import sys; sys.exit(sys.version_info < (3, 11))'; then
76+
if ! $PYTHON_CMD -I -c 'import sys; sys.exit(sys.version_info < (3, 11))'; then
7777
echo "::error::commit-check-action needs Python 3.11 or newer on PATH, found $($PYTHON_CMD --version 2>&1). Add actions/setup-python with python-version 3.11 or newer before this step."
7878
exit 1
7979
fi
@@ -84,7 +84,11 @@ runs:
8484
WORK="$RUNNER_TEMP/commit-check-action"
8585
rm -rf "$WORK" # a second invocation in the same job starts clean, so the wheel glob below matches exactly one file
8686
mkdir -p "$WORK/wheels"
87-
$PYTHON_CMD -m venv "$WORK/venv"
87+
# -I (isolated mode) everywhere below: `python -m` otherwise puts the
88+
# current directory, the caller's checkout, first on sys.path, so a
89+
# pip.py or venv.py at the repository root would run in place of pip
90+
# or venv, with this step's GITHUB_TOKEN.
91+
$PYTHON_CMD -I -m venv "$WORK/venv"
8892
if [[ "$RUNNER_OS" == "Windows" ]]; then
8993
source "$WORK/venv/Scripts/activate"
9094
else
@@ -94,7 +98,7 @@ runs:
9498
# One download of the pinned closure, into the scratch dir. Wheels only:
9599
# the offline install below cannot build an sdist, so a platform without
96100
# binary wheels fails here, with pip's message, rather than later.
97-
$PYTHON_CMD -m pip download -q --only-binary=:all: -d "$WORK/wheels" -r "$GITHUB_ACTION_PATH/requirements.txt"
101+
$PYTHON_CMD -I -m pip download -q --only-binary=:all: -d "$WORK/wheels" -r "$GITHUB_ACTION_PATH/requirements.txt"
98102
99103
# Verify the commit-check wheel's build provenance (PyGithub and the
100104
# transitive wheels are pinned but not attested; see README "Runner requirements").
@@ -106,7 +110,7 @@ runs:
106110
107111
# Install offline from the wheels we already downloaded and verified:
108112
# no second trip to PyPI, and the installed set is exactly the downloaded set.
109-
$PYTHON_CMD -m pip install -q --no-cache-dir --no-index --find-links "$WORK/wheels" -r "$GITHUB_ACTION_PATH/requirements.txt"
113+
$PYTHON_CMD -I -m pip install -q --no-cache-dir --no-index --find-links "$WORK/wheels" -r "$GITHUB_ACTION_PATH/requirements.txt"
110114
111115
$PYTHON_CMD "$GITHUB_ACTION_PATH/main.py"
112116
env:

‎main.py‎

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import re
1818
import subprocess
1919
import sys
20+
import tempfile
2021
import uuid
2122
from dataclasses import dataclass, field
2223
from typing import Any
@@ -66,9 +67,22 @@
6667
}
6768

6869

70+
#: Boolean inputs whose value was neither true nor false, as ``(name, value)``.
71+
#: ``main`` fails the step on them before any check runs.
72+
INVALID_FLAGS: list[tuple[str, str]] = []
73+
74+
6975
def env_flag(name: str, default: str = "false") -> bool:
70-
"""Read a GitHub Action boolean-style environment variable."""
71-
return os.getenv(name, default).lower() == "true"
76+
"""Read a GitHub Action boolean input: ``true`` or ``false``, any case.
77+
78+
Anything else (``yes``, ``on``, a typo) used to read as false and turn
79+
the check off without a word. It is now recorded in ``INVALID_FLAGS``,
80+
and the step fails with the input's name.
81+
"""
82+
value = os.getenv(name, default).strip().lower()
83+
if value not in ("true", "false"):
84+
INVALID_FLAGS.append((name, os.getenv(name, default)))
85+
return value == "true"
7286

7387

7488
def _reconfigure_io() -> None:
@@ -554,14 +568,13 @@ def parse_commit_messages(output: str) -> list[Commit]:
554568
555569
The fields alternate hash, message, hash, message; git puts a newline
556570
between commits, which lands on the front of the next hash and is
557-
stripped along with the message's trailing newline. Commits are paired
558-
before empty messages are dropped, so a blank message never shifts the
559-
hashes of the commits after it.
571+
stripped along with the message's trailing newline. A commit with an
572+
empty message is kept, as ``(sha, "")``: it is a commit of the pull
573+
request like any other, and ``allow_empty_commits`` is the rule that
574+
judges it.
560575
"""
561576
fields = [f.strip("\n") for f in output.split(COMMIT_MESSAGE_DELIMITER)]
562-
return [
563-
(sha, message) for sha, message in zip(fields[0::2], fields[1::2]) if message
564-
]
577+
return [(sha, message) for sha, message in zip(fields[0::2], fields[1::2]) if sha]
565578

566579

567580
def _messages_in_range(revision_range: str) -> list[Commit]:
@@ -818,12 +831,31 @@ def run_pr_message_checks(pr_commits: list[Commit]) -> list[ScopeResult]:
818831
if author:
819832
names = ("GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL")
820833
env = {key: value for key, value in zip(names, author) if value} or None
821-
results.append(
822-
check_scope(label, ["--message"], input_text=msg, sha=sha, env=env)
823-
)
834+
if msg.strip():
835+
scope = check_scope(label, ["--message"], input_text=msg, sha=sha, env=env)
836+
else:
837+
scope = check_empty_message(label, sha, env)
838+
results.append(scope)
824839
return results
825840

826841

842+
def check_empty_message(
843+
label: str, sha: str, env: dict[str, str] | None
844+
) -> ScopeResult:
845+
"""Check a commit whose message is empty, from an empty file.
846+
847+
Piped, an empty message reads to the CLI as no input at all, and it
848+
checks HEAD in its place: the merge commit, which passes. A file names
849+
the message, so the empty one is judged (``allow_empty_commits``).
850+
"""
851+
fd, path = tempfile.mkstemp(prefix="commit-msg-", dir=os.getenv("RUNNER_TEMP"))
852+
os.close(fd)
853+
try:
854+
return check_scope(label, ["--message", path], sha=sha, env=env)
855+
finally:
856+
os.unlink(path)
857+
858+
827859
def run_other_checks(args: list[str], rev: str | None = None) -> list[ScopeResult]:
828860
"""Run each non-message check (branch, author) once, as its own scope.
829861
@@ -1809,14 +1841,15 @@ def _is_bot(comment: Any) -> bool:
18091841
def _find_own_comments(comments: list[Any]) -> tuple[Any | None, list[Any]]:
18101842
"""Pick the comment to update and the ones to delete.
18111843
1812-
Returns ``(target, stale)``. Only comments carrying ``COMMENT_MARKER`` are
1813-
ever deleted — those are unambiguously ours. A comment from an earlier
1844+
Returns ``(target, stale)``. Only comments a bot posted with
1845+
``COMMENT_MARKER`` in them are ever edited or deleted: a person can paste
1846+
the marker too (quoting a report, say), and their comment is theirs. A comment from an earlier
18141847
version has no marker, so it is adopted (edited, which adds the marker)
18151848
when there is no marked comment yet, and only if a bot posted it: the
18161849
legacy signal is a title prefix, which a person can type by accident, and
18171850
editing someone's comment out from under them is not recoverable.
18181851
"""
1819-
marked = [c for c in comments if COMMENT_MARKER in c.body]
1852+
marked = [c for c in comments if COMMENT_MARKER in c.body and _is_bot(c)]
18201853
if marked:
18211854
return marked[-1], marked[:-1]
18221855

@@ -1887,7 +1920,8 @@ def add_pr_comments(results: list[ScopeResult]) -> int:
18871920
if not repo_name:
18881921
raise ValueError("GITHUB_REPOSITORY is not set")
18891922

1890-
g = Github(auth=Auth.Token(token))
1923+
api_url = os.getenv("GITHUB_API_URL", "https://api.github.com")
1924+
g = Github(auth=Auth.Token(token), base_url=api_url)
18911925
repo = g.get_repo(repo_name)
18921926
pull_request = repo.get_issue(pr_number)
18931927

@@ -2033,9 +2067,23 @@ def log_error_and_exit(ret_code: int) -> None:
20332067
sys.exit(ret_code)
20342068

20352069

2070+
def fail_on_invalid_flags() -> None:
2071+
"""Stop with an error when a boolean input is neither true nor false."""
2072+
if not INVALID_FLAGS:
2073+
return
2074+
for name, value in INVALID_FLAGS:
2075+
input_name = name.lower().replace("_", "-")
2076+
print(
2077+
f"::error::Input {input_name} must be true or false, got "
2078+
f"{_log_safe(repr(value))}"
2079+
)
2080+
sys.exit(1)
2081+
2082+
20362083
def main():
20372084
"""Main function to run commit-check and render all output surfaces."""
20382085
_reconfigure_io()
2086+
fail_on_invalid_flags()
20392087
log_env_vars()
20402088

20412089
ret_code, results = run_commit_check()

‎main_test.py‎

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,12 @@ def test_the_format_asks_git_for_the_hash_and_the_body(self):
245245
self.assertEqual(main.COMMIT_LOG_FORMAT, "--pretty=format:%H%x00%B%x00")
246246
self.assertNotIn("\x00", main.COMMIT_LOG_FORMAT)
247247

248-
def test_an_empty_message_does_not_shift_the_hashes_after_it(self):
248+
def test_an_empty_message_is_kept_and_does_not_shift_the_hashes(self):
249249
output = f"{SHA_A}\x00\n\x00\n{SHA_B}\x00feat: second\n\x00"
250-
self.assertEqual(main.parse_commit_messages(output), [(SHA_B, "feat: second")])
250+
self.assertEqual(
251+
main.parse_commit_messages(output),
252+
[(SHA_A, ""), (SHA_B, "feat: second")],
253+
)
251254

252255
def test_empty_output_is_no_commits(self):
253256
self.assertEqual(main.parse_commit_messages(""), [])
@@ -2455,6 +2458,7 @@ def test_updates_existing_comment_when_changed(self):
24552458
def test_skips_when_comment_is_up_to_date(self):
24562459
body = main.render_pr_comment([fail_scope()])
24572460
existing = MagicMock(body=body)
2461+
existing.user.type = "Bot"
24582462
mock_pull_request = MagicMock()
24592463
mock_pull_request.get_comments.return_value = [existing]
24602464
mock_repo = MagicMock()
@@ -2904,6 +2908,14 @@ def test_bot_comment_from_an_older_version_is_adopted(self):
29042908
self.assertIs(target, legacy)
29052909
self.assertEqual(stale, [])
29062910

2911+
def test_human_comment_with_the_marker_is_never_touched(self):
2912+
"""A person can paste the marker too, e.g. quoting a report."""
2913+
quoted = self._comment(f"{main.COMMENT_MARKER}\nquoted", user_type="User")
2914+
mine = self._comment(f"{main.COMMENT_MARKER}\nreport")
2915+
self.assertEqual(main._find_own_comments([quoted, mine]), (mine, []))
2916+
self.assertEqual(main._find_own_comments([mine, quoted]), (mine, []))
2917+
self.assertEqual(main._find_own_comments([quoted]), (None, []))
2918+
29072919
def test_no_comments_yields_nothing_to_update(self):
29082920
target, stale = main._find_own_comments([])
29092921
self.assertIsNone(target)
@@ -4096,3 +4108,116 @@ def test_a_missing_pygithub_warns(self):
40964108

40974109
if __name__ == "__main__":
40984110
unittest.main()
4111+
4112+
4113+
class TestBooleanInputs(unittest.TestCase):
4114+
"""A boolean input is true or false; anything else fails the step."""
4115+
4116+
def setUp(self):
4117+
main.INVALID_FLAGS.clear()
4118+
self.addCleanup(main.INVALID_FLAGS.clear)
4119+
4120+
def test_true_and_false_in_any_case_and_padding(self):
4121+
for value, expected in (
4122+
("true", True),
4123+
(" True ", True),
4124+
("FALSE", False),
4125+
("false", False),
4126+
):
4127+
with patch.dict(os.environ, {"MESSAGE": value}):
4128+
self.assertIs(main.env_flag("MESSAGE"), expected, value)
4129+
self.assertEqual(main.INVALID_FLAGS, [])
4130+
4131+
def test_unset_input_takes_the_default(self):
4132+
with patch.dict(os.environ, {}, clear=True):
4133+
self.assertFalse(main.env_flag("MESSAGE"))
4134+
self.assertEqual(main.INVALID_FLAGS, [])
4135+
4136+
def test_other_values_are_recorded(self):
4137+
for value in ("yes", "on", "1", "maybe"):
4138+
with patch.dict(os.environ, {"MESSAGE": value}):
4139+
self.assertFalse(main.env_flag("MESSAGE"))
4140+
self.assertEqual(
4141+
main.INVALID_FLAGS,
4142+
[
4143+
("MESSAGE", "yes"),
4144+
("MESSAGE", "on"),
4145+
("MESSAGE", "1"),
4146+
("MESSAGE", "maybe"),
4147+
],
4148+
)
4149+
4150+
def test_invalid_input_fails_before_any_check(self):
4151+
main.INVALID_FLAGS.append(("PR_TITLE", "yes"))
4152+
out = io.StringIO()
4153+
with (
4154+
patch("sys.stdout", out),
4155+
patch("main.run_commit_check") as run,
4156+
self.assertRaises(SystemExit) as cm,
4157+
):
4158+
main.main()
4159+
self.assertEqual(cm.exception.code, 1)
4160+
run.assert_not_called()
4161+
self.assertIn(
4162+
"::error::Input pr-title must be true or false, got 'yes'", out.getvalue()
4163+
)
4164+
4165+
4166+
class TestPrCommentApiUrl(unittest.TestCase):
4167+
"""The PR comment goes to the same API host as every other call (GHES)."""
4168+
4169+
def test_comment_client_uses_github_api_url(self):
4170+
github_module = MagicMock()
4171+
pull_request = MagicMock()
4172+
pull_request.get_comments.return_value = []
4173+
github_module.Github.return_value.get_repo.return_value.get_issue.return_value = pull_request
4174+
with (
4175+
patch("main.PR_COMMENTS_ENABLED", True),
4176+
patch("main.is_fork_pr_with_readonly_token", return_value=False),
4177+
patch("main.get_pr_number", return_value=7),
4178+
patch.dict(
4179+
os.environ,
4180+
{
4181+
"GITHUB_EVENT_NAME": "pull_request",
4182+
"GITHUB_TOKEN": "t",
4183+
"GITHUB_REPOSITORY": "o/r",
4184+
"GITHUB_API_URL": "https://ghe.example.com/api/v3",
4185+
},
4186+
),
4187+
patch.dict(sys.modules, {"github": github_module}),
4188+
):
4189+
main.add_pr_comments([fail_scope()])
4190+
_, kwargs = github_module.Github.call_args
4191+
self.assertEqual(kwargs["base_url"], "https://ghe.example.com/api/v3")
4192+
4193+
4194+
class TestEmptyCommitMessage(unittest.TestCase):
4195+
"""An empty message is checked as empty, never as HEAD's message."""
4196+
4197+
def test_an_empty_message_goes_to_the_cli_as_an_empty_file(self):
4198+
seen: list[tuple[list[str], str | None, str]] = []
4199+
4200+
def run(command, **kwargs):
4201+
path = command[-1]
4202+
with open(path, encoding="utf-8") as f:
4203+
seen.append((command, kwargs.get("input"), f.read()))
4204+
return MagicMock(
4205+
returncode=1,
4206+
stdout=json_output(make_check("allow_empty_commits", status="fail")),
4207+
stderr="",
4208+
)
4209+
4210+
with (
4211+
patch("main.subprocess.run", side_effect=run),
4212+
patch("main._local_author", return_value=None),
4213+
):
4214+
results = main.run_pr_message_checks([("a1", "")])
4215+
command, piped, content = seen[0]
4216+
self.assertEqual(command[:4], ["commit-check", "--format", "json", "--message"])
4217+
self.assertIsNone(piped)
4218+
self.assertEqual(content, "")
4219+
self.assertFalse(os.path.exists(command[-1]))
4220+
self.assertEqual(
4221+
[(s.label, s.sha, s.status) for s in results],
4222+
[("Commit 1/1", "a1", "fail")],
4223+
)

0 commit comments

Comments
 (0)