From 1ca6b0413b672c846419b0865bab6ce5320d70eb Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 10 May 2023 14:23:08 +1000 Subject: [PATCH] BuildStrategies implementation need to know the current job Signed-off-by: Olivier Lamy --- .../jenkins/branch/BranchBuildStrategy.java | 32 +++++++++++++++++-- .../jenkins/branch/BranchProjectFactory.java | 8 ++--- .../jenkins/branch/MultiBranchProject.java | 7 ++-- src/test/java/integration/EventsTest.java | 15 +++++---- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/main/java/jenkins/branch/BranchBuildStrategy.java b/src/main/java/jenkins/branch/BranchBuildStrategy.java index f32ddd5d..a690642c 100644 --- a/src/main/java/jenkins/branch/BranchBuildStrategy.java +++ b/src/main/java/jenkins/branch/BranchBuildStrategy.java @@ -28,6 +28,7 @@ import hudson.ExtensionPoint; import hudson.Util; import hudson.model.AbstractDescribableImpl; +import hudson.model.Job; import hudson.model.TaskListener; import hudson.util.LogTaskListener; import java.util.logging.Level; @@ -161,12 +162,22 @@ public boolean isAutomaticBuild(@NonNull SCMSource source, * @since 2.4.2 */ @Restricted(ProtectedExternally.class) + public boolean isAutomaticBuild(@NonNull SCMSource source, + @NonNull SCMHead head, + @NonNull SCMRevision currRevision, + @CheckForNull SCMRevision lastBuiltRevision, + @CheckForNull SCMRevision lastSeenRevision, + @NonNull TaskListener listener) { + return isAutomaticBuild(source, head, currRevision, lastBuiltRevision, lastSeenRevision, listener, null); + } + public abstract boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision, @CheckForNull SCMRevision lastBuiltRevision, @CheckForNull SCMRevision lastSeenRevision, - @NonNull TaskListener listener); + @NonNull TaskListener listener, + @CheckForNull Job job); /** * API: Should the specified {@link SCMRevision} of the {@link SCMHead} for the specified {@link SCMSource} be @@ -219,6 +230,15 @@ public final boolean automaticBuild(@NonNull SCMSource source, return automaticBuild(source, head, currRevision, prevRevision, prevRevision, listener); } + public final boolean automaticBuild(@NonNull SCMSource source, + @NonNull SCMHead head, + @NonNull SCMRevision currRevision, + @CheckForNull SCMRevision lastBuiltRevision, + @CheckForNull SCMRevision lastSeenRevision, + @NonNull TaskListener listener) { + return automaticBuild(source, head, currRevision, lastBuiltRevision, lastSeenRevision, listener, null); + } + /** * API: Should the specified {@link SCMRevision} of the {@link SCMHead} for the specified {@link SCMSource} be * triggered when the {@link SCMHead} has been detected as created / modified? @@ -243,7 +263,13 @@ public final boolean automaticBuild(@NonNull SCMSource source, @NonNull SCMRevision currRevision, @CheckForNull SCMRevision lastBuiltRevision, @CheckForNull SCMRevision lastSeenRevision, - @NonNull TaskListener listener) { + @NonNull TaskListener listener, + @CheckForNull Job job) { + if (Util.isOverridden(BranchBuildStrategy.class, getClass(), "isAutomaticBuild", SCMSource.class, + SCMHead.class, SCMRevision.class, SCMRevision.class, SCMRevision.class, TaskListener.class, Job.class)) { + // modern implementation written to the some random number + return isAutomaticBuild(source, head, currRevision, lastBuiltRevision, lastSeenRevision, listener, job); + } if (Util.isOverridden(BranchBuildStrategy.class, getClass(), "isAutomaticBuild", SCMSource.class, SCMHead.class, SCMRevision.class, SCMRevision.class, SCMRevision.class, TaskListener.class)) { // modern implementation written to the 2.4.2+ spec @@ -271,7 +297,7 @@ public final boolean automaticBuild(@NonNull SCMSource source, } // this is going to throw an abstract method exception, but we should never get here as all implementations // have to at least have overridden one of the methods above. - return isAutomaticBuild(source, head, currRevision, lastBuiltRevision, lastSeenRevision, listener); + return isAutomaticBuild(source, head, currRevision, lastBuiltRevision, lastSeenRevision, listener, job); } /** diff --git a/src/main/java/jenkins/branch/BranchProjectFactory.java b/src/main/java/jenkins/branch/BranchProjectFactory.java index 6e49f31b..627ea583 100644 --- a/src/main/java/jenkins/branch/BranchProjectFactory.java +++ b/src/main/java/jenkins/branch/BranchProjectFactory.java @@ -158,7 +158,7 @@ public P asProject(@NonNull Item item) { * @return the {@link SCMRevision} of the last build. */ @CheckForNull - public SCMRevision getRevision(P project) { + public static SCMRevision getRevision(Job project) { XmlFile file = new XmlFile(new File(project.getRootDir(), "scm-revision-hash.xml")); try { return (SCMRevision) file.read(); @@ -175,7 +175,7 @@ public SCMRevision getRevision(P project) { * @param revision the {@link SCMRevision} of the last build. * @throws IOException if there was an issue persisting the details. */ - public void setRevisionHash(P project, SCMRevision revision) throws IOException { + public static void setRevisionHash(Job project, SCMRevision revision) throws IOException { XmlFile file = new XmlFile(new File(project.getRootDir(), "scm-revision-hash.xml")); file.write(revision); } @@ -187,7 +187,7 @@ public void setRevisionHash(P project, SCMRevision revision) throws IOException * @return the {@link SCMRevision} of the last seen. */ @CheckForNull - public SCMRevision getLastSeenRevision(P project) { + public static SCMRevision getLastSeenRevision(Job project) { XmlFile file = new XmlFile(new File(project.getRootDir(), "scm-last-seen-revision-hash.xml")); try { return (SCMRevision) file.read(); @@ -204,7 +204,7 @@ public SCMRevision getLastSeenRevision(P project) { * @param revision the {@link SCMRevision} of the last build. * @throws IOException if there was an issue persisting the details. */ - public void setLastSeenRevisionHash(P project, SCMRevision revision) throws IOException { + public static void setLastSeenRevisionHash(Job project, SCMRevision revision) throws IOException { XmlFile file = new XmlFile(new File(project.getRootDir(), "scm-last-seen-revision-hash.xml")); file.write(revision); } diff --git a/src/main/java/jenkins/branch/MultiBranchProject.java b/src/main/java/jenkins/branch/MultiBranchProject.java index 1b383b42..0eb2f791 100644 --- a/src/main/java/jenkins/branch/MultiBranchProject.java +++ b/src/main/java/jenkins/branch/MultiBranchProject.java @@ -2197,7 +2197,7 @@ private SCMRevision lastSeenRevisionOrDefault(@NonNull P project, SCMRevision sc } private void doAutomaticBuilds(@NonNull SCMHead head, @NonNull SCMRevision revision, @NonNull String rawName, @NonNull P project, Action[] revisionActions, SCMRevision scmLastBuiltRevision, SCMRevision scmLastSeenRevision) { - if (isAutomaticBuild(head, revision, scmLastBuiltRevision, scmLastSeenRevision)) { + if (isAutomaticBuild(head, revision, scmLastBuiltRevision, scmLastSeenRevision, project)) { scheduleBuild( _factory, project, @@ -2228,7 +2228,8 @@ private void doAutomaticBuilds(@NonNull SCMHead head, @NonNull SCMRevision revis private boolean isAutomaticBuild(@NonNull SCMHead head, @NonNull SCMRevision currRevision, @CheckForNull SCMRevision lastBuiltRevision, - @CheckForNull SCMRevision lastSeenRevision) { + @CheckForNull SCMRevision lastSeenRevision, + @NonNull P project) { BranchSource branchSource = null; for (BranchSource s: MultiBranchProject.this.sources) { if (s.getSource().getId().equals(source.getId())) { @@ -2246,7 +2247,7 @@ private boolean isAutomaticBuild(@NonNull SCMHead head, return !(head instanceof TagSCMHead); } else { for (BranchBuildStrategy s: buildStrategies) { - if (s.automaticBuild(source, head, currRevision, lastBuiltRevision, lastSeenRevision, listener)) { + if (s.automaticBuild(source, head, currRevision, lastBuiltRevision, lastSeenRevision, listener, project)) { return true; } } diff --git a/src/test/java/integration/EventsTest.java b/src/test/java/integration/EventsTest.java index 6c4fe1c2..5dacdb93 100644 --- a/src/test/java/integration/EventsTest.java +++ b/src/test/java/integration/EventsTest.java @@ -33,6 +33,7 @@ import hudson.model.Computer; import hudson.model.Executor; import hudson.model.FreeStyleProject; +import hudson.model.Job; import hudson.model.OneOffExecutor; import hudson.model.Queue; import hudson.model.Result; @@ -648,7 +649,7 @@ public static class BuildEverythingStrategyImpl extends BranchBuildStrategy { public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision, SCMRevision lastBuiltRevision, SCMRevision lastSeenRevision, - TaskListener listener) { + TaskListener listener, Job p) { return true; } @@ -699,7 +700,7 @@ public static class BuildChangeRequestsStrategyImpl extends BranchBuildStrategy public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision, SCMRevision lastBuiltRevision, SCMRevision lastSeenRevision, - @NonNull TaskListener listener) { + @NonNull TaskListener listener, Job p) { return head instanceof ChangeRequestSCMHead; } @@ -715,7 +716,7 @@ public static class BuildTrustedChangeRequestsStrategyImpl extends BranchBuildSt public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision, SCMRevision lastBuiltRevision, SCMRevision lastSeenRevision, - @NonNull TaskListener listener) { + @NonNull TaskListener listener, Job p) { if (head instanceof ChangeRequestSCMHead) { try { return currRevision.equals(source.getTrustedRevision(currRevision, listener)); @@ -1082,7 +1083,7 @@ public BuildRevisionStrategyImpl(String... approved) { public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision, SCMRevision lastBuiltRevision, SCMRevision lastSeenRevision, - @NonNull TaskListener listener) { + @NonNull TaskListener listener, Job p) { return currRevision instanceof MockSCMRevision && approved.contains(((MockSCMRevision) currRevision).getHash()); } @@ -1148,7 +1149,7 @@ public IgnoreTargetChangesStrategyImpl() { public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision, SCMRevision lastBuiltRevision, SCMRevision lastSeenRevision, - @NonNull TaskListener listener) { + @NonNull TaskListener listener, Job p) { if (currRevision instanceof ChangeRequestSCMRevision) { ChangeRequestSCMRevision currCR = (ChangeRequestSCMRevision) currRevision; if (lastBuiltRevision instanceof ChangeRequestSCMRevision) { @@ -2865,7 +2866,7 @@ public static class SkipInitialBuildStrategyImpl extends BranchBuildStrategy { public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision, SCMRevision lastBuiltRevision, SCMRevision lastSeenRevision, - TaskListener listener) { + TaskListener listener, Job p) { if (lastSeenRevision != null) { return true; } @@ -2911,7 +2912,7 @@ public static class SkipIAllBuildStrategyImpl extends BranchBuildStrategy { public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision, SCMRevision lastBuiltRevision, SCMRevision lastSeenRevision, - TaskListener listener) { + TaskListener listener, Job p) { return false; }