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

Simplifying code using default methods on ParameterizedJob #45

Merged
merged 4 commits into from
May 17, 2017
Merged
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</pluginRepository>
</pluginRepositories>
<properties>
<jenkins.version>2.60</jenkins.version>
<jenkins.version>2.61</jenkins.version>
<java.level>8</java.level>
<no-test-jar>false</no-test-jar>
<workflow-support-plugin.version>2.14</workflow-support-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
import hudson.init.Initializer;
import hudson.model.Action;
import hudson.model.BallColor;
import hudson.model.BuildAuthorizationToken;
import hudson.model.BuildableItem;
import hudson.model.Cause;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.DescriptorVisibilityFilter;
Expand Down Expand Up @@ -87,12 +84,10 @@
import javax.annotation.CheckForNull;
import javax.servlet.ServletException;
import jenkins.model.BlockedBecauseOfBuildInProgress;

import jenkins.model.Jenkins;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.model.lazy.LazyBuildMixIn;
import jenkins.triggers.SCMTriggerItem;
import jenkins.util.TimeDuration;
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
Expand All @@ -104,14 +99,13 @@
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.interceptor.RequirePOST;

@SuppressWarnings({"unchecked", "rawtypes"})
public final class WorkflowJob extends Job<WorkflowJob,WorkflowRun> implements BuildableItem, LazyBuildMixIn.LazyLoadingJob<WorkflowJob,WorkflowRun>, ParameterizedJobMixIn.ParameterizedJob, TopLevelItem, Queue.FlyweightTask, SCMTriggerItem {
public final class WorkflowJob extends Job<WorkflowJob,WorkflowRun> implements LazyBuildMixIn.LazyLoadingJob<WorkflowJob,WorkflowRun>, ParameterizedJobMixIn.ParameterizedJob<WorkflowJob, WorkflowRun>, TopLevelItem, Queue.FlyweightTask, SCMTriggerItem {

private static final Logger LOGGER = Logger.getLogger(WorkflowJob.class.getName());

Expand Down Expand Up @@ -170,14 +164,6 @@ private LazyBuildMixIn<WorkflowJob,WorkflowRun> createBuildMixIn() {
};
}

private ParameterizedJobMixIn<WorkflowJob,WorkflowRun> createParameterizedJobMixIn() {
return new ParameterizedJobMixIn<WorkflowJob,WorkflowRun>() {
@Override protected WorkflowJob asJob() {
return WorkflowJob.this;
}
};
}

public FlowDefinition getDefinition() {
return definition;
}
Expand Down Expand Up @@ -268,53 +254,19 @@ public void setDefinition(FlowDefinition definition) {
}

// TODO https://github.com/jenkinsci/jenkins/pull/2866 remove override
@Override public Queue.Executable createExecutable() throws IOException {
@Override public WorkflowRun createExecutable() throws IOException {
if (isDisabled()) {
return null;
}
return buildMixIn.newBuild();
}

@Deprecated
@Override public boolean scheduleBuild() {
return createParameterizedJobMixIn().scheduleBuild();
}

@Override public boolean scheduleBuild(Cause c) {
return createParameterizedJobMixIn().scheduleBuild(c);
}

@Deprecated
@Override public boolean scheduleBuild(int quietPeriod) {
return createParameterizedJobMixIn().scheduleBuild(quietPeriod);
}

@Override public boolean scheduleBuild(int quietPeriod, Cause c) {
return createParameterizedJobMixIn().scheduleBuild(quietPeriod, c);
}

@Override public @CheckForNull QueueTaskFuture<WorkflowRun> scheduleBuild2(int quietPeriod, Action... actions) {
return createParameterizedJobMixIn().scheduleBuild2(quietPeriod, actions);
}

public void doBuild(StaplerRequest req, StaplerResponse rsp, @QueryParameter TimeDuration delay) throws IOException, ServletException {
createParameterizedJobMixIn().doBuild(req, rsp, delay);
}

public void doBuildWithParameters(StaplerRequest req, StaplerResponse rsp, @QueryParameter TimeDuration delay) throws IOException, ServletException {
createParameterizedJobMixIn().doBuildWithParameters(req, rsp, delay);
}

@RequirePOST public void doCancelQueue(StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
createParameterizedJobMixIn().doCancelQueue(req, rsp);
return ParameterizedJobMixIn.ParameterizedJob.super.scheduleBuild2(quietPeriod, actions);
}

@Override protected SearchIndexBuilder makeSearchIndex() {
return createParameterizedJobMixIn().extendSearchIndex(super.makeSearchIndex());
}

public boolean isParameterized() {
return createParameterizedJobMixIn().isParameterized();
return getParameterizedJobMixIn().extendSearchIndex(super.makeSearchIndex());
}

// TODO https://github.com/jenkinsci/jenkins/pull/2866 @Override
Expand Down Expand Up @@ -392,10 +344,6 @@ public void setQuietPeriod(Integer seconds) throws IOException {
save();
}

@Override public String getBuildNowText() {
return createParameterizedJobMixIn().getBuildNowText();
}

@Override public boolean isBuildBlocked() {
return getCauseOfBlockage() != null;
}
Expand Down Expand Up @@ -659,7 +607,7 @@ public boolean schedulePolling() {
// TODO https://github.com/jenkinsci/jenkins/pull/2866 remove override
@SuppressWarnings("deprecation")
public void doPolling(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
BuildAuthorizationToken.checkPermission((Job) this, getAuthToken(), req, rsp);
hudson.model.BuildAuthorizationToken.checkPermission((Job) this, getAuthToken(), req, rsp);
schedulePolling();
rsp.sendRedirect(".");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,24 @@ public void failedToStartRun() throws Exception {

@Test public void interruptWithResult() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("semaphore 'hang'"));
p.setDefinition(new CpsFlowDefinition("sleep 1; semaphore 'hang'", true));
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
Thread.sleep(500); // TODO sleeps should not be necessary but seems to randomly fail to receive interrupt otherwise
SemaphoreStep.waitForStart("hang/1", b1);
Thread.sleep(500);
Executor ex = b1.getExecutor();
assertNotNull(ex);
ex.interrupt(Result.NOT_BUILT, new CauseOfInterruption.UserInterruption("bob"));
r.assertBuildStatus(Result.NOT_BUILT, r.waitForCompletion(b1));
InterruptedBuildAction iba = b1.getAction(InterruptedBuildAction.class);
assertNotNull(iba);
assertEquals(Collections.singletonList(new CauseOfInterruption.UserInterruption("bob")), iba.getCauses());
Thread.sleep(500);
WorkflowRun b2 = p.scheduleBuild2(0).waitForStart();
assertEquals(2, b2.getNumber());
Thread.sleep(500);
SemaphoreStep.waitForStart("hang/2", b2);
Thread.sleep(500);
ex = b2.getExecutor();
assertNotNull(ex);
ex.interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public QueryingMockTrigger() {}

@Override public void start(BuildableItem project, boolean newInstance) {
super.start(project, newInstance);
for (Trigger t : ((ParameterizedJobMixIn.ParameterizedJob)project).getTriggers().values()) {
for (Trigger<?> t : ((ParameterizedJobMixIn.ParameterizedJob<?, ?>) project).getTriggers().values()) {
if (t instanceof QueryingMockTrigger) {
foundSelf = true;
}
Expand Down