From acbaed50f624409d04f810ca466b426ef58d68ff Mon Sep 17 00:00:00 2001 From: Thierry Date: Mon, 14 Oct 2019 11:56:11 +0200 Subject: [PATCH 1/3] JENKINS-48431 Support both lightweight checkout AND build parameters --- .../java/jenkins/scm/api/SCMFileSystem.java | 88 +++++++++++++++++++ .../jenkins/scm/impl/SCMFileSystemTest.java | 15 ++++ 2 files changed, 103 insertions(+) diff --git a/src/main/java/jenkins/scm/api/SCMFileSystem.java b/src/main/java/jenkins/scm/api/SCMFileSystem.java index d5a34327..899ecc7c 100644 --- a/src/main/java/jenkins/scm/api/SCMFileSystem.java +++ b/src/main/java/jenkins/scm/api/SCMFileSystem.java @@ -158,6 +158,75 @@ public boolean changesSince(@CheckForNull SCMRevision revision, @NonNull OutputS throw new UnsupportedOperationException(); } + /** + * Given a {@link SCM} this method will try to retrieve a corresponding {@link SCMFileSystem} instance. + * + * @param build the build of the {@link SCM} + * @param scm the {@link SCM}. + * @return the corresponding {@link SCMFileSystem} or {@code null} if there is none. + * @throws IOException if the attempt to create a {@link SCMFileSystem} failed due to an IO error + * (such as the remote system being unavailable) + * @throws InterruptedException if the attempt to create a {@link SCMFileSystem} was interrupted. + */ + @CheckForNull + public static SCMFileSystem of(@NonNull Run build, @NonNull SCM scm) throws IOException, InterruptedException { + return of(build, scm, null); + } + + /** + * Given a {@link SCM} this method will try to retrieve a corresponding {@link SCMFileSystem} instance that + * reflects the content at the specified {@link SCMRevision}. + * + * @param build the build of the {@link SCM} + * @param scm the {@link SCM}. + * @param rev the specified {@link SCMRevision}. + * @return the corresponding {@link SCMFileSystem} or {@code null} if there is none. + * @throws IOException if the attempt to create a {@link SCMFileSystem} failed due to an IO error + * (such as the remote system being unavailable) + * @throws InterruptedException if the attempt to create a {@link SCMFileSystem} was interrupted. + */ + @CheckForNull + public static SCMFileSystem of(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException { + Objects.requireNonNull(scm); + SCMFileSystem fallBack = null; + Throwable failure = null; + for (Builder b : ExtensionList.lookup(Builder.class)) { + if (b.supports(scm)) { + try { + SCMFileSystem inspector = b.build(build, scm, rev); + if (inspector != null) { + if (inspector.isFixedRevision()) { + return inspector; + } + if (fallBack == null) { + fallBack = inspector; + } + } + } catch (IOException | InterruptedException | RuntimeException e) { + if (failure == null) { + failure = e; + } else { + failure.addSuppressed(e); + } + } + } + } + if (fallBack == null) { + if (failure instanceof IOException) { + throw (IOException) failure; + } + if (failure instanceof InterruptedException) { + throw (InterruptedException) failure; + } + //noinspection ConstantConditions + if (failure instanceof RuntimeException) { + throw (RuntimeException) failure; + } + } + return fallBack; + } + /** * Given a {@link SCM} this method will try to retrieve a corresponding {@link SCMFileSystem} instance. * @@ -483,6 +552,25 @@ public final boolean supports(SCMSourceDescriptor descriptor) { */ protected abstract boolean supportsDescriptor(SCMSourceDescriptor descriptor); + /** + * 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 + * for a fixed revision, best effort is acceptable as the most capable {@link SCMFileSystem} will be returned + * to the caller. + * + * @param build the build of the {@link SCM} + * @param scm the {@link SCM}. + * @param rev the specified {@link SCMRevision}. + * @return the corresponding {@link SCMFileSystem} or {@code null} if this builder cannot create a {@link + * SCMFileSystem} for the specified {@link SCM}. + * @throws IOException if the attempt to create a {@link SCMFileSystem} failed due to an IO error + * (such as the remote system being unavailable) + * @throws InterruptedException if the attempt to create a {@link SCMFileSystem} was interrupted. + */ + @CheckForNull + public abstract SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException; + /** * 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 diff --git a/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java b/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java index 178cd5a6..f1cad54a 100644 --- a/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java +++ b/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java @@ -27,6 +27,7 @@ import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.model.Item; +import hudson.model.Run; import hudson.scm.SCM; import hudson.scm.SCMDescriptor; import jenkins.scm.api.SCMFileSystem; @@ -117,6 +118,13 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull return null; } + @Override + @CheckForNull + public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException { + return null; + } + } @TestExtension("filesystem_supports_false_implementation_for_descriptor") @@ -148,5 +156,12 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull return null; } + @Override + @CheckForNull + public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException { + return null; + } + } } From b73b3fca985c01de52a69837c875ddf176315594 Mon Sep 17 00:00:00 2001 From: Thierry Gatineau Date: Wed, 27 May 2020 15:56:56 +0200 Subject: [PATCH 2/3] Take into account @jetersen remarks --- src/main/java/jenkins/scm/api/SCMFileSystem.java | 10 ++++++---- src/test/java/jenkins/scm/impl/SCMFileSystemTest.java | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/jenkins/scm/api/SCMFileSystem.java b/src/main/java/jenkins/scm/api/SCMFileSystem.java index 899ecc7c..3142c73e 100644 --- a/src/main/java/jenkins/scm/api/SCMFileSystem.java +++ b/src/main/java/jenkins/scm/api/SCMFileSystem.java @@ -169,7 +169,7 @@ public boolean changesSince(@CheckForNull SCMRevision revision, @NonNull OutputS * @throws InterruptedException if the attempt to create a {@link SCMFileSystem} was interrupted. */ @CheckForNull - public static SCMFileSystem of(@NonNull Run build, @NonNull SCM scm) throws IOException, InterruptedException { + public static SCMFileSystem of(@NonNull Run build, @NonNull SCM scm) throws IOException, InterruptedException { return of(build, scm, null); } @@ -186,7 +186,7 @@ public static SCMFileSystem of(@NonNull Run build, @NonNull SCM scm) throws IOEx * @throws InterruptedException if the attempt to create a {@link SCMFileSystem} was interrupted. */ @CheckForNull - public static SCMFileSystem of(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + public static SCMFileSystem of(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) throws IOException, InterruptedException { Objects.requireNonNull(scm); SCMFileSystem fallBack = null; @@ -568,8 +568,10 @@ public final boolean supports(SCMSourceDescriptor descriptor) { * @throws InterruptedException if the attempt to create a {@link SCMFileSystem} was interrupted. */ @CheckForNull - public abstract SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) - throws IOException, InterruptedException; + public abstract SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException { + return build(build.getParent(), scm, rev); + } /** * Given a {@link SCM} this should try to build a corresponding {@link SCMFileSystem} instance that diff --git a/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java b/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java index f1cad54a..ea130295 100644 --- a/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java +++ b/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java @@ -120,7 +120,7 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull @Override @CheckForNull - public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) throws IOException, InterruptedException { return null; } @@ -158,7 +158,7 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull @Override @CheckForNull - public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) throws IOException, InterruptedException { return null; } From 3b8bfb770720c07d664bd9a9b02233132a4001ef Mon Sep 17 00:00:00 2001 From: Thierry Gatineau Date: Wed, 27 May 2020 16:06:00 +0200 Subject: [PATCH 3/3] Bug fix abstract --- src/main/java/jenkins/scm/api/SCMFileSystem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/jenkins/scm/api/SCMFileSystem.java b/src/main/java/jenkins/scm/api/SCMFileSystem.java index 3142c73e..45d1df22 100644 --- a/src/main/java/jenkins/scm/api/SCMFileSystem.java +++ b/src/main/java/jenkins/scm/api/SCMFileSystem.java @@ -568,7 +568,7 @@ public final boolean supports(SCMSourceDescriptor descriptor) { * @throws InterruptedException if the attempt to create a {@link SCMFileSystem} was interrupted. */ @CheckForNull - public abstract SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) throws IOException, InterruptedException { return build(build.getParent(), scm, rev); }