diff --git a/pytest_mergify/ci_insights.py b/pytest_mergify/ci_insights.py index 9a3bd31..f0d5649 100644 --- a/pytest_mergify/ci_insights.py +++ b/pytest_mergify/ci_insights.py @@ -151,11 +151,12 @@ def __post_init__(self) -> None: self.tracer_provider.add_span_processor(span_processor) self.tracer = self.tracer_provider.get_tracer("pytest-mergify") - # Retrieve the branch name, preferring base ref (target branch) over head ref. + # Retrieve the branch name, preferring base ref (target branch) over + # head ref. `or` ensures an empty base ref (e.g. `GITHUB_BASE_REF=""` on + # push runs) falls through to the head ref. branch_name = resource.attributes.get( - vcs_attributes.VCS_REF_BASE_NAME, - resource.attributes.get(vcs_attributes.VCS_REF_HEAD_NAME), - ) + vcs_attributes.VCS_REF_BASE_NAME + ) or resource.attributes.get(vcs_attributes.VCS_REF_HEAD_NAME) if branch_name is not None: # `str` cast just for `mypy`. self.branch_name = str(branch_name) diff --git a/tests/test_ci_insights.py b/tests/test_ci_insights.py index 2ea232f..6932b5a 100644 --- a/tests/test_ci_insights.py +++ b/tests/test_ci_insights.py @@ -685,3 +685,35 @@ def test_bar(): plugin.mergify_ci.flaky_detector._available_budget_duration.total_seconds() == datetime.timedelta(seconds=5).total_seconds() ) + + +@responses.activate +def test_empty_base_ref_falls_through_to_head_ref( + monkeypatch: pytest.MonkeyPatch, + pytester: _pytest.pytester.Pytester, +) -> None: + """When `GITHUB_BASE_REF` is empty (push/scheduled runs), `branch_name` + should fall through to the HEAD ref so quarantine still works.""" + _set_test_environment(monkeypatch, mode="unhealthy") + + # Set an empty `GITHUB_BASE_REF` to simulate push runs where the env var + # exists but is empty. + monkeypatch.setenv("GITHUB_BASE_REF", "") + + _make_quarantine_mock() + _make_flaky_detection_context_mock() + + pytester.makepyfile( + """ + def test_foo(): + assert True + """ + ) + + plugin = pytest_mergify.PytestMergify() + pytester.runpytest_inprocess(plugins=[plugin]) + + # `branch_name` should come from HEAD ref, not the empty base ref. + assert plugin.mergify_ci.branch_name == "main" + assert plugin.mergify_ci.flaky_detector is not None + assert plugin.mergify_ci.flaky_detector.mode == "unhealthy"