Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuildStrategies implementation need to know the current job #369

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/main/java/jenkins/branch/BranchBuildStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this pattern of keep adding parameters is getting worst and worst :(
Maybe we could have something such (pseudo code) to avoid breaking backward compat with every new data:

class AutomaticBuild {
  SCMHead head;
  SCMRevision currRevision;
  // all the getters/setters
}

or wait to be 17 here and use record :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use record now.

Is this PR still relevant? Is there some proposed caller?

@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
Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/jenkins/branch/BranchProjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/jenkins/branch/MultiBranchProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())) {
Expand All @@ -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;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/integration/EventsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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));
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down