Skip to content

Commit

Permalink
[JENKINS-24626] Added tests for DownstreamBuildSelector.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikedam committed Sep 21, 2014
1 parent 7277e1d commit 8321927
Show file tree
Hide file tree
Showing 2 changed files with 642 additions and 11 deletions.
Expand Up @@ -35,9 +35,11 @@

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Item;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Run;
import hudson.util.FormValidation;

Expand All @@ -46,6 +48,7 @@
*/
public class DownstreamBuildSelector extends BuildSelector {
private static final Logger LOGGER = Logger.getLogger(DownstreamBuildSelector.class.getName());
private static final String COPIER_PROJECT_KEY = "___COPIER_PROJECT_KEY___";
private final String upstreamProjectName;
private final String upstreamBuildNumber;

Expand Down Expand Up @@ -73,13 +76,27 @@ public String getUpstreamBuildNumber() {
return upstreamBuildNumber;
}

@Override
public Run<?, ?> getBuild(Job<?, ?> job, EnvVars env, BuildFilter filter, Run<?, ?> parent) {
EnvVars extendedEnv = new EnvVars(env);
// Workaround to pass who is copier to isSelectable().
extendedEnv.put(COPIER_PROJECT_KEY, parent.getParent().getFullName());
return super.getBuild(job, extendedEnv, filter, parent);
}

@Override
protected boolean isSelectable(Run<?, ?> run, EnvVars env) {
if (!(run instanceof AbstractBuild<?,?>)) {
LOGGER.warning(String.format("Only applicable to AbstractBuild: but is %s.", run.getClass().getName()));
return false;
}

// Workaround to retrieve who is copying.
Job<?,?> copier = Jenkins.getInstance().getItemByFullName(env.get(COPIER_PROJECT_KEY), Job.class);
if (copier != null && (copier instanceof AbstractProject<?,?>)) {
copier = ((AbstractProject<?,?>)copier).getRootProject();
}

String projectName = env.expand(getUpstreamProjectName());
String buildNumber = env.expand(getUpstreamBuildNumber());

Expand All @@ -95,16 +112,16 @@ protected boolean isSelectable(Run<?, ?> run, EnvVars env) {

AbstractProject<?,?> upstreamProject = Jenkins.getInstance().getItem(
projectName,
run.getParent(),
copier,
AbstractProject.class
);
if (upstreamProject == null || !upstreamProject.hasPermission(Jenkins.READ)) {
LOGGER.warning(String.format("Upstream project '%s' is not found.", upstreamProject));
if (upstreamProject == null || !upstreamProject.hasPermission(Item.READ)) {
LOGGER.warning(String.format("Upstream project '%s' is not found.", projectName));
return false;
}
AbstractBuild<?,?> upstreamBuild = ((AbstractBuild<?,?>)run).getUpstreamRelationshipBuild(upstreamProject);
if (upstreamBuild == null || !upstreamBuild.hasPermission(Jenkins.READ)) {
LOGGER.fine(String.format("No upstream build of project '%s' is found for build %s-%s.", upstreamProject, run.getParent().getFullName(), run.getDisplayName()));
if (upstreamBuild == null || !upstreamBuild.hasPermission(Item.READ)) {
LOGGER.fine(String.format("No upstream build of project '%s' is found for build %s-%s.", upstreamProject.getFullName(), run.getParent().getFullName(), run.getDisplayName()));
return false;
}

Expand Down Expand Up @@ -161,9 +178,9 @@ public FormValidation doCheckUpstreamProjectName(
}

AbstractProject<?,?> upstreamProject = Jenkins.getInstance().getItem(
upstreamProjectName, project, AbstractProject.class
upstreamProjectName, project.getRootProject(), AbstractProject.class
);
if (upstreamProject == null || upstreamProject.hasPermission(Jenkins.READ)) {
if (upstreamProject == null || !upstreamProject.hasPermission(Item.READ)) {
return FormValidation.error(Messages.DownstreamBuildSelector_UpstreamProjectName_NotFound());
}
return FormValidation.ok();
Expand Down Expand Up @@ -198,16 +215,16 @@ public FormValidation doCheckUpstreamBuildNumber(
}

AbstractProject<?,?> upstreamProject = Jenkins.getInstance().getItem(
upstreamProjectName, project, AbstractProject.class
upstreamProjectName, project.getRootProject(), AbstractProject.class
);
if (upstreamProject == null || upstreamProject.hasPermission(Jenkins.READ)) {
if (upstreamProject == null || !upstreamProject.hasPermission(Item.READ)) {
return FormValidation.ok();
}

try {
int number = Integer.parseInt(upstreamBuildNumber);
AbstractBuild<?,?> upstreamBuild = upstreamProject.getBuildByNumber(number);
if (upstreamBuild != null && upstreamBuild.hasPermission(Jenkins.READ)) {
if (upstreamBuild != null && upstreamBuild.hasPermission(Item.READ)) {
// build number matches.
return FormValidation.ok();
}
Expand All @@ -217,7 +234,7 @@ public FormValidation doCheckUpstreamBuildNumber(

{
AbstractBuild<?,?> upstreamBuild = upstreamProject.getBuild(upstreamBuildNumber);
if (upstreamBuild != null && upstreamBuild.hasPermission(Jenkins.READ)) {
if (upstreamBuild != null && upstreamBuild.hasPermission(Item.READ)) {
// build id matches.
return FormValidation.ok();
}
Expand Down

0 comments on commit 8321927

Please sign in to comment.