From 80f69c86643b1068b70aada3d82b0224eaa3a9bd Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Fri, 19 Jul 2019 14:10:43 -0700 Subject: [PATCH 1/5] Backport of PR-708 --- pom.xml | 2 +- .../plugins/git/AbstractGitSCMSource.java | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 83e419d344..c954984f7a 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ false true 1C - 2.2.6 + 2.6.3 1.23 3.1.0 8.22 diff --git a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java index e343c988f4..c4e526e199 100644 --- a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java +++ b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java @@ -786,14 +786,14 @@ public void record(@NonNull SCMHead head, SCMRevision revision, boolean isMatch) */ @CheckForNull @Override - protected SCMRevision retrieve(@NonNull final String revision, @NonNull final TaskListener listener) throws IOException, InterruptedException { + protected SCMRevision retrieve(@NonNull final String revision, @NonNull final TaskListener listener, @CheckForNull Item retrieveContext) throws IOException, InterruptedException { final GitSCMSourceContext context = new GitSCMSourceContext<>(null, SCMHeadObserver.none()).withTraits(getTraits()); final GitSCMTelescope telescope = GitSCMTelescope.of(this); if (telescope != null) { final String remote = getRemote(); - final StandardUsernameCredentials credentials = getCredentials(); + final StandardUsernameCredentials credentials = getCredentials(retrieveContext); telescope.validate(remote, credentials); SCMRevision result = telescope.getRevision(remote, credentials, revision); if (result != null) { @@ -825,7 +825,7 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta git.using(tool.getGitExe()); } final GitClient client = git.getClient(); - client.addDefaultCredentials(getCredentials()); + client.addDefaultCredentials(getCredentials(retrieveContext)); listener.getLogger().printf("Attempting to resolve %s from remote references...%n", revision); boolean headsOnly = !context.wantOtherRefs() && context.wantBranches(); boolean tagsOnly = !context.wantOtherRefs() && context.wantTags(); @@ -1016,14 +1016,14 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException, */ @NonNull @Override - protected Set retrieveRevisions(@NonNull final TaskListener listener) throws IOException, InterruptedException { + protected Set retrieveRevisions(@NonNull final TaskListener listener, @CheckForNull Item retrieveContext) throws IOException, InterruptedException { final GitSCMSourceContext context = new GitSCMSourceContext<>(null, SCMHeadObserver.none()).withTraits(getTraits()); final GitSCMTelescope telescope = GitSCMTelescope.of(this); if (telescope != null) { final String remote = getRemote(); - final StandardUsernameCredentials credentials = getCredentials(); + final StandardUsernameCredentials credentials = getCredentials(retrieveContext); telescope.validate(remote, credentials); Set referenceTypes = new HashSet<>(); if (context.wantBranches()) { @@ -1048,7 +1048,7 @@ protected Set retrieveRevisions(@NonNull final TaskListener listener) th git.using(tool.getGitExe()); } GitClient client = git.getClient(); - client.addDefaultCredentials(getCredentials()); + client.addDefaultCredentials(getCredentials(retrieveContext)); Set revisions = new HashSet<>(); if (context.wantBranches() || context.wantTags() || context.wantOtherRefs()) { listener.getLogger().println("Listing remote references..."); @@ -1237,13 +1237,18 @@ protected static Lock getCacheLock(String cacheEntry) { @CheckForNull protected StandardUsernameCredentials getCredentials() { + return getCredentials(getOwner()); + } + + @CheckForNull + private StandardUsernameCredentials getCredentials(@CheckForNull Item context) { String credentialsId = getCredentialsId(); if (credentialsId == null) { return null; } return CredentialsMatchers .firstOrNull( - CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, getOwner(), + CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, context, ACL.SYSTEM, URIRequirementBuilder.fromUri(getRemote()).build()), CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId), GitClient.CREDENTIALS_MATCHER)); From 30dc9766a6090c9a828214317cad9fb0d115bb79 Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Wed, 3 Oct 2018 12:07:03 -0400 Subject: [PATCH 2/5] [JENKINS-52964] Add SCMFileSystem.Builder supports for descriptor Downstream of https://github.com/jenkinsci/scm-api-plugin/pull/58 --- src/main/java/jenkins/plugins/git/GitSCMFileSystem.java | 6 ++++++ .../java/jenkins/plugins/git/GitSCMFileSystemTest.java | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 1e27a0f79d..4030e33d8c 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -61,6 +61,7 @@ import jenkins.scm.api.SCMHead; import jenkins.scm.api.SCMRevision; import jenkins.scm.api.SCMSource; +import jenkins.scm.api.SCMSourceDescriptor; import org.apache.commons.lang.StringUtils; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; @@ -279,6 +280,11 @@ public boolean supports(SCMSource source) { return source instanceof AbstractGitSCMSource; } + @Override + public boolean supports(SCMSourceDescriptor descriptor) { + return AbstractGitSCMSource.class.isAssignableFrom(descriptor.clazz); + } + @Override public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) throws IOException, InterruptedException { diff --git a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java index 1741553b2c..5c20d4b0a7 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java @@ -44,6 +44,7 @@ import jenkins.scm.api.SCMHead; import jenkins.scm.api.SCMRevision; import jenkins.scm.api.SCMSource; +import jenkins.scm.api.SCMSourceDescriptor; import org.eclipse.jgit.lib.ObjectId; import org.jenkinsci.plugins.gitclient.Git; import org.jenkinsci.plugins.gitclient.GitClient; @@ -347,6 +348,13 @@ public void given_filesystem_when_askingChangesSinceNewRevision_then_changesAreP assertThat(out.toString(), is("")); } + @Issue("JENKINS-52964") + @Test + public void filesystem_supports_descriptor() throws Exception { + SCMSourceDescriptor descriptor = r.jenkins.getDescriptorByType(GitSCMSource.DescriptorImpl.class); + assertTrue(SCMFileSystem.supports(descriptor)); + } + /** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */ private boolean isWindows() { return java.io.File.pathSeparatorChar==';'; From 9f56ade24963ea95642258fe1bdc827012a0fab3 Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Wed, 3 Oct 2018 13:25:13 -0400 Subject: [PATCH 3/5] Bump scm-api build, switch API around --- .../java/jenkins/plugins/git/GitSCMFileSystem.java | 9 ++++++++- .../java/jenkins/plugins/git/GitSCMSourceTest.java | 12 ++++++++++++ .../jenkins/plugins/git/GitSCMTelescopeTest.java | 11 +++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 4030e33d8c..27f5ae3de2 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -43,6 +43,7 @@ import hudson.plugins.git.UserRemoteConfig; import hudson.remoting.VirtualChannel; import hudson.scm.SCM; +import hudson.scm.SCMDescriptor; import hudson.security.ACL; import hudson.util.LogTaskListener; import java.io.File; @@ -281,7 +282,13 @@ public boolean supports(SCMSource source) { } @Override - public boolean supports(SCMSourceDescriptor descriptor) { + public boolean supportsDescriptor(SCMDescriptor descriptor) { + // Assume by default that we don't support GitSCM since we can't tell how it would be configured. + return false; + } + + @Override + public boolean supportsDescriptor(SCMSourceDescriptor descriptor) { return AbstractGitSCMSource.class.isAssignableFrom(descriptor.clazz); } diff --git a/src/test/java/jenkins/plugins/git/GitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/GitSCMSourceTest.java index e6edb78d43..0a488c4563 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMSourceTest.java @@ -8,6 +8,7 @@ import hudson.model.TopLevelItem; import hudson.plugins.git.GitStatus; import hudson.util.LogTaskListener; +import hudson.scm.SCMDescriptor; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.InputStream; @@ -31,6 +32,7 @@ import jenkins.scm.api.SCMRevision; import jenkins.scm.api.SCMSource; import jenkins.scm.api.SCMSourceCriteria; +import jenkins.scm.api.SCMSourceDescriptor; import jenkins.scm.api.SCMSourceOwner; import jenkins.scm.api.metadata.PrimaryInstanceMetadataAction; import jenkins.scm.api.trait.SCMSourceTrait; @@ -340,6 +342,16 @@ public boolean supports(@NonNull String remote) { return "http://git.test/telescope.git".equals(remote); } + @Override + public boolean supportsDescriptor(SCMDescriptor descriptor) { + return false; + } + + @Override + public boolean supportsDescriptor(SCMSourceDescriptor descriptor) { + return false; + } + @Override public void validate(@NonNull String remote, StandardCredentials credentials) throws IOException, InterruptedException { diff --git a/src/test/java/jenkins/plugins/git/GitSCMTelescopeTest.java b/src/test/java/jenkins/plugins/git/GitSCMTelescopeTest.java index f70e97030a..55f82c19b8 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMTelescopeTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMTelescopeTest.java @@ -38,6 +38,7 @@ import hudson.plugins.git.extensions.GitSCMExtension; import hudson.scm.NullSCM; import hudson.scm.SCM; +import hudson.scm.SCMDescriptor; import hudson.search.Search; import hudson.search.SearchIndex; import hudson.security.ACL; @@ -498,6 +499,16 @@ public boolean supports(String remote) { return allowedRemote.equals(remote); } + @Override + public boolean supportsDescriptor(SCMDescriptor descriptor) { + return false; + } + + @Override + public boolean supportsDescriptor(SCMSourceDescriptor descriptor) { + return false; + } + @Override public void validate(String remote, StandardCredentials credentials) throws IOException, InterruptedException { } From adadd397dd4e5e094629e94bed20caeaba871a40 Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Wed, 3 Oct 2018 16:01:34 -0400 Subject: [PATCH 4/5] switch supportsDescriptor(SCMDescriptor) to return true for GitSCM.DescriptorImpl --- src/main/java/jenkins/plugins/git/GitSCMFileSystem.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 27f5ae3de2..aae1097de0 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -283,8 +283,7 @@ public boolean supports(SCMSource source) { @Override public boolean supportsDescriptor(SCMDescriptor descriptor) { - // Assume by default that we don't support GitSCM since we can't tell how it would be configured. - return false; + return descriptor instanceof GitSCM.DescriptorImpl; } @Override From 0399643c72b404a46016e23d683c9d74716eeef3 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Mon, 22 Jul 2019 12:36:27 -0600 Subject: [PATCH 5/5] Bump plugin version number for dependency update Requiring scm-api 2.6.3 instead of scm-api 2.2.7 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c954984f7a..7e1c2118ad 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ 2007 - 3.10.2 + 3.11.0 -SNAPSHOT 2.121.1 8