Skip to content

Commit

Permalink
Fix check webmethod for BuildTriggerConfig projectList and clean up J…
Browse files Browse the repository at this point in the history
…elly that used old idioms

Repairs defect found in demo that for workflow jobs, UI shows "project is not buildable" error in UI
Also, remove some trailing whitespace
  • Loading branch information
svanoort committed Aug 5, 2015
1 parent 4b5c5a0 commit 072465e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.queue.Tasks;
import hudson.plugins.parameterizedtrigger.AbstractBuildParameters.DontTriggerException;
import hudson.plugins.promoted_builds.Promotion;
import hudson.security.ACL;
import hudson.tasks.Messages;
import hudson.Util;
import hudson.util.FormValidation;
import hudson.util.VersionNumber;

import jenkins.model.Jenkins;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import org.acegisecurity.Authentication;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -421,10 +425,10 @@ private List<List<AbstractBuildParameters>> getDynamicBuildParameters(AbstractBu

/**
* Create UpstreamCause that triggers a downstream build.
*
*
* If the upstream build is a promotion, return the UpstreamCause
* as triggered by the target of the promotion.
*
*
* @param build an upstream build
* @return UpstreamCause
*/
Expand All @@ -433,10 +437,10 @@ protected Cause createUpstreamCause(Run<?, ?> build) {
// Test only when promoted-builds is installed.
if(build instanceof Promotion) {
Promotion promotion = (Promotion)build;

// This cannot be done for PromotionCause#PromotionCause is in a package scope.
// return new PromotionCause(build, promotion.getTarget());

return new UpstreamCause((Run<?,?>)promotion.getTarget());
}
}
Expand Down Expand Up @@ -479,11 +483,11 @@ protected Future schedule(AbstractBuild<?, ?> build, Job project, List<Action> l

/**
* A backport of {@link Items#computeRelativeNamesAfterRenaming(String, String, String, ItemGroup)} in Jenkins 1.530.
*
*
* computeRelativeNamesAfterRenaming contains a bug in Jenkins < 1.530.
* Replace this to {@link Items#computeRelativeNamesAfterRenaming(String, String, String, ItemGroup)}
* when updated the target version to >= 1.530.
*
*
* @param oldFullName
* @param newFullName
* @param relativeNames
Expand Down Expand Up @@ -593,7 +597,7 @@ public List<Descriptor<AbstractBuildParameterFactory>> getBuilderConfigFactoryDe
*
* Copied from hudson.tasks.BuildTrigger.doCheck(Item project, String value)
*/
public FormValidation doCheckProjects(@AncestorInPath AbstractProject<?,?> project, @QueryParameter String value ) {
public FormValidation doCheckProjects(@AncestorInPath Job<?,?> project, @QueryParameter String value ) {
// Require CONFIGURE permission on this project
if(!project.hasPermission(Item.CONFIGURE)){
return FormValidation.ok();
Expand All @@ -605,17 +609,29 @@ public FormValidation doCheckProjects(@AncestorInPath AbstractProject<?,?> proje
if (StringUtils.isNotBlank(projectName)) {
Item item = Jenkins.getInstance().getItem(projectName,project,Item.class); // only works after version 1.410
if(item==null){
return FormValidation.error(Messages.BuildTrigger_NoSuchProject(projectName,AbstractProject.findNearest(projectName).getName()));
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 AbstractProject)){
if(!(item instanceof Job)){
return FormValidation.error(Messages.BuildTrigger_NotBuildable(projectName));
}

// check whether the supposed user is expected to be able to build
if (project instanceof ParameterizedJobMixIn.ParameterizedJob) {

This comment has been minimized.

Copy link
@jglick

jglick Aug 5, 2015

🐜 if !(project instanceof ParameterizedJobMixIn.ParameterizedJob) then you will not be able to trigger that job so this should be treated as an error (BuildTrigger.NotBuildable as above).

Authentication auth = Tasks.getAuthenticationOf((ParameterizedJobMixIn.ParameterizedJob)project);
if (auth.equals(ACL.SYSTEM) && !QueueItemAuthenticatorConfiguration.get().getAuthenticators().isEmpty()) {
auth = Jenkins.ANONYMOUS; // compare behavior in execute, above

This comment has been minimized.

Copy link
@jglick

jglick Aug 5, 2015

I guess this comment was copied from hudson.tasks.BuildTrigger where it made sense.

}
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()); // only works with Jenkins version built after 2011-01-30
return FormValidation.error("No project specified");
return FormValidation.error(Messages.BuildTrigger_NoProjectSpecified());
}

return FormValidation.ok();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%Projects to build}">
<f:textbox name="buildTrigger.projects" value="${instance.projectsValue}"
checkUrl="'descriptorByName/hudson.tasks.BuildTrigger/check?value='+encodeURIComponent(this.value)"
autoCompleteDelimChar=","
field="projects"/>
<f:entry title="${%Projects to build}" field="projects">
<f:textbox autoCompleteDelimChar="," />
</f:entry>
<f:entry title="${%Trigger when build is}" field="condition">
<f:enum>${it.displayName}</f:enum>
</f:entry>
<f:entry title="${%Trigger build without parameters}" field="triggerWithNoParameters" >
<f:checkbox checked="${instance.triggerWithNoParameters}"/>
<f:checkbox checked="${instance.triggerWithNoParameters}"/>
</f:entry>

<f:block>
Expand Down

0 comments on commit 072465e

Please sign in to comment.