Skip to content

Commit ac25092

Browse files
remyduthuclaude
andauthored
fix(ci-insights): Use GITHUB_HEAD_REF instead of GITHUB_REF_NAME (#355)
`GITHUB_REF_NAME` contains `<pr_number>/merge` in pull request contexts instead of the original branch name. `GITHUB_HEAD_REF` provides the actual source branch name for pull requests. See: github/docs#15319 Since `GITHUB_HEAD_REF` is only set for PR events, we fall back to `GITHUB_REF_NAME` o ensure branch detection keeps working in all contexts. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 67e173a commit ac25092

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

pytest_mergify/resources/github_actions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def _get_repository_url() -> typing.Optional[str]:
2626
return None
2727

2828

29+
def _get_head_ref_name() -> typing.Optional[str]:
30+
# `GITHUB_HEAD_REF` contains the actual branch name for PRs, while
31+
# `GITHUB_REF_NAME` contains `<pr_number>/merge`. However, `GITHUB_HEAD_REF`
32+
# is only set for PR events, so we fall back to `GITHUB_REF_NAME`.
33+
return os.getenv("GITHUB_HEAD_REF") or os.getenv("GITHUB_REF_NAME")
34+
35+
2936
class GitHubActionsResourceDetector(ResourceDetector):
3037
"""Detects OpenTelemetry Resource attributes for GitHub Actions."""
3138

@@ -35,7 +42,7 @@ class GitHubActionsResourceDetector(ResourceDetector):
3542
cicd_attributes.CICD_PIPELINE_RUN_ID: (int, "GITHUB_RUN_ID"),
3643
"cicd.pipeline.run.attempt": (int, "GITHUB_RUN_ATTEMPT"),
3744
"cicd.pipeline.runner.name": (str, "RUNNER_NAME"),
38-
vcs_attributes.VCS_REF_HEAD_NAME: (str, "GITHUB_REF_NAME"),
45+
vcs_attributes.VCS_REF_HEAD_NAME: (str, _get_head_ref_name),
3946
vcs_attributes.VCS_REF_HEAD_TYPE: (str, "GITHUB_REF_TYPE"),
4047
vcs_attributes.VCS_REF_BASE_NAME: (str, "GITHUB_BASE_REF"),
4148
"vcs.repository.name": (str, "GITHUB_REPOSITORY"),

tests/test_ci_insights.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ def _set_test_environment(
2727
monkeypatch.setenv("MERGIFY_TOKEN", "my_token")
2828

2929
if mode == "unhealthy":
30-
# Simulate absence of a PR context: without `GITHUB_BASE_REF` and
31-
# `GITHUB_REF_NAME` `MergifyCIInsights.branch_name` can't be derived,
30+
# Simulate absence of a PR context: without `GITHUB_BASE_REF` and branch
31+
# ref variables, `MergifyCIInsights.branch_name` can't be derived,
3232
# forcing the flaky detector to fall back to `unhealthy` mode. This
3333
# explicitly exercises the fallback path used when no PR metadata is
3434
# available.
3535
monkeypatch.delenv("GITHUB_BASE_REF", raising=False)
36+
monkeypatch.delenv("GITHUB_HEAD_REF", raising=False)
3637
monkeypatch.delenv("GITHUB_REF_NAME", raising=False)
3738

3839

tests/test_resources.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from pytest_mergify import utils
8+
from pytest_mergify.resources import github_actions
89
from tests import conftest
910

1011

@@ -78,6 +79,32 @@ def test_span_github_actions(
7879
)
7980

8081

82+
@pytest.mark.parametrize(
83+
("head_ref", "ref_name", "expected"),
84+
[
85+
pytest.param("feature-branch", "123/merge", "feature-branch", id="PR context"),
86+
pytest.param(None, "main", "main", id="Without `GITHUB_REF_NAME`"),
87+
pytest.param("", "main", "main", id="Empty `GITHUB_REF_NAME`"),
88+
pytest.param(None, None, None, id="Without any variable"),
89+
],
90+
)
91+
def test_get_head_ref_name(
92+
monkeypatch: pytest.MonkeyPatch,
93+
head_ref: typing.Optional[str],
94+
ref_name: typing.Optional[str],
95+
expected: typing.Optional[str],
96+
) -> None:
97+
monkeypatch.delenv("GITHUB_HEAD_REF", raising=False)
98+
monkeypatch.delenv("GITHUB_REF_NAME", raising=False)
99+
100+
if head_ref:
101+
monkeypatch.setenv("GITHUB_HEAD_REF", head_ref)
102+
if ref_name:
103+
monkeypatch.setenv("GITHUB_REF_NAME", ref_name)
104+
105+
assert github_actions._get_head_ref_name() == expected
106+
107+
81108
@mock.patch("pytest_mergify.utils.git", return_value=None)
82109
def test_span_jenkins(
83110
git: mock.Mock,

0 commit comments

Comments
 (0)