Skip to content

Commit

Permalink
Merge pull request #111 from Jimilian/fix_extra_comma_error
Browse files Browse the repository at this point in the history
Fix extra comma error in list of projects
  • Loading branch information
olivergondza authored Jan 2, 2017
2 parents f82bea4 + 8cb5f4c commit c2a8b82
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -645,28 +645,30 @@ public FormValidation doCheckProjects(@AncestorInPath Job<?,?> project, @QueryPa
boolean hasProjects = false;
while(tokens.hasMoreTokens()) {
String projectName = tokens.nextToken().trim();
if (StringUtils.isNotBlank(projectName)) {
Item item = Jenkins.getInstance().getItem(projectName,project,Item.class); // only works after version 1.410
if(item==null){
Item nearest = Items.findNearest(Job.class, projectName, Jenkins.getInstance());
String alternative = nearest != null ? nearest.getRelativeNameFrom(project) : "?";
return FormValidation.error(Messages.BuildTrigger_NoSuchProject(projectName, alternative));
}
if(!(item instanceof Job) || !(item instanceof ParameterizedJobMixIn.ParameterizedJob)) {
return FormValidation.error(Messages.BuildTrigger_NotBuildable(projectName));
}
if (StringUtils.isBlank(projectName)) {
return FormValidation.error("Blank project name in the list");
}

// check whether the supposed user is expected to be able to build
Authentication auth = Tasks.getAuthenticationOf((ParameterizedJobMixIn.ParameterizedJob)project);
if (auth.equals(ACL.SYSTEM) && !QueueItemAuthenticatorConfiguration.get().getAuthenticators().isEmpty()) {
auth = Jenkins.ANONYMOUS;
}
if (!item.getACL().hasPermission(auth, Item.BUILD)) {
return FormValidation.error(Messages.BuildTrigger_you_have_no_permission_to_build_(projectName));
}
Item item = Jenkins.getInstance().getItem(projectName,project,Item.class); // only works after version 1.410
if(item==null){
Item nearest = Items.findNearest(Job.class, projectName, Jenkins.getInstance());
String alternative = nearest != null ? nearest.getRelativeNameFrom(project) : "?";
return FormValidation.error(Messages.BuildTrigger_NoSuchProject(projectName, alternative));
}
if(!(item instanceof Job) || !(item instanceof ParameterizedJobMixIn.ParameterizedJob)) {
return FormValidation.error(Messages.BuildTrigger_NotBuildable(projectName));
}

hasProjects = true;
// check whether the supposed user is expected to be able to build
Authentication auth = Tasks.getAuthenticationOf((ParameterizedJobMixIn.ParameterizedJob)project);
if (auth.equals(ACL.SYSTEM) && !QueueItemAuthenticatorConfiguration.get().getAuthenticators().isEmpty()) {
auth = Jenkins.ANONYMOUS;
}
if (!item.getACL().hasPermission(auth, Item.BUILD)) {
return FormValidation.error(Messages.BuildTrigger_you_have_no_permission_to_build_(projectName));
}

hasProjects = true;
}
if (!hasProjects) {
return FormValidation.error(Messages.BuildTrigger_NoProjectSpecified());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public static String readFileToString(FilePath f, String encoding) throws IOExce
}
}

/**
* {@link read VirtualFile
/**
* {@link} read VirtualFile
*
* @param f file to read
* @return read string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import hudson.security.AuthorizationMatrixProperty;
import hudson.security.Permission;
import hudson.security.ProjectMatrixAuthorizationStrategy;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand Down Expand Up @@ -307,4 +308,39 @@ public void testGetProjectListTriggered() throws Exception {

}

@Test
public void testBlankConfig() throws Exception {
Project<?, ?> masterProject = r.createFreeStyleProject("project");

FormValidation form = r.jenkins.getDescriptorByType(BuildTriggerConfig.DescriptorImpl.class).doCheckProjects(masterProject, "");

assertEquals(FormValidation.Kind.ERROR, form.kind);
}

@Test
public void testNonExistedProject() throws Exception {
Project<?, ?> masterProject = r.createFreeStyleProject("project");

FormValidation form = r.jenkins.getDescriptorByType(BuildTriggerConfig.DescriptorImpl.class).doCheckProjects(masterProject, "nonExistedProject");

assertEquals(FormValidation.Kind.ERROR, form.kind);
}

@Test
public void testValidConfig() throws Exception {
Project<?, ?> masterProject = r.createFreeStyleProject("project");

FormValidation form = r.jenkins.getDescriptorByType(BuildTriggerConfig.DescriptorImpl.class).doCheckProjects(masterProject, "project");

assertEquals(FormValidation.Kind.OK, form.kind);
}

@Test
public void testBlankProjectNameInConfig() throws Exception {
Project<?, ?> masterProject = r.createFreeStyleProject("project");

FormValidation form = r.jenkins.getDescriptorByType(BuildTriggerConfig.DescriptorImpl.class).doCheckProjects(masterProject, "project, ");

assertEquals(FormValidation.Kind.ERROR, form.kind);
}
}

0 comments on commit c2a8b82

Please sign in to comment.