File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed
Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 88from mergify_cli import utils
99
1010
11+ GIT_BRANCH_PREFIXES = ("origin/" , "refs/heads/" )
12+
1113CIProviderT = 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+
4358def 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
Original file line number Diff line number Diff line change 99PULL_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+
1218def test_get_head_sha_github_actions (monkeypatch : pytest .MonkeyPatch ) -> None :
1319 monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
1420 monkeypatch .setenv ("GITHUB_EVENT_NAME" , "pull_request" )
You can’t perform that action at this time.
0 commit comments