Skip to content

Commit

Permalink
[JENKINS-32527] Handle null @AncestorInPath when selecting Downstream… (
Browse files Browse the repository at this point in the history
#98)

* [JENKINS-32527] Handle null @AncestorInPath when selecting Downstream Jobs

* [JENKINS-32527] Resolved conflict
  • Loading branch information
Dohbedoh authored and kwhetstone committed Feb 27, 2017
1 parent 9968041 commit ffd67b5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
Expand Up @@ -661,6 +661,8 @@ public boolean isItemGroup(AbstractProject project){
* Copied from hudson.tasks.BuildTrigger.doCheck(Item project, String value)
*/
public FormValidation doCheckProjects(@AncestorInPath Job<?,?> project, @QueryParameter String value ) {
// JENKINS-32527: Check that it behaves gracefully for an unknown context
if (project == null) return FormValidation.ok("Context Unknown: the value specified cannot be validated");
// Require CONFIGURE permission on this project
if(!project.hasPermission(Item.CONFIGURE)){
return FormValidation.ok();
Expand Down
Expand Up @@ -45,6 +45,7 @@
import hudson.security.ProjectMatrixAuthorizationStrategy;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;
import org.acegisecurity.context.SecurityContextHolder;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand All @@ -62,16 +63,19 @@
import java.util.Set;

import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.Issue;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

public class BuildTriggerConfigTest {

@Rule
public JenkinsRule r = new JenkinsRule();

private BlockableBuildTriggerConfig createConfig(String projectToTrigger){
List<AbstractBuildParameters> buildParameters = new ArrayList<AbstractBuildParameters>();
buildParameters.add(new CurrentBuildParameters());
Expand Down Expand Up @@ -204,7 +208,7 @@ public void testBuildWithWorkflowProjects() throws Exception {
r.assertBuildStatusSuccess(workflowRun);
r.assertLogContains("GOOBER", workflowRun);
}

@Bug(31727)
@Test
public void testShouldNotFailOnDiscoverWithoutReadPermission() throws Exception {
Expand All @@ -215,42 +219,42 @@ public void testShouldNotFailOnDiscoverWithoutReadPermission() throws Exception
strategy.add(Item.DISCOVER, "anonymous");
strategy.add(Jenkins.READ, "anonymous");
r.jenkins.setAuthorizationStrategy(strategy);

// Create project with downstream trigger
final FreeStyleProject downstreamProject = r.createFreeStyleProject("downstreamProject");
final FreeStyleProject upstreamProject = r.createFreeStyleProject("upstreamProject");
final BlockableBuildTriggerConfig triggerConfg = createConfig("downstreamProject");
addParameterizedTrigger(upstreamProject, triggerConfg);

// Setup upstream project security
Map<Permission,Set<String>> permissions = new HashMap<Permission,Set<String>>();
Set<String> userIds = new HashSet<String>(Arrays.asList("testUser"));
permissions.put(Item.READ, userIds);
AuthorizationMatrixProperty projectPermissions = new AuthorizationMatrixProperty(permissions);
upstreamProject.addProperty(projectPermissions);

// Ensure that we can get the info about the downstream project, but it is unresolved
ACL.impersonate(user.impersonate(), new Runnable() {
@Override
public void run() {
public void run() {
SubProjectData projectInfo = triggerConfg.getProjectInfo(upstreamProject);
assertTrue("Downstream project should be unresolved, because testUser has no READ permission",
assertTrue("Downstream project should be unresolved, because testUser has no READ permission",
projectInfo.getUnresolved().contains(downstreamProject.getName()));
}
});

// Now invoke the build and check again (other logic handlers)
r.buildAndAssertSuccess(upstreamProject);
ACL.impersonate(user.impersonate(), new Runnable() {
@Override
public void run() {
public void run() {
SubProjectData projectInfo = triggerConfg.getProjectInfo(upstreamProject);
assertTrue("Downstream project should be unresolved, because testUser has no READ permission",
assertTrue("Downstream project should be unresolved, because testUser has no READ permission",
projectInfo.getUnresolved().contains(downstreamProject.getName()));
}
});
}

/**
* Testing statically and dynamically defined projects
*
Expand Down Expand Up @@ -343,4 +347,26 @@ public void testBlankProjectNameInConfig() throws Exception {

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

@Issue("JENKINS-32527")
@Test
public void testFieldValidation() throws Exception {
FreeStyleProject p = r.createFreeStyleProject("project");
BuildTriggerConfig.DescriptorImpl descriptor = r.jenkins.getDescriptorByType(BuildTriggerConfig.DescriptorImpl.class);
assertNotNull(descriptor);
// Valid value, Empty Value
assertSame(FormValidation.Kind.OK, descriptor.doCheckProjects(p, p.getFullName()).kind);
assertSame(FormValidation.Kind.ERROR, descriptor.doCheckProjects(p, "FOO").kind);
assertSame(FormValidation.Kind.ERROR, descriptor.doCheckProjects(p, "").kind);
//JENKINS-32526: Check that it behaves gracefully for an unknown context.
assertSame(FormValidation.Kind.OK, descriptor.doCheckProjects(null, p.getFullName()).kind);
assertSame(FormValidation.Kind.OK, descriptor.doCheckProjects(null, "FOO").kind);
assertSame(FormValidation.Kind.OK, descriptor.doCheckProjects(null, "").kind);

// Just returns OK if no permission
r.jenkins.setAuthorizationStrategy(new ProjectMatrixAuthorizationStrategy());
SecurityContextHolder.clearContext();
assertSame(FormValidation.Kind.OK, descriptor.doCheckProjects(p, "").kind);
assertSame(FormValidation.Kind.OK, descriptor.doCheckProjects(null, "").kind);
}
}

1 comment on commit ffd67b5

@ranjitkumar518
Copy link

Choose a reason for hiding this comment

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

Sorry for Commenting on this pull request. I didn't find a proper way to create issue in this repo. Please find the issue here: https://issues.jenkins-ci.org/browse/JENKINS-9105
This issue is created in 2011. Still now there is no update. Can you provide a option to block the triggered job until its all child jobs executed.

Please sign in to comment.