Skip to content
Permalink
Browse files

Merge pull request #108 from JamesForsee/JENKINS-38670

[Fixed JENKINS-23532] Search Folders when trying to find upstream project
  • Loading branch information...
dalvizu committed Jun 19, 2017
2 parents 12e7766 + 7eab095 commit 6300e17fca0073c71f369a71bb7370b275e6f8dd
@@ -33,16 +33,17 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractDescribableImpl;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.model.Cause;
import hudson.model.Action;
import hudson.model.CauseAction;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.plugins.parameterizedtrigger.AbstractBuildParameters;
import hudson.plugins.parameterizedtrigger.BuildTrigger;
import hudson.plugins.parameterizedtrigger.BuildTriggerConfig;
import hudson.tasks.Publisher;
import hudson.util.DescribableList;
@@ -52,13 +53,13 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Set;
import java.util.LinkedHashMap;

/**
* <p>
@@ -203,12 +204,13 @@ protected CauseAction filterOutUserIdCause(CauseAction causeAction) {
public int triggerManualBuild(ItemGroup pipelineContext, Integer upstreamBuildNumber, String triggerProjectName,
String upstreamProjectName) {
final AbstractProject<?, ?> triggerProject =
(AbstractProject<?, ?>) Jenkins.getInstance().getItem(triggerProjectName, pipelineContext);
(AbstractProject<?, ?>) Jenkins.getInstance().getItem(triggerProjectName, pipelineContext);
final AbstractProject<?, ?> upstreamProject =
(AbstractProject<?, ?>) Jenkins.getInstance().getItem(upstreamProjectName, pipelineContext);
(AbstractProject<?, ?>) Jenkins.getInstance().getItem(upstreamProjectName, pipelineContext);

final AbstractBuild<?, ?> upstreamBuild = retrieveBuild(upstreamBuildNumber, upstreamProject);


// Get parameters from upstream build
if (upstreamBuild != null) {
LOGGER.fine("Getting parameters from upstream build " + upstreamBuild.getExternalizableId()); //$NON-NLS-1$
@@ -221,7 +223,6 @@ public int triggerManualBuild(ItemGroup pipelineContext, Integer upstreamBuildNu
return triggerBuild(triggerProject, upstreamBuild, buildParametersAction);
}


/**
* Schedules a build to start.
*
@@ -253,7 +254,8 @@ protected int triggerBuild(final AbstractProject<?, ?> triggerProject, final Abs
if (upstreamBuild != null) {


final List<AbstractBuildParameters> configs = retrieveUpstreamProjectTriggerConfig(triggerProject, upstreamBuild);
final List<AbstractBuildParameters> configs = retrieveUpstreamProjectTriggerConfig(triggerProject,
upstreamBuild);

if (configs == null) {
LOGGER.log(Level.SEVERE, "No upstream trigger found for this project: " + triggerProject.getFullDisplayName());
@@ -298,13 +300,28 @@ protected int triggerBuild(final AbstractProject<?, ?> triggerProject, final Abs

List<AbstractBuildParameters> configs = null;

final hudson.tasks.BuildTrigger autoTrigger =
upstreamProjectPublishersList.get(hudson.tasks.BuildTrigger.class);
if (autoTrigger != null) {
LOGGER.fine("Found Hudson Trigger (BuildTrigger) found in upstream project publisher list ");
final String downstreamProjects = autoTrigger.getChildProjectsValue();
if (downstreamProjects.contains(project.getFullName())) {
configs = new ArrayList<AbstractBuildParameters>();
} else {
LOGGER.warning("Upstream project had a Hudson BuildTrigger for projects [" + downstreamProjects
+ "], but that did not include our project [" + project.getFullName() + "]");
}
}

final BuildPipelineTrigger manualTrigger = upstreamProjectPublishersList.get(BuildPipelineTrigger.class);
if (manualTrigger != null) {
LOGGER.fine("Found Manual Trigger (BuildPipelineTrigger) found in upstream project publisher list ");
final Set<String> downstreamProjectsNames =
Sets.newHashSet(Splitter.on(",").trimResults().split(manualTrigger.getDownstreamProjectNames()));

LOGGER.fine("Downstream project names: " + downstreamProjectsNames);
// defect: requires full name in the trigger. But downstream is just fine!

if (downstreamProjectsNames.contains(project.getFullName())) {
configs = manualTrigger.getConfigs();
} else {
@@ -315,12 +332,18 @@ protected int triggerBuild(final AbstractProject<?, ?> triggerProject, final Abs
LOGGER.fine("No manual trigger found");
}

final BuildTrigger autoTrigger = upstreamProjectPublishersList.get(BuildTrigger.class);
if (autoTrigger != null) {
for (BuildTriggerConfig config : autoTrigger.getConfigs()) {
final hudson.plugins.parameterizedtrigger.BuildTrigger autoParameterizedTrigger =
upstreamProjectPublishersList.get(hudson.plugins.parameterizedtrigger.BuildTrigger.class);
if (autoParameterizedTrigger != null) {
LOGGER.fine("Found Parameterized Trigger (BuildTrigger) found in upstream project publisher list ");
for (BuildTriggerConfig config : autoParameterizedTrigger.getConfigs()) {
final Set<String> downstreamProjectsNames = Sets.newHashSet(Splitter.on(",").trimResults().split(config.getProjects()));
if (downstreamProjectsNames.contains(project.getFullName())) {
configs = config.getConfigs();
for (String currentDownstreamName : downstreamProjectsNames) {
final Job downstreamJob = (Job) Jenkins.getInstance().getItem(currentDownstreamName,
upstreamBuild.getProject().getParent(), Item.class);
if (downstreamJob != null && downstreamJob.getFullName().equals(project.getFullName())) {
configs = config.getConfigs();
}
}
}
}
@@ -31,27 +31,28 @@
import au.com.centrumsystems.hudson.plugin.buildpipeline.BuildPipelineView;
import au.com.centrumsystems.hudson.plugin.buildpipeline.DownstreamProjectGridBuilder;
import au.com.centrumsystems.hudson.plugin.buildpipeline.extension.StandardBuildCard;
import hudson.model.AbstractProject;
import hudson.model.Cause;
import hudson.model.Descriptor;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Hudson;
import hudson.plugins.parameterizedtrigger.AbstractBuildParameters;
import hudson.EnvVars;
import hudson.model.*;
import hudson.plugins.parameterizedtrigger.*;

import hudson.tasks.Publisher;
import hudson.util.DescribableList;
import hudson.util.FormValidation;

import java.io.IOException;
import java.util.Collections;
import java.util.NoSuchElementException;


import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.*;


import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* BuildPipelineTrigger test class
@@ -246,6 +247,87 @@ public void testManualTriggerCause() throws Exception
assertNotNull(upstreamCause);
}

@Test
@Issue("JENKINS-23532")
public void testTriggerProjectInFolderUsingParameteriezedTrigger()
throws Exception
{
//root folder
MockFolder folder1 = jenkins.createFolder("Folder1");
MockFolder folder2 = jenkins.createFolder("Folder2");

// /Folder1
final String projUp = "upstream";
folder1.createProject(FreeStyleProject.class, projUp);
//FreeStyleProject upstreamProject = (FreeStyleProject) folder1.getItem(projUp);
FreeStyleProject upstreamProject = jenkins.createFreeStyleProject(projUp);

// /Folder2
final String projDown = "downstream";
folder2.createProject(FreeStyleProject.class, projDown);
FreeStyleProject downstreamProject = (FreeStyleProject) folder2.getItem(projDown);

BuildTriggerConfig notFakeConfig = new BuildTriggerConfig("Folder2/downstream", ResultCondition.ALWAYS, false,
Collections.singletonList((AbstractBuildParameters) new CurrentBuildParameters()));
upstreamProject.getPublishersList().add(new hudson.plugins.parameterizedtrigger.BuildTrigger(notFakeConfig));
jenkins.getInstance().rebuildDependencyGraph();

BuildPipelineView view = new BuildPipelineView("Pipeline", "Title", new DownstreamProjectGridBuilder("upstream"), "1", false, "");
view.setBuildCard(new StandardBuildCard());
jenkins.buildAndAssertSuccess(upstreamProject);

view.triggerManualBuild(1, downstreamProject.getFullName(), upstreamProject.getFullName());

jenkins.waitUntilNoActivity();

assertNotNull(downstreamProject.getLastBuild());
FreeStyleBuild build = downstreamProject.getLastBuild();
Cause.UserIdCause cause = build.getCause(Cause.UserIdCause.class);
assertNotNull(cause);
//Check that cause is of core class Cause.UserIdCause and not MyUserIdCause
assertEquals(Cause.UserIdCause.class.getName(), cause.getClass().getName());
Cause.UpstreamCause upstreamCause = build.getCause(Cause.UpstreamCause.class);
assertNotNull(upstreamCause);
}

@Test
public void testTriggerProjectInFolderUsingHudsonTrigger()
throws Exception
{
//root folder
MockFolder folder1 = jenkins.createFolder("Folder1");
MockFolder folder2 = jenkins.createFolder("Folder2");

// /folder1
final String projUp = "upstream";
folder1.createProject(FreeStyleProject.class, projUp);
FreeStyleProject upstreamProject = (FreeStyleProject) folder1.getItem(projUp);

// /folder2
final String projDown = "downstream";
folder2.createProject(FreeStyleProject.class, projDown);
FreeStyleProject downstreamProject = (FreeStyleProject) folder2.getItem(projDown);

upstreamProject.getPublishersList().add(new hudson.tasks.BuildTrigger(downstreamProject.getFullName(), false));
jenkins.getInstance().rebuildDependencyGraph();

BuildPipelineView view = new BuildPipelineView("Pipeline", "Title", new DownstreamProjectGridBuilder("upstream"), "1", false, "");
view.setBuildCard(new StandardBuildCard());
jenkins.buildAndAssertSuccess(upstreamProject);

view.triggerManualBuild(1, downstreamProject.getFullName(), upstreamProject.getFullName());

jenkins.waitUntilNoActivity();

assertNotNull(downstreamProject.getLastBuild());
FreeStyleBuild build = downstreamProject.getLastBuild();
Cause.UserIdCause cause = build.getCause(Cause.UserIdCause.class);
assertNotNull(cause);
assertEquals(Cause.UserIdCause.class.getName(), cause.getClass().getName());
Cause.UpstreamCause upstreamCause = build.getCause(Cause.UpstreamCause.class);
assertNotNull(upstreamCause);
}

@Test
@Issue("JENKINS-24883")
public void testReRunBuildPipelineTrigger()

0 comments on commit 6300e17

Please sign in to comment.
You can’t perform that action at this time.