Skip to content

Commit 5ce95e2

Browse files
committed
feat: Add support for gerrit changes for download Jenkins file
We want to support the verification pipeline when the Jenkinsfile is part of the modification itself. This allow to test the pipeline on any project and test it. Signed-off-by: Michael Trimarchi <michael@amarulasolutions.com>
1 parent 93c9316 commit 5ce95e2

5 files changed

Lines changed: 43 additions & 0 deletions

File tree

src/main/java/hudson/plugins/git/BranchSpec.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ private Pattern getPattern(EnvVars env, String repositoryName) {
159159
String regexSubstring = expandedName.substring(1, expandedName.length());
160160
return Pattern.compile(regexSubstring);
161161
}
162+
if (expandedName.startsWith("refs/changes/")) {
163+
return Pattern.compile(Pattern.quote(expandedName));
164+
}
162165
if (repositoryName != null) {
163166
// remove the "refs/.../" stuff from the branch-spec if necessary
164167
String pattern = cutRefs(expandedName)

src/main/java/jenkins/plugins/git/GitSCMFileSystem.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ public boolean supports(SCM source) {
267267
|| gscm.getBranches().get(0).getName().matches(
268268
"^((\\Q" + Constants.R_TAGS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
269269
)
270+
|| gscm.getBranches().get(0).getName().matches(
271+
"^((\\Qrefs/changes/\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
272+
)
270273
);
271274
// we only support where the branch spec is obvious and not a wildcard
272275
}
@@ -306,6 +309,8 @@ static HeadNameResult calculate(@NonNull BranchSpec branchSpec,
306309
String prefix = Constants.R_HEADS;
307310
if (branchSpecExpandedName.startsWith(Constants.R_TAGS)) {
308311
prefix = Constants.R_TAGS;
312+
} else if (branchSpecExpandedName.startsWith("refs/changes")) {
313+
prefix = "refs/changes/";
309314
}
310315

311316
String headName;

src/main/resources/hudson/plugins/git/BranchSpec/help-name.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
Tracks/checks out the specified tag.<br/>
4848
E.g. <code>refs/tags/git-2.3.0</code>
4949
</li>
50+
<li> <strong><code>refs/changes/&lt;gerritChange&gt;</code></strong><br/>
51+
Tracks/checks out the specified gerrit change.<br/>
52+
E.g. <code>refs/changes/91/45391/1</code>
53+
</li>
5054
<li> <strong><code>&lt;commitId&gt;</code></strong><br/>
5155
Checks out the specified commit.<br/>
5256
E.g. <code>5062ac843f2b947733e6a3b105977056821bd352</code>, <code>5062ac84</code>, ...

src/test/java/hudson/plugins/git/BranchSpecTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ void testUsesRefsHeads() {
149149
assertFalse(m.matches("remote/origin/jane"));
150150
}
151151

152+
@Test
153+
public void testMatchesGerritChangeRef() {
154+
BranchSpec spec = new BranchSpec("refs/changes/91/45391/1");
155+
156+
assertTrue(spec.matches("refs/changes/91/45391/1"));
157+
assertFalse(spec.matches("refs/heads/refs/changes/91/45391/1"));
158+
}
159+
152160
@Test
153161
void testUsesJavaPatternDirectlyIfPrefixedWithColon() {
154162
BranchSpec m = new BranchSpec(":^(?!(origin/prefix)).*");

src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,29 @@ void create_SCMFileSystem_from_tag() throws Exception {
435435
assertThat(file.contentAsString(), is("modified"));
436436
}
437437

438+
@Test
439+
public void testSupportsGerritChangeRef() throws Exception {
440+
// Create a dummy GitSCM configured with a Gerrit change refspec
441+
GitSCM scm = new GitSCM(
442+
GitSCM.createRepoList("https://example.com/repo.git", null),
443+
Collections.singletonList(new BranchSpec("refs/changes/91/45391/1")),
444+
null, null, Collections.emptyList());
445+
446+
GitSCMFileSystem.BuilderImpl builder = new GitSCMFileSystem.BuilderImpl();
447+
assertTrue(builder.supports(scm), "Builder should support refs/changes/ branch specs");
448+
}
449+
450+
@Test
451+
public void testHeadNameResultCalculateGerritChange() {
452+
BranchSpec spec = new BranchSpec("refs/changes/91/45391/1");
453+
454+
GitSCMFileSystem.BuilderImpl.HeadNameResult result =
455+
GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(spec, null, null);
456+
457+
assertEquals("refs/changes/", result.prefix);
458+
assertEquals("91/45391/1", result.headName);
459+
}
460+
438461
@Issue("JENKINS-52964")
439462
@Test
440463
void filesystem_supports_descriptor() throws Exception {

0 commit comments

Comments
 (0)