Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions pytest_mergify/ci_insights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions tests/test_ci_insights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"