From 5bd9173c0774af2cc92f57f4a756291c32bff52c Mon Sep 17 00:00:00 2001 From: Thierry Date: Mon, 14 Oct 2019 11:59:09 +0200 Subject: [PATCH 1/4] JENKINS-48431 Support both lightweight checkout AND build parameters --- pom.xml | 2 + .../jenkins/plugins/git/GitSCMFileSystem.java | 86 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/pom.xml b/pom.xml index c479f0724c..96f8822929 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,7 @@ true 1C false + 2.6.4-SNAPSHOT @@ -98,6 +99,7 @@ org.jenkins-ci.plugins scm-api + ${scm-api-plugin.version} org.jenkins-ci.plugins.workflow diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index b436d6646a..c3e720d874 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -34,6 +34,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import hudson.EnvVars; import hudson.Extension; +import hudson.model.Run; import hudson.model.Item; import hudson.model.TaskListener; import hudson.plugins.git.BranchSpec; @@ -286,6 +287,91 @@ public boolean supportsDescriptor(SCMSourceDescriptor descriptor) { return AbstractGitSCMSource.class.isAssignableFrom(descriptor.clazz); } + @Override + public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException { + if (rev != null && !(rev instanceof AbstractGitSCMSource.SCMRevisionImpl)) { + return null; + } + Item owner = build.getParent(); + TaskListener listener = new LogTaskListener(LOGGER, Level.FINE); + GitSCM gitSCM = (GitSCM) scm; + UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); + BranchSpec branchSpec = gitSCM.getBranches().get(0); + EnvVars environment = build.getEnvironment(listener); + String remote = environment.expand(config.getUrl()); + String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote); + Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry); + cacheLock.lock(); + try { + File cacheDir = AbstractGitSCMSource.getCacheDir(cacheEntry); + Git git = Git.with(listener, environment).in(cacheDir); + GitTool tool = gitSCM.resolveGitTool(listener); + if (tool != null) { + git.using(tool.getGitExe()); + } + GitClient client = git.getClient(); + String credentialsId = config.getCredentialsId(); + if (credentialsId != null) { + StandardCredentials credential = CredentialsMatchers.firstOrNull( + CredentialsProvider.lookupCredentials( + StandardUsernameCredentials.class, + owner, + ACL.SYSTEM, + URIRequirementBuilder.fromUri(remote).build() + ), + CredentialsMatchers.allOf( + CredentialsMatchers.withId(credentialsId), + GitClient.CREDENTIALS_MATCHER + ) + ); + client.addDefaultCredentials(credential); + CredentialsProvider.track(owner, credential); + } + + if (!client.hasGitRepo()) { + listener.getLogger().println("Creating git repository in " + cacheDir); + client.init(); + } + String remoteName = StringUtils.defaultIfBlank(environment.expand(config.getName()), Constants.DEFAULT_REMOTE_NAME); + listener.getLogger().println("Setting " + remoteName + " to " + remote); + client.setRemoteUrl(remoteName, remote); + listener.getLogger().println("Fetching & pruning " + remoteName + "..."); + URIish remoteURI = null; + try { + remoteURI = new URIish(remoteName); + } catch (URISyntaxException ex) { + listener.getLogger().println("URI syntax exception for '" + remoteName + "' " + ex); + } + String headName; + if (rev != null) { + headName = environment.expand(rev.getHead().getName()); + } else { + String branch = environment.expand(branchSpec.getName()); + if (branch.startsWith(Constants.R_HEADS)) { + headName = branch.substring(Constants.R_HEADS.length()); + } else if (branch.startsWith("*/")) { + headName = branch.substring(2); + } else { + headName = branch; + } + } + + String refspec = environment.expand(config.getRefspec()); + String head = headName; + if (refspec == null) { + refspec = "+" + Constants.R_HEADS + headName + ":" + Constants.R_REMOTES + remoteName + "/" + headName; + head = Constants.R_REMOTES + remoteName + "/" +headName; + } + client.fetch_().prune().from(remoteURI, Arrays + .asList(new RefSpec (refspec))).execute(); + listener.getLogger().println("Done."); + return new GitSCMFileSystem(client, remote, head, (AbstractGitSCMSource.SCMRevisionImpl) rev); + } finally { + cacheLock.unlock(); + } + } + @Override public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) throws IOException, InterruptedException { From bf791e285e8f069894d923caab9651ae11ac8d24 Mon Sep 17 00:00:00 2001 From: Thierry Date: Mon, 14 Oct 2019 19:22:19 +0200 Subject: [PATCH 2/4] Bugfix for tests --- src/main/java/jenkins/plugins/git/GitSCMTelescope.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/jenkins/plugins/git/GitSCMTelescope.java b/src/main/java/jenkins/plugins/git/GitSCMTelescope.java index 886e5cae95..8537e3d6be 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMTelescope.java +++ b/src/main/java/jenkins/plugins/git/GitSCMTelescope.java @@ -35,6 +35,7 @@ import hudson.ExtensionList; import hudson.model.Item; import hudson.model.Queue; +import hudson.model.Run; import hudson.model.queue.Tasks; import hudson.plugins.git.BranchSpec; import hudson.plugins.git.GitSCM; @@ -230,6 +231,12 @@ public final SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, SCMRevis return null; } + @Override + public final SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException { + return null; + } + /** * Given a {@link SCM} this should try to build a corresponding {@link SCMFileSystem} instance that * reflects the content at the specified {@link SCMRevision}. If the {@link SCM} is supported but not From 96f458d6a67c5be6febf1f102077094bb392c01a Mon Sep 17 00:00:00 2001 From: Thierry Gatineau Date: Sat, 19 Oct 2019 20:32:34 +0200 Subject: [PATCH 3/4] Trim whitespaces --- .../java/jenkins/plugins/git/GitSCMFileSystem.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index c3e720d874..0a61a2490f 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -299,7 +299,7 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); BranchSpec branchSpec = gitSCM.getBranches().get(0); EnvVars environment = build.getEnvironment(listener); - String remote = environment.expand(config.getUrl()); + String remote = environment.expand(config.getUrl()).trim(); String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote); Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry); cacheLock.lock(); @@ -333,7 +333,7 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S listener.getLogger().println("Creating git repository in " + cacheDir); client.init(); } - String remoteName = StringUtils.defaultIfBlank(environment.expand(config.getName()), Constants.DEFAULT_REMOTE_NAME); + String remoteName = StringUtils.defaultIfBlank(environment.expand(config.getName()).trim(), Constants.DEFAULT_REMOTE_NAME); listener.getLogger().println("Setting " + remoteName + " to " + remote); client.setRemoteUrl(remoteName, remote); listener.getLogger().println("Fetching & pruning " + remoteName + "..."); @@ -345,9 +345,9 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S } String headName; if (rev != null) { - headName = environment.expand(rev.getHead().getName()); + headName = environment.expand(rev.getHead().getName()).trim(); } else { - String branch = environment.expand(branchSpec.getName()); + String branch = environment.expand(branchSpec.getName()).trim(); if (branch.startsWith(Constants.R_HEADS)) { headName = branch.substring(Constants.R_HEADS.length()); } else if (branch.startsWith("*/")) { @@ -357,7 +357,7 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S } } - String refspec = environment.expand(config.getRefspec()); + String refspec = environment.expand(config.getRefspec()).trim(); String head = headName; if (refspec == null) { refspec = "+" + Constants.R_HEADS + headName + ":" + Constants.R_REMOTES + remoteName + "/" + headName; From cb533202ba71bc5dabe4d6fb04fccf10f48cecf6 Mon Sep 17 00:00:00 2001 From: Thierry Date: Thu, 28 Nov 2019 11:51:54 +0100 Subject: [PATCH 4/4] Bugfix spotbugs --- src/main/java/jenkins/plugins/git/GitSCMFileSystem.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 0a61a2490f..a54805ae89 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -357,12 +357,15 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S } } - String refspec = environment.expand(config.getRefspec()).trim(); + String refspec = environment.expand(config.getRefspec()); String head = headName; if (refspec == null) { refspec = "+" + Constants.R_HEADS + headName + ":" + Constants.R_REMOTES + remoteName + "/" + headName; head = Constants.R_REMOTES + remoteName + "/" +headName; } + else { + refspec = refspec.trim(); + } client.fetch_().prune().from(remoteURI, Arrays .asList(new RefSpec (refspec))).execute(); listener.getLogger().println("Done.");