Skip to content

Commit 551d909

Browse files
authored
fix(ci-lint): read GITHUB_EVENT_PATH when --event is omitted; warn when reuse has no head SHA (#21)
Title-edit reuse silently never fired on zackees/template-python-rust-cmd#19: its plan step passed --reuse but not --event, so the PR head SHA was never found and reuse returned 'nothing' without a warning. _load_event now falls back to GITHUB_EVENT_PATH (set by GitHub Actions and act), and a --reuse request with no head SHA prints an explicit warning.
1 parent 589e2c0 commit 551d909

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎ci_lint/cli.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060

6161

6262
def _load_event(path: str | None) -> dict[str, object]:
63+
"""Load the GitHub event payload from `--event`, falling back to the
64+
runner's `GITHUB_EVENT_PATH` (also set by act). Without this fallback a
65+
workflow step that omitted `--event` silently lost the PR head SHA, and
66+
title-edit reuse never fired (template-python-rust-cmd#19)."""
67+
if path is None:
68+
path = os.environ.get("GITHUB_EVENT_PATH") or None
6369
if path is None:
6470
return {}
6571
try:
@@ -107,8 +113,15 @@ def _compute_reuse_for_plan(plan: Plan, event_name: str, event: dict[str, object
107113
token = os.environ.get("GITHUB_TOKEN")
108114
repo_slug = os.environ.get("GITHUB_REPOSITORY")
109115
head_sha = _head_sha_from_event(event)
110-
if not token or not repo_slug or not head_sha:
116+
if not token or not repo_slug:
111117
return empty_result(lane_digests)
118+
if not head_sha:
119+
warning = (
120+
"reuse: no pull_request.head.sha in the event payload; pass --event or run "
121+
"inside GitHub Actions (GITHUB_EVENT_PATH) -- reusing nothing"
122+
)
123+
print(f"ci-lint: {warning}", file=sys.stderr)
124+
return empty_result(lane_digests, warning=warning)
112125
result = compute_reuse(
113126
repo=repo_slug,
114127
head_sha=head_sha,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""`_load_event` falls back to GITHUB_EVENT_PATH (template-python-rust-cmd#19:
2+
title-edit reuse silently never fired because the plan step omitted --event)."""
3+
4+
import json
5+
import os
6+
import tempfile
7+
import unittest
8+
from unittest import mock
9+
10+
from ci_lint.cli import _head_sha_from_event, _load_event
11+
12+
13+
class EventPathFallbackTest(unittest.TestCase):
14+
def test_uses_github_event_path_when_no_flag(self) -> None:
15+
payload = {"pull_request": {"title": "[ci-windows] x", "head": {"sha": "a" * 40}}}
16+
with tempfile.TemporaryDirectory() as tmp:
17+
path = os.path.join(tmp, "event.json")
18+
with open(path, "w", encoding="utf-8") as fh:
19+
json.dump(payload, fh)
20+
with mock.patch.dict(os.environ, {"GITHUB_EVENT_PATH": path}):
21+
event = _load_event(None)
22+
self.assertEqual(_head_sha_from_event(event), "a" * 40)
23+
24+
def test_explicit_flag_wins(self) -> None:
25+
with tempfile.TemporaryDirectory() as tmp:
26+
explicit = os.path.join(tmp, "explicit.json")
27+
with open(explicit, "w", encoding="utf-8") as fh:
28+
json.dump({"pull_request": {"head": {"sha": "b" * 40}}}, fh)
29+
with mock.patch.dict(os.environ, {"GITHUB_EVENT_PATH": os.path.join(tmp, "missing.json")}):
30+
event = _load_event(explicit)
31+
self.assertEqual(_head_sha_from_event(event), "b" * 40)
32+
33+
def test_no_flag_no_env_is_empty(self) -> None:
34+
with mock.patch.dict(os.environ, {}, clear=True):
35+
self.assertEqual(_load_event(None), {})
36+
37+
38+
if __name__ == "__main__":
39+
unittest.main()

0 commit comments

Comments
 (0)