Skip to content

Commit 60005a6

Browse files
authored
fix(jenkins): get git branch correctly (#736)
Fixes MRGFY-5595
1 parent 99d00e1 commit 60005a6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mergify_cli/ci/detector.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from mergify_cli import utils
99

1010

11+
GIT_BRANCH_PREFIXES = ("origin/", "refs/heads/")
12+
1113
CIProviderT = typing.Literal["github_actions", "circleci", "jenkins"]
1214

1315

@@ -40,14 +42,27 @@ def get_job_name() -> str | None:
4042
return None
4143

4244

45+
def get_jenkins_head_ref_name() -> str | None:
46+
branch = os.getenv("GIT_BRANCH")
47+
if branch:
48+
# NOTE(sileht): it's not 100% bullet proof but since it's very complicated
49+
# and unlikely to change/add a remote with Jenkins Git/GitHub plugins,
50+
# we just handle the most common cases.
51+
for prefix in GIT_BRANCH_PREFIXES:
52+
if branch.startswith(prefix):
53+
return branch[len(prefix) :]
54+
return branch
55+
return None
56+
57+
4358
def get_head_ref_name() -> str | None:
4459
match get_ci_provider():
4560
case "github_actions":
4661
return os.getenv("GITHUB_REF_NAME")
4762
case "circleci":
4863
return os.getenv("CIRCLE_BRANCH")
4964
case "jenkins":
50-
return os.getenv("GIT_BRANCH")
65+
return get_jenkins_head_ref_name()
5166
case _:
5267
return None
5368

mergify_cli/tests/ci/test_detector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
PULL_REQUEST_EVENT = pathlib.Path(__file__).parent / "pull_request.json"
1010

1111

12+
def test_get_head_branch_jenkins(monkeypatch: pytest.MonkeyPatch) -> None:
13+
monkeypatch.setenv("GIT_BRANCH", "origin/main")
14+
15+
assert detector.get_jenkins_head_ref_name() == "main"
16+
17+
1218
def test_get_head_sha_github_actions(monkeypatch: pytest.MonkeyPatch) -> None:
1319
monkeypatch.setenv("GITHUB_ACTIONS", "true")
1420
monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request")

0 commit comments

Comments
 (0)