From cc02c2f217b6bc332b7c0946c0d51d04056ccfb9 Mon Sep 17 00:00:00 2001 From: Martin Kosicky Date: Tue, 2 Aug 2022 13:30:38 +0200 Subject: [PATCH 01/14] [JENKINS-42971] GitSCMFileSystem consumes environment and expands branch name --- pom.xml | 10 +++++++ .../jenkins/plugins/git/GitSCMFileSystem.java | 27 ++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 46f2941174..8fed866238 100644 --- a/pom.xml +++ b/pom.xml @@ -102,6 +102,7 @@ org.jenkins-ci.plugins + 625.va_c59c48ce36f scm-api @@ -191,6 +192,7 @@ org.jenkins-ci.plugins scm-api + 625.va_c59c48ce36f tests test @@ -255,12 +257,20 @@ repo.jenkins-ci.org https://repo.jenkins-ci.org/public/ + + incrementals.jenkins-ci.org + https://repo.jenkins-ci.org/incrementals/ + repo.jenkins-ci.org https://repo.jenkins-ci.org/public/ + + incrementals.jenkins-ci.org + https://repo.jenkins-ci.org/incrementals/ + diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index e585139d0b..efaf171a58 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -35,6 +35,7 @@ import hudson.EnvVars; import hudson.Extension; import hudson.model.Item; +import hudson.model.Run; import hudson.model.TaskListener; import hudson.plugins.git.BranchSpec; import hudson.plugins.git.GitException; @@ -284,7 +285,8 @@ public boolean supportsDescriptor(SCMSourceDescriptor descriptor) { } @Override - public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) + public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev, + @CheckForNull Run _build) throws IOException, InterruptedException { if (rev != null && !(rev instanceof AbstractGitSCMSource.SCMRevisionImpl)) { return null; @@ -292,6 +294,7 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull if (!(scm instanceof GitSCM)) { return null; // Spotbugs warns about unchecked cast without this check } + GitSCM gitSCM = (GitSCM) scm; UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); BranchSpec branchSpec = gitSCM.getBranches().get(0); @@ -301,6 +304,13 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull listener.getLogger().println("Git remote url is null"); return null; } + + EnvVars env = null; + if (_build != null) + { + env = _build.getEnvironment(listener); + } + String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote); Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry); cacheLock.lock(); @@ -353,12 +363,17 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull if (rev != null) { headName = rev.getHead().getName(); } else { - if (branchSpec.getName().startsWith(prefix)){ - headName = branchSpec.getName().substring(prefix.length()); - } else if (branchSpec.getName().startsWith("*/")) { - headName = branchSpec.getName().substring(2); + String branchSpecExpandedName = branchSpec.getName(); + if (env != null) { + branchSpecExpandedName = env.expand(branchSpecExpandedName); + } + + if (branchSpecExpandedName.startsWith(prefix)){ + headName = branchSpecExpandedName.substring(prefix.length()); + } else if (branchSpecExpandedName.startsWith("*/")) { + headName = branchSpecExpandedName.substring(2); } else { - headName = branchSpec.getName(); + headName = branchSpecExpandedName; } } client.fetch_().prune(true).from(remoteURI, Arrays From a69a857d032a200b1d4132c3e6da8511a02982fb Mon Sep 17 00:00:00 2001 From: Martin Kosicky Date: Tue, 2 Aug 2022 14:05:17 +0200 Subject: [PATCH 02/14] Raised jenkins.version in POM 2 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 8fed866238..505c0e9790 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ 4.11.5 -SNAPSHOT jenkinsci/${project.artifactId}-plugin - 2.303.3 + 2.319.3 false true false From 27aa68eae4435ca2269047678286a26c78cf94e2 Mon Sep 17 00:00:00 2001 From: MartinKosicky Date: Tue, 2 Aug 2022 17:17:20 +0200 Subject: [PATCH 03/14] Update pom.xml Co-authored-by: Jesse Glick --- pom.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pom.xml b/pom.xml index 505c0e9790..972396ea1a 100644 --- a/pom.xml +++ b/pom.xml @@ -257,10 +257,6 @@ repo.jenkins-ci.org https://repo.jenkins-ci.org/public/ - - incrementals.jenkins-ci.org - https://repo.jenkins-ci.org/incrementals/ - From b04e8f73970fb788de99163b839253a3bbd02066 Mon Sep 17 00:00:00 2001 From: Martin Kosicky Date: Wed, 3 Aug 2022 19:46:59 +0200 Subject: [PATCH 04/14] Added testing of GitSCMFileSystem branch expansion using env vars --- .../jenkins/plugins/git/GitSCMFileSystem.java | 79 +++++++++++++------ .../plugins/git/GitSCMFileSystemTest.java | 39 +++++++++ 2 files changed, 94 insertions(+), 24 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index efaf171a58..250c115eb3 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -284,6 +284,55 @@ public boolean supportsDescriptor(SCMSourceDescriptor descriptor) { return AbstractGitSCMSource.class.isAssignableFrom(descriptor.clazz); } + public static class HeadNameResult + { + private final String headName; + private final String prefix; + + public HeadNameResult(String headName, String prefix) { + this.headName = headName; + this.prefix = prefix; + } + + public String getHeadName() { + return headName; + } + + public String getPrefix() { + return prefix; + } + } + + public static HeadNameResult calculateHeadName(@NonNull BranchSpec branchSpec, + @CheckForNull SCMRevision rev, + @CheckForNull EnvVars env) + { + String branchSpecExpandedName = branchSpec.getName(); + if (env != null) { + branchSpecExpandedName = env.expand(branchSpecExpandedName); + } + + String prefix = Constants.R_HEADS; + if(branchSpecExpandedName.startsWith(Constants.R_TAGS)){ + prefix = Constants.R_TAGS; + } + + String headName; + if (rev != null) { + headName = env.expand(rev.getHead().getName()); + } else { + if (branchSpecExpandedName.startsWith(prefix)){ + headName = branchSpecExpandedName.substring(prefix.length()); + } else if (branchSpecExpandedName.startsWith("*/")) { + headName = branchSpecExpandedName.substring(2); + } else { + headName = branchSpecExpandedName; + } + } + + return new HeadNameResult(headName, prefix); + } + @Override public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev, @CheckForNull Run _build) @@ -355,33 +404,15 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull } catch (URISyntaxException ex) { listener.getLogger().println("URI syntax exception for '" + remoteName + "' " + ex); } - String prefix = Constants.R_HEADS; - if(branchSpec.getName().startsWith(Constants.R_TAGS)){ - prefix = Constants.R_TAGS; - } - String headName; - if (rev != null) { - headName = rev.getHead().getName(); - } else { - String branchSpecExpandedName = branchSpec.getName(); - if (env != null) { - branchSpecExpandedName = env.expand(branchSpecExpandedName); - } - - if (branchSpecExpandedName.startsWith(prefix)){ - headName = branchSpecExpandedName.substring(prefix.length()); - } else if (branchSpecExpandedName.startsWith("*/")) { - headName = branchSpecExpandedName.substring(2); - } else { - headName = branchSpecExpandedName; - } - } + + HeadNameResult headNameResult = calculateHeadName(branchSpec, rev, env); + client.fetch_().prune(true).from(remoteURI, Arrays .asList(new RefSpec( - "+" + prefix + headName + ":" + Constants.R_REMOTES + remoteName + "/" - + headName))).execute(); + "+" + headNameResult.getPrefix() + headNameResult.getHeadName() + ":" + Constants.R_REMOTES + remoteName + "/" + + headNameResult.getHeadName()))).execute(); listener.getLogger().println("Done."); - return new GitSCMFileSystem(client, remote, Constants.R_REMOTES + remoteName + "/" +headName, (AbstractGitSCMSource.SCMRevisionImpl) rev); + return new GitSCMFileSystem(client, remote, Constants.R_REMOTES + remoteName + "/" + headNameResult.getHeadName(), (AbstractGitSCMSource.SCMRevisionImpl) rev); } finally { cacheLock.unlock(); } diff --git a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java index 921c5b2120..17b04ca087 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java @@ -43,6 +43,8 @@ import jenkins.scm.api.SCMRevision; import jenkins.scm.api.SCMSource; import jenkins.scm.api.SCMSourceDescriptor; + +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.jenkinsci.plugins.gitclient.Git; import org.jenkinsci.plugins.gitclient.GitClient; @@ -427,6 +429,43 @@ public void filesystem_supports_descriptor() throws Exception { assertTrue(SCMFileSystem.supports(descriptor)); } + @Issue("JENKINS-42971") + @Test + public void calculate_head_name_with_env() throws Exception + { + GitSCMFileSystem.BuilderImpl.HeadNameResult result1 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("${BRANCH}"), null, + new EnvVars("BRANCH","master")); + assertEquals(result1.getHeadName(),"master"); + assertEquals(result1.getPrefix(), Constants.R_HEADS); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result2 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("${BRANCH}"), null, + new EnvVars("BRANCH","refs/heads/master")); + assertEquals("master", result2.getHeadName()); + assertEquals(Constants.R_HEADS, result2.getPrefix()); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result3 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("refs/heads/${BRANCH}"), null, + new EnvVars("BRANCH","master")); + assertEquals("master", result3.getHeadName()); + assertEquals(Constants.R_HEADS, result3.getPrefix()); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result4 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("${BRANCH}"), null, + null); + assertEquals(result4.getHeadName(),"${BRANCH}"); + assertEquals(result4.getPrefix(), Constants.R_HEADS); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result5 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("*/${BRANCH}"), null, + new EnvVars("BRANCH","master")); + assertEquals("master", result5.getHeadName()); + assertEquals(Constants.R_HEADS, result5.getPrefix()); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result6 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("*/master"), null, + new EnvVars("BRANCH","dummy")); + assertEquals("master", result6.getHeadName()); + assertEquals(Constants.R_HEADS, result6.getPrefix()); + + + } + /** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */ private boolean isWindows() { return java.io.File.pathSeparatorChar==';'; From d7159dc8566217e0e161fb67c8771585ade67f95 Mon Sep 17 00:00:00 2001 From: Martin Kosicky Date: Wed, 3 Aug 2022 19:49:14 +0200 Subject: [PATCH 05/14] Removed unnecesary plugin incrementals in pom.xml . --- pom.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pom.xml b/pom.xml index 972396ea1a..bc1227e40b 100644 --- a/pom.xml +++ b/pom.xml @@ -263,10 +263,6 @@ repo.jenkins-ci.org https://repo.jenkins-ci.org/public/ - - incrementals.jenkins-ci.org - https://repo.jenkins-ci.org/incrementals/ - From 6111ed506aee6688117677c748df25221d9be8cf Mon Sep 17 00:00:00 2001 From: MartinKosicky Date: Fri, 5 Aug 2022 17:14:42 +0200 Subject: [PATCH 06/14] Apply suggestions from code review (code format cleanup) Co-authored-by: Jesse Glick --- .../java/jenkins/plugins/git/GitSCMFileSystem.java | 8 +++----- .../java/jenkins/plugins/git/GitSCMFileSystemTest.java | 10 ++++------ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 250c115eb3..aac082df0c 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -313,7 +313,7 @@ public static HeadNameResult calculateHeadName(@NonNull BranchSpec branchSpec, } String prefix = Constants.R_HEADS; - if(branchSpecExpandedName.startsWith(Constants.R_TAGS)){ + if (branchSpecExpandedName.startsWith(Constants.R_TAGS)){ prefix = Constants.R_TAGS; } @@ -321,7 +321,7 @@ public static HeadNameResult calculateHeadName(@NonNull BranchSpec branchSpec, if (rev != null) { headName = env.expand(rev.getHead().getName()); } else { - if (branchSpecExpandedName.startsWith(prefix)){ + if (branchSpecExpandedName.startsWith(prefix)) { headName = branchSpecExpandedName.substring(prefix.length()); } else if (branchSpecExpandedName.startsWith("*/")) { headName = branchSpecExpandedName.substring(2); @@ -343,7 +343,6 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull if (!(scm instanceof GitSCM)) { return null; // Spotbugs warns about unchecked cast without this check } - GitSCM gitSCM = (GitSCM) scm; UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); BranchSpec branchSpec = gitSCM.getBranches().get(0); @@ -355,8 +354,7 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull } EnvVars env = null; - if (_build != null) - { + if (_build != null) { env = _build.getEnvironment(listener); } diff --git a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java index 17b04ca087..5d89d40176 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java @@ -435,8 +435,8 @@ public void calculate_head_name_with_env() throws Exception { GitSCMFileSystem.BuilderImpl.HeadNameResult result1 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("${BRANCH}"), null, new EnvVars("BRANCH","master")); - assertEquals(result1.getHeadName(),"master"); - assertEquals(result1.getPrefix(), Constants.R_HEADS); + assertEquals("master", result1.getHeadName()); + assertEquals(Constants.R_HEADS, result1.getPrefix()); GitSCMFileSystem.BuilderImpl.HeadNameResult result2 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("${BRANCH}"), null, new EnvVars("BRANCH","refs/heads/master")); @@ -450,8 +450,8 @@ public void calculate_head_name_with_env() throws Exception GitSCMFileSystem.BuilderImpl.HeadNameResult result4 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("${BRANCH}"), null, null); - assertEquals(result4.getHeadName(),"${BRANCH}"); - assertEquals(result4.getPrefix(), Constants.R_HEADS); + assertEquals("${BRANCH}", result4.getHeadName()); + assertEquals(Constants.R_HEADS, result4.getPrefix()); GitSCMFileSystem.BuilderImpl.HeadNameResult result5 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("*/${BRANCH}"), null, new EnvVars("BRANCH","master")); @@ -462,8 +462,6 @@ public void calculate_head_name_with_env() throws Exception new EnvVars("BRANCH","dummy")); assertEquals("master", result6.getHeadName()); assertEquals(Constants.R_HEADS, result6.getPrefix()); - - } /** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */ From 5f84f6bd86977ce045be1f0e4f9bc1e15f66c63b Mon Sep 17 00:00:00 2001 From: Martin Kosicky Date: Fri, 5 Aug 2022 17:34:31 +0200 Subject: [PATCH 07/14] HeadNameResult refactor --- .../jenkins/plugins/git/GitSCMFileSystem.java | 71 ++++++++----------- .../plugins/git/GitSCMFileSystemTest.java | 64 +++++++++-------- 2 files changed, 64 insertions(+), 71 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index aac082df0c..7309c050f9 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -284,53 +284,42 @@ public boolean supportsDescriptor(SCMSourceDescriptor descriptor) { return AbstractGitSCMSource.class.isAssignableFrom(descriptor.clazz); } - public static class HeadNameResult - { - private final String headName; - private final String prefix; + static class HeadNameResult { + final String headName; + final String prefix; - public HeadNameResult(String headName, String prefix) { + private HeadNameResult(String headName, String prefix) { this.headName = headName; this.prefix = prefix; } - public String getHeadName() { - return headName; - } - - public String getPrefix() { - return prefix; - } - } - - public static HeadNameResult calculateHeadName(@NonNull BranchSpec branchSpec, - @CheckForNull SCMRevision rev, - @CheckForNull EnvVars env) - { - String branchSpecExpandedName = branchSpec.getName(); - if (env != null) { - branchSpecExpandedName = env.expand(branchSpecExpandedName); - } + static HeadNameResult calculate(@NonNull BranchSpec branchSpec, + @CheckForNull SCMRevision rev, + @CheckForNull EnvVars env) { + String branchSpecExpandedName = branchSpec.getName(); + if (env != null) { + branchSpecExpandedName = env.expand(branchSpecExpandedName); + } - String prefix = Constants.R_HEADS; - if (branchSpecExpandedName.startsWith(Constants.R_TAGS)){ - prefix = Constants.R_TAGS; - } + String prefix = Constants.R_HEADS; + if (branchSpecExpandedName.startsWith(Constants.R_TAGS)) { + prefix = Constants.R_TAGS; + } - String headName; - if (rev != null) { - headName = env.expand(rev.getHead().getName()); - } else { - if (branchSpecExpandedName.startsWith(prefix)) { - headName = branchSpecExpandedName.substring(prefix.length()); - } else if (branchSpecExpandedName.startsWith("*/")) { - headName = branchSpecExpandedName.substring(2); + String headName; + if (rev != null) { + headName = env.expand(rev.getHead().getName()); } else { - headName = branchSpecExpandedName; + if (branchSpecExpandedName.startsWith(prefix)) { + headName = branchSpecExpandedName.substring(prefix.length()); + } else if (branchSpecExpandedName.startsWith("*/")) { + headName = branchSpecExpandedName.substring(2); + } else { + headName = branchSpecExpandedName; + } } + return new HeadNameResult(headName, prefix); } - - return new HeadNameResult(headName, prefix); } @Override @@ -403,14 +392,14 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull listener.getLogger().println("URI syntax exception for '" + remoteName + "' " + ex); } - HeadNameResult headNameResult = calculateHeadName(branchSpec, rev, env); + HeadNameResult headNameResult = HeadNameResult.calculate(branchSpec, rev, env); client.fetch_().prune(true).from(remoteURI, Arrays .asList(new RefSpec( - "+" + headNameResult.getPrefix() + headNameResult.getHeadName() + ":" + Constants.R_REMOTES + remoteName + "/" - + headNameResult.getHeadName()))).execute(); + "+" + headNameResult.prefix + headNameResult.headName + ":" + Constants.R_REMOTES + remoteName + "/" + + headNameResult.headName))).execute(); listener.getLogger().println("Done."); - return new GitSCMFileSystem(client, remote, Constants.R_REMOTES + remoteName + "/" + headNameResult.getHeadName(), (AbstractGitSCMSource.SCMRevisionImpl) rev); + return new GitSCMFileSystem(client, remote, Constants.R_REMOTES + remoteName + "/" + headNameResult.headName, (AbstractGitSCMSource.SCMRevisionImpl) rev); } finally { cacheLock.unlock(); } diff --git a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java index 5d89d40176..96af3c6385 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java @@ -431,37 +431,41 @@ public void filesystem_supports_descriptor() throws Exception { @Issue("JENKINS-42971") @Test - public void calculate_head_name_with_env() throws Exception - { - GitSCMFileSystem.BuilderImpl.HeadNameResult result1 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("${BRANCH}"), null, - new EnvVars("BRANCH","master")); - assertEquals("master", result1.getHeadName()); - assertEquals(Constants.R_HEADS, result1.getPrefix()); - - GitSCMFileSystem.BuilderImpl.HeadNameResult result2 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("${BRANCH}"), null, - new EnvVars("BRANCH","refs/heads/master")); - assertEquals("master", result2.getHeadName()); - assertEquals(Constants.R_HEADS, result2.getPrefix()); - - GitSCMFileSystem.BuilderImpl.HeadNameResult result3 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("refs/heads/${BRANCH}"), null, - new EnvVars("BRANCH","master")); - assertEquals("master", result3.getHeadName()); - assertEquals(Constants.R_HEADS, result3.getPrefix()); - - GitSCMFileSystem.BuilderImpl.HeadNameResult result4 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("${BRANCH}"), null, + public void calculate_head_name_with_env() throws Exception { + GitSCMFileSystem.BuilderImpl.HeadNameResult result1 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, + new EnvVars("BRANCH", "master")); + assertEquals("master", result1.headName); + assertEquals(Constants.R_HEADS, result1.prefix); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result2 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, + new EnvVars("BRANCH", "refs/heads/master")); + assertEquals("master", result2.headName); + assertEquals(Constants.R_HEADS, result2.prefix); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result3 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("refs/heads/${BRANCH}"), null, + new EnvVars("BRANCH", "master")); + assertEquals("master", result3.headName); + assertEquals(Constants.R_HEADS, result3.prefix); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result4 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, null); - assertEquals("${BRANCH}", result4.getHeadName()); - assertEquals(Constants.R_HEADS, result4.getPrefix()); - - GitSCMFileSystem.BuilderImpl.HeadNameResult result5 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("*/${BRANCH}"), null, - new EnvVars("BRANCH","master")); - assertEquals("master", result5.getHeadName()); - assertEquals(Constants.R_HEADS, result5.getPrefix()); - - GitSCMFileSystem.BuilderImpl.HeadNameResult result6 = GitSCMFileSystem.BuilderImpl.calculateHeadName(new BranchSpec("*/master"), null, - new EnvVars("BRANCH","dummy")); - assertEquals("master", result6.getHeadName()); - assertEquals(Constants.R_HEADS, result6.getPrefix()); + assertEquals("${BRANCH}", result4.headName); + assertEquals(Constants.R_HEADS, result4.prefix); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result5 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("*/${BRANCH}"), null, + new EnvVars("BRANCH", "master")); + assertEquals("master", result5.headName); + assertEquals(Constants.R_HEADS, result5.prefix); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result6 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("*/master"), null, + new EnvVars("BRANCH", "dummy")); + assertEquals("master", result6.headName); + assertEquals(Constants.R_HEADS, result6.prefix); + + GitSCMFileSystem.BuilderImpl.HeadNameResult result7 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("*/master"), null, + new EnvVars("BRANCH", "dummy")); + assertEquals("master", result6.headName); + assertEquals(Constants.R_HEADS, result6.prefix); } /** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */ From 2b17a1c80897ad7c4d3d6c6d3eb29873b7597951 Mon Sep 17 00:00:00 2001 From: Martin Kosicky Date: Fri, 5 Aug 2022 17:43:11 +0200 Subject: [PATCH 08/14] Changed BOM and removed accidental test --- pom.xml | 2 +- src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index bc1227e40b..fb69450369 100644 --- a/pom.xml +++ b/pom.xml @@ -75,7 +75,7 @@ io.jenkins.tools.bom - bom-2.303.x + bom-2.319.x 1500.ve4d05cd32975 pom import diff --git a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java index 96af3c6385..5afde40d27 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java @@ -461,11 +461,6 @@ public void calculate_head_name_with_env() throws Exception { new EnvVars("BRANCH", "dummy")); assertEquals("master", result6.headName); assertEquals(Constants.R_HEADS, result6.prefix); - - GitSCMFileSystem.BuilderImpl.HeadNameResult result7 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("*/master"), null, - new EnvVars("BRANCH", "dummy")); - assertEquals("master", result6.headName); - assertEquals(Constants.R_HEADS, result6.prefix); } /** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */ From 0fad6baa09282299777582f03b206508febe2192 Mon Sep 17 00:00:00 2001 From: MartinKosicky Date: Tue, 9 Aug 2022 19:57:43 +0200 Subject: [PATCH 09/14] Apply suggestions from code review. POM dependency of scm-api required for this feature Co-authored-by: Jesse Glick --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 2f7f4c59d4..5e06010f6e 100644 --- a/pom.xml +++ b/pom.xml @@ -102,7 +102,7 @@ org.jenkins-ci.plugins - 625.va_c59c48ce36f + 621.vda_a_b_055e58f7 scm-api @@ -192,7 +192,7 @@ org.jenkins-ci.plugins scm-api - 625.va_c59c48ce36f + 621.vda_a_b_055e58f7 tests test From 33d96e01778694f6794f2df309275171c8a7e0ce Mon Sep 17 00:00:00 2001 From: Martin Kosicky Date: Thu, 11 Aug 2022 12:30:29 +0200 Subject: [PATCH 10/14] Bumped jenkins version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 5e06010f6e..e503f431ad 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ 4.12.0 -SNAPSHOT jenkinsci/${project.artifactId}-plugin - 2.332.4 + 2.346.1 false true false From c61fbc41c2e0a51cb27336aa29b4b1af3d1de844 Mon Sep 17 00:00:00 2001 From: Martin Kosicky Date: Thu, 11 Aug 2022 14:08:57 +0200 Subject: [PATCH 11/14] Revert "Bumped jenkins version" This reverts commit 33d96e01778694f6794f2df309275171c8a7e0ce. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e503f431ad..5e06010f6e 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ 4.12.0 -SNAPSHOT jenkinsci/${project.artifactId}-plugin - 2.346.1 + 2.332.4 false true false From 99eb6a22f20374f0b89e0e933cc746e1621b152b Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Mon, 14 Nov 2022 05:49:22 -0700 Subject: [PATCH 12/14] Get scm-api version from bom --- pom.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pom.xml b/pom.xml index 413f58e37d..4a4c6c569e 100644 --- a/pom.xml +++ b/pom.xml @@ -101,7 +101,6 @@ org.jenkins-ci.plugins - 621.vda_a_b_055e58f7 scm-api @@ -184,7 +183,6 @@ org.jenkins-ci.plugins scm-api - 621.vda_a_b_055e58f7 tests test From 5e77775b9f90e7cc9ab057af0bebd467259eea37 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Mon, 14 Nov 2022 05:56:00 -0700 Subject: [PATCH 13/14] Intentionally vary expected value in test The branch name "master" is a default in many places. Using a non-default value in the tests (slightly) reduces the chances of a default being returned instead of the expected expansion of the variable. --- .../plugins/git/GitSCMFileSystemTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java index cb91afa6b9..59b2bf2875 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java @@ -432,18 +432,18 @@ public void filesystem_supports_descriptor() throws Exception { @Test public void calculate_head_name_with_env() throws Exception { GitSCMFileSystem.BuilderImpl.HeadNameResult result1 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, - new EnvVars("BRANCH", "master")); - assertEquals("master", result1.headName); + new EnvVars("BRANCH", "master-a")); + assertEquals("master-a", result1.headName); assertEquals(Constants.R_HEADS, result1.prefix); GitSCMFileSystem.BuilderImpl.HeadNameResult result2 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, - new EnvVars("BRANCH", "refs/heads/master")); - assertEquals("master", result2.headName); + new EnvVars("BRANCH", "refs/heads/master-b")); + assertEquals("master-b", result2.headName); assertEquals(Constants.R_HEADS, result2.prefix); GitSCMFileSystem.BuilderImpl.HeadNameResult result3 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("refs/heads/${BRANCH}"), null, - new EnvVars("BRANCH", "master")); - assertEquals("master", result3.headName); + new EnvVars("BRANCH", "master-c")); + assertEquals("master-c", result3.headName); assertEquals(Constants.R_HEADS, result3.prefix); GitSCMFileSystem.BuilderImpl.HeadNameResult result4 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, @@ -452,13 +452,13 @@ public void calculate_head_name_with_env() throws Exception { assertEquals(Constants.R_HEADS, result4.prefix); GitSCMFileSystem.BuilderImpl.HeadNameResult result5 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("*/${BRANCH}"), null, - new EnvVars("BRANCH", "master")); - assertEquals("master", result5.headName); + new EnvVars("BRANCH", "master-d")); + assertEquals("master-d", result5.headName); assertEquals(Constants.R_HEADS, result5.prefix); - GitSCMFileSystem.BuilderImpl.HeadNameResult result6 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("*/master"), null, + GitSCMFileSystem.BuilderImpl.HeadNameResult result6 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("*/master-e"), null, new EnvVars("BRANCH", "dummy")); - assertEquals("master", result6.headName); + assertEquals("master-e", result6.headName); assertEquals(Constants.R_HEADS, result6.prefix); } From 54af228609a88a3316bb74cdb5e1833a79bfadc2 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Tue, 22 Nov 2022 17:22:33 -0700 Subject: [PATCH 14/14] Retain API compatibilty by including previous signature --- src/main/java/jenkins/plugins/git/GitSCMFileSystem.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 1d47cf7a0b..f9535e372b 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -323,6 +323,12 @@ static HeadNameResult calculate(@NonNull BranchSpec branchSpec, } } + @Override + public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException { + return build(owner, scm, rev, null); + } + @Override public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev, @CheckForNull Run _build)