Skip to content

Commit 35f149d

Browse files
authored
chore: better base detection debug (#894)
This change introduces the source of where we found the base/head sha This will help debuging Customer CI.
1 parent 7f080b3 commit 35f149d

File tree

5 files changed

+87
-49
lines changed

5 files changed

+87
-49
lines changed

mergify_cli/ci/cli.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,19 @@ def scopes(
237237
head: str | None = None,
238238
base: str | None = None,
239239
) -> None:
240-
ref = git_refs_detector.detect()
240+
if base or head:
241+
ref = git_refs_detector.References(
242+
base=base,
243+
head=head or "HEAD",
244+
source="manual",
245+
)
246+
else:
247+
ref = git_refs_detector.detect()
248+
241249
try:
242250
scopes = scopes_cli.detect(
243251
config_path=config_path,
244-
base=base or ref.base,
245-
head=head or ref.head,
246-
is_merge_queue=ref.is_merge_queue,
252+
references=ref,
247253
)
248254
except scopes_exc.ScopesError as e:
249255
raise click.ClickException(str(e)) from e

mergify_cli/ci/git_refs/detector.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,21 @@ def _detect_base_from_push_event(ev: dict[str, typing.Any]) -> str | None:
107107
return None
108108

109109

110+
ReferencesSource = typing.Literal[
111+
"manual",
112+
"merge_queue",
113+
"fallback_last_commit",
114+
"github_event_other",
115+
"github_event_pull_request",
116+
"github_event_push",
117+
]
118+
119+
110120
@dataclasses.dataclass
111121
class References:
112122
base: str | None
113123
head: str
114-
is_merge_queue: bool
124+
source: ReferencesSource
115125

116126
def maybe_write_to_github_outputs(self) -> None:
117127
gha = os.environ.get("GITHUB_OUTPUT")
@@ -135,37 +145,41 @@ def detect() -> References:
135145
event_name, event = utils.get_github_event()
136146
except utils.GitHubEventNotFoundError:
137147
# fallback to last commit
138-
return References("HEAD^", "HEAD", is_merge_queue=False)
148+
return References("HEAD^", "HEAD", "fallback_last_commit")
139149
else:
140150
if event_name in PULL_REQUEST_EVENTS:
141151
head = _detect_head_from_event(event) or "HEAD"
142152
# 0) merge-queue PR override
143153
mq_sha = _detect_base_from_merge_queue_payload(event)
144154
if mq_sha:
145-
return References(mq_sha, head, is_merge_queue=True)
155+
return References(mq_sha, head, "merge_queue")
146156

147157
# 1) standard event payload
148158
event_sha = _detect_base_from_event(event)
149159
if event_sha:
150-
return References(event_sha, head, is_merge_queue=False)
160+
return References(event_sha, head, "github_event_pull_request")
151161

152162
# 2) standard event payload
153163
event_sha = _detect_default_branch_from_event(event)
154164
if event_sha:
155-
return References(event_sha, head, is_merge_queue=False)
165+
return References(
166+
event_sha,
167+
head,
168+
"github_event_pull_request",
169+
)
156170

157171
elif event_name == "push":
158172
head_sha = _detect_head_from_push_event(event) or "HEAD"
159173
base_sha = _detect_base_from_push_event(event)
160174
if base_sha:
161-
return References(base_sha, head_sha, is_merge_queue=False)
175+
return References(base_sha, head_sha, "github_event_push")
162176

163177
event_sha = _detect_default_branch_from_event(event)
164178
if event_sha:
165-
return References(event_sha, "HEAD", is_merge_queue=False)
179+
return References(event_sha, "HEAD", "github_event_push")
166180

167181
else:
168-
return References(None, "HEAD", is_merge_queue=False)
182+
return References(None, "HEAD", "github_event_other")
169183

170184
msg = "Could not detect base SHA. Provide GITHUB_EVENT_NAME / GITHUB_EVENT_PATH."
171185
raise BaseNotFoundError(msg)

mergify_cli/ci/scopes/cli.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
if typing.TYPE_CHECKING:
2121
from collections import abc
2222

23+
from mergify_cli.ci.git_refs import detector as git_refs_detector
24+
2325
GITHUB_ACTIONS_SCOPES_OUTPUT_NAME = "scopes"
2426

2527

@@ -93,8 +95,7 @@ def maybe_write_github_outputs(
9395

9496

9597
def maybe_write_github_step_summary(
96-
base: str | None,
97-
head: str,
98+
references: git_refs_detector.References,
9899
all_scopes: abc.Iterable[str],
99100
scopes_hit: set[str],
100101
) -> None:
@@ -103,8 +104,8 @@ def maybe_write_github_step_summary(
103104
return
104105
# Build a pretty Markdown table with emojis
105106
markdown = "## Mergify CI Scope Matching Results"
106-
if base is not None:
107-
markdown += f" for `{base}...{head}`"
107+
if references.base is not None:
108+
markdown += f" for `{references.base[:7]}...{references.head[:7]}` (source: `{references.source}`)"
108109
markdown += "\n\n"
109110
markdown += "| 🎯 Scope | ✅ Match |\n|:--|:--|\n"
110111
for scope in sorted(all_scopes):
@@ -138,15 +139,14 @@ def load_from_file(cls, filename: str) -> DetectedScope:
138139
def detect(
139140
config_path: str,
140141
*,
141-
base: str | None,
142-
head: str,
143-
is_merge_queue: bool,
142+
references: git_refs_detector.References,
144143
) -> DetectedScope:
145144
cfg = config.Config.from_yaml(config_path)
146145

147-
if base is not None:
148-
click.echo(f"Base: {base}")
149-
click.echo(f"Head: {head}")
146+
if references.base is not None:
147+
click.echo(f"Base: {references.base}")
148+
click.echo(f"Head: {references.head}")
149+
click.echo(f"Source: {references.source}")
150150

151151
scopes_hit: set[str]
152152
per_scope: dict[str, list[str]]
@@ -158,12 +158,12 @@ def detect(
158158
per_scope = {}
159159
elif isinstance(source, config.SourceFiles):
160160
all_scopes = set(source.files.keys())
161-
if base is None:
161+
if references.base is None:
162162
click.echo("No base provided, selecting all scopes")
163163
scopes_hit = set(source.files.keys())
164164
per_scope = {}
165165
else:
166-
changed = changed_files.git_changed_files(base, head)
166+
changed = changed_files.git_changed_files(references.base, references.head)
167167
click.echo("Changed files detected:")
168168
for file in changed:
169169
click.echo(f"- {file}")
@@ -177,7 +177,7 @@ def detect(
177177

178178
if cfg.scopes.merge_queue_scope is not None:
179179
all_scopes.add(cfg.scopes.merge_queue_scope)
180-
if is_merge_queue:
180+
if references.source == "merge_queue":
181181
scopes_hit.add(cfg.scopes.merge_queue_scope)
182182

183183
if scopes_hit:
@@ -192,8 +192,7 @@ def detect(
192192

193193
maybe_write_github_outputs(all_scopes, scopes_hit)
194194
maybe_write_github_step_summary(
195-
base,
196-
head,
195+
references,
197196
all_scopes,
198197
scopes_hit,
199198
)

mergify_cli/tests/ci/git_refs/test_git_refs_detector.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def test_detect_base_from_repository_default_branch(
2727

2828
result = detector.detect()
2929

30-
assert result == detector.References("main", "HEAD", is_merge_queue=False)
30+
expected_base_source: detector.ReferencesSource = (
31+
"github_event_push" if event_name == "push" else "github_event_pull_request"
32+
)
33+
assert result == detector.References("main", "HEAD", expected_base_source)
3134

3235

3336
def test_maybe_write_github_outputs(
@@ -71,7 +74,7 @@ def test_detect_base_from_push_event(
7174
assert result == detector.References(
7275
"abc123",
7376
"xyz987",
74-
is_merge_queue=False,
77+
"github_event_push",
7578
)
7679

7780

@@ -96,7 +99,7 @@ def test_detect_base_from_pull_request_event_path(
9699
assert result == detector.References(
97100
"abc123",
98101
"xyz987",
99-
is_merge_queue=False,
102+
"github_event_pull_request",
100103
)
101104

102105

@@ -119,7 +122,7 @@ def test_detect_base_merge_queue_override(
119122

120123
result = detector.detect()
121124

122-
assert result == detector.References("xyz789", "HEAD", is_merge_queue=True)
125+
assert result == detector.References("xyz789", "HEAD", "merge_queue")
123126

124127

125128
def test_detect_base_no_info(
@@ -194,4 +197,4 @@ def test_detect_unhandled_event(
194197

195198
result = detector.detect()
196199

197-
assert result == detector.References(None, "HEAD", is_merge_queue=False)
200+
assert result == detector.References(None, "HEAD", "github_event_other")

mergify_cli/tests/ci/scopes/test_cli.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import respx
99
import yaml
1010

11+
from mergify_cli.ci.git_refs import detector as git_refs_detector
1112
from mergify_cli.ci.scopes import cli
1213
from mergify_cli.ci.scopes import config
1314
from mergify_cli.ci.scopes import exceptions
@@ -313,10 +314,18 @@ def test_maybe_write_github_step_summary(
313314
all_scopes = ["backend", "frontend", "docs"]
314315
scopes_hit = {"backend", "docs"}
315316

316-
cli.maybe_write_github_step_summary("da26838", "HEAD", all_scopes, scopes_hit)
317+
cli.maybe_write_github_step_summary(
318+
git_refs_detector.References(
319+
"da26838azertyuiopqsdfghjkxcvbn",
320+
"HEAD",
321+
"github_event_pull_request",
322+
),
323+
all_scopes,
324+
scopes_hit,
325+
)
317326

318327
content = output_file.read_text()
319-
expected = """## Mergify CI Scope Matching Results for `da26838...HEAD`
328+
expected = """## Mergify CI Scope Matching Results for `da26838...HEAD` (source: `github_event_pull_request`)
320329
321330
| 🎯 Scope | ✅ Match |
322331
|:--|:--|
@@ -364,9 +373,7 @@ def test_detect_with_matches(
364373
with mock.patch("click.echo") as mock_echo:
365374
result = cli.detect(
366375
str(config_file),
367-
base="old",
368-
head="new",
369-
is_merge_queue=True,
376+
references=git_refs_detector.References("old", "new", "merge_queue"),
370377
)
371378

372379
# Verify calls
@@ -403,9 +410,11 @@ def test_detect_manual(
403410
):
404411
cli.detect(
405412
str(config_file),
406-
base="old",
407-
head="new",
408-
is_merge_queue=False,
413+
references=git_refs_detector.References(
414+
"old",
415+
"new",
416+
"github_event_pull_request",
417+
),
409418
)
410419

411420

@@ -432,14 +441,17 @@ def test_detect_no_base(
432441
with mock.patch("click.echo") as mock_echo:
433442
result = cli.detect(
434443
str(config_file),
435-
base=None,
436-
head="HEAD",
437-
is_merge_queue=False,
444+
references=git_refs_detector.References(
445+
None,
446+
"HEAD",
447+
"fallback_last_commit",
448+
),
438449
)
439450

440451
# Verify output
441452
calls = [call.args[0] for call in mock_echo.call_args_list]
442453
assert "Head: HEAD" in calls
454+
assert "Source: fallback_last_commit" in calls
443455
assert "Scopes touched:" in calls
444456
assert "- backend" in calls
445457
assert "- frontend" in calls
@@ -468,9 +480,11 @@ def test_detect_no_matches(
468480
with mock.patch("click.echo") as mock_echo:
469481
result = cli.detect(
470482
str(config_file),
471-
base="old",
472-
head="new",
473-
is_merge_queue=False,
483+
references=git_refs_detector.References(
484+
"old",
485+
"new",
486+
"github_event_pull_request",
487+
),
474488
)
475489

476490
# Verify output
@@ -520,9 +534,11 @@ def test_detect_debug_output(
520534
with mock.patch("click.echo") as mock_echo:
521535
result = cli.detect(
522536
str(config_file),
523-
base="old",
524-
head="new",
525-
is_merge_queue=False,
537+
references=git_refs_detector.References(
538+
"old",
539+
"new",
540+
"github_event_pull_request",
541+
),
526542
)
527543
# Verify debug output includes file details
528544
calls = [call.args[0] for call in mock_echo.call_args_list]

0 commit comments

Comments
 (0)