Skip to content

Commit

Permalink
[FIX JENKINS-17751] Called from a promotion, set UpstreamCause from t…
Browse files Browse the repository at this point in the history
…he original build of the promotion.
  • Loading branch information
ikedam committed Jul 20, 2013
1 parent a302204 commit e13ee74
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Expand Up @@ -69,7 +69,7 @@ protected Future schedule(AbstractBuild<?, ?> build, AbstractProject project, Li

// if we fail to add the item to the queue, wait and retry.
// it also means we have to force quiet period = 0, or else it'll never leave the queue
Future f = project.scheduleBuild2(0, new UpstreamCause((Run) build), list.toArray(new Action[list.size()]));
Future f = super.schedule(build, project, 0, list);
//when a project is disabled or the configuration is not yet saved f will always be null and we'ure caught in a loop, therefore we need to check for it
if (f!=null || (f==null && !project.isBuildable())){
return f;
Expand Down
Expand Up @@ -13,6 +13,7 @@
import hudson.model.Action;
import hudson.model.AutoCompletionCandidates;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.Cause.UpstreamCause;
import hudson.model.Describable;
import hudson.model.Descriptor;
Expand All @@ -28,6 +29,7 @@
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.parameterizedtrigger.AbstractBuildParameters.DontTriggerException;
import hudson.plugins.promoted_builds.Promotion;
import hudson.tasks.Messages;
import hudson.util.FormValidation;

Expand Down Expand Up @@ -285,12 +287,41 @@ private List<List<AbstractBuildParameters>> getDynamicBuildParameters(AbstractBu
}
}

protected Future schedule(AbstractBuild<?, ?> build, AbstractProject project, List<Action> list) throws InterruptedException, IOException {
return project.scheduleBuild2(project.getQuietPeriod(),
new UpstreamCause((Run) build),
/**
* Create UpstreamCause that triggers a downstream build.
*
* If the upstream build is a promotion, return the UpstreamCause
* as triggered by the target of the promotion.
*
* @param build an upstream build
* @return UpstreamCause
*/
protected Cause createUpstreamCause(AbstractBuild<?, ?> build) {
if(Jenkins.getInstance().getPlugin("promoted-builds") != null) {
// Test only when promoted-builds is installed.
if(build instanceof Promotion) {
Promotion promotion = (Promotion)build;

// This cannot be done for PromotionCause#PromotionCause is in a package scope.
// return new PromotionCause(build, promotion.getTarget());

return new UpstreamCause((Run<?,?>)promotion.getTarget());
}
}
return new UpstreamCause((Run) build);
}

protected Future schedule(AbstractBuild<?, ?> build, AbstractProject project, int quietPeriod, List<Action> list) throws InterruptedException, IOException {
Cause cause = createUpstreamCause(build);
return project.scheduleBuild2(quietPeriod,
cause,
list.toArray(new Action[list.size()]));
}

protected Future schedule(AbstractBuild<?, ?> build, AbstractProject project, List<Action> list) throws InterruptedException, IOException {
return schedule(build, project, project.getQuietPeriod(), list);
}

public boolean onJobRenamed(String oldName, String newName) {
boolean changed = false;
String[] list = projects.split(",");
Expand Down
Expand Up @@ -275,6 +275,7 @@ public void testBlockingTriggerWithMatrixProject() throws Exception {

@Bug(17751)
public void testTriggerFromPromotion() throws Exception {
assertNotNull("promoted-builds must be installed.", Jenkins.getInstance().getPlugin("promoted-builds"));
// Test combination with PromotedBuilds.
// Assert that the original build can be tracked from triggered build.
// The configuration is as following:
Expand Down

0 comments on commit e13ee74

Please sign in to comment.