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

Fix extra comma error in list of projects #111

Merged
merged 4 commits into from
Jan 2, 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
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);
}
}