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 3 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(Messages.BuildTrigger_NoSuchProject(" ", "?"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather return another warning ("Blank job name in the list")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other messages are using hudson.tasks.Messages from core. And most of them are localized. Is it ok to put custom message? It could break user's expectations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a new message, so IMHO it's fine. Jenkins core just skips such empty lines without any message:

public static <T extends Item> List<T> fromNameList(ItemGroup context, @Nonnull String list, @Nonnull Class<T> type) {
        final Jenkins jenkins = Jenkins.getInstance();
        
        List<T> r = new ArrayList<T>();
        if (jenkins == null) {
            return r;
        }
        
        StringTokenizer tokens = new StringTokenizer(list,",");
        while(tokens.hasMoreTokens()) {
            String fullName = tokens.nextToken().trim();
            if (StringUtils.isNotEmpty(fullName)) {
                T item = jenkins.getItem(fullName, context, type);
                if(item!=null)
                    r.add(item);
            }
        }
        return r;
    }

}

// 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 testExtraCommaInConfig() throws Exception {
Project<?, ?> masterProject = r.createFreeStyleProject("project");

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

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