Skip to content

Commit

Permalink
Merge pull request #8 from jenkinsci/submitter-variable
Browse files Browse the repository at this point in the history
[FIXED JENKINS-31396] Add the submitterParameter property
  • Loading branch information
jglick committed Nov 9, 2016
2 parents 2a413ef + c893d24 commit 1006125
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
Expand Up @@ -36,6 +36,11 @@ public class InputStep extends AbstractStepImpl implements Serializable {
*/
private String submitter;

/**
* Optional parameter name to stored the user who responded to the input.
*/
private String submitterParameter;


/**
* Either a single {@link ParameterDefinition} or a list of them.
Expand Down Expand Up @@ -73,6 +78,12 @@ public String getSubmitter() {
this.submitter = Util.fixEmptyAndTrim(submitter);
}

public String getSubmitterParameter() { return submitterParameter; }

@DataBoundSetter public void setSubmitterParameter(String submitterParameter) {
this.submitterParameter = Util.fixEmptyAndTrim(submitterParameter);
}

private String capitalize(String id) {
if (id==null)
return null;
Expand Down
Expand Up @@ -306,8 +306,13 @@ private Object parseValue(StaplerRequest request) throws ServletException, IOExc
}
}

// TODO: perhaps we should return a different object to allow the workflow to look up
// who approved it, etc?
// If a destination value is specified, push the submitter to it.
String valueName = input.getSubmitterParameter();
if (valueName != null && !valueName.isEmpty()) {
Authentication a = Jenkins.getAuthentication();
mapResult.put(valueName, a.getName());
}

switch (mapResult.size()) {
case 0:
return null; // no value if there's no parameter
Expand Down
Expand Up @@ -38,6 +38,9 @@ THE SOFTWARE.
<f:entry field="submitter" title="Allowed Submitter">
<f:textbox/>
</f:entry>
<f:entry field="submitterParameter" title="Parameter to store the approving submitter">
<f:textbox/>
</f:entry>
<f:entry field="parameters" title="Parameters">
<f:repeatableHeteroProperty field="parameters"/>
</f:entry>
Expand Down
@@ -0,0 +1,6 @@
<div>
If specified, this is the name of the return value that will contain the ID of the user that approves this
input.

The return value will be handled in a fashion similar to the <code>parameters</code> value.
</div>
Expand Up @@ -166,6 +166,77 @@ public void test_submitters() throws Exception {
runAndAbort(webClient, foo, "charlie", false); // charlie shouldn't work coz he's not declared as 'submitter' and doesn't have Job.CANCEL privs
}

@Test
@Issue("JENKINS-31396")
public void test_submitter_parameter() throws Exception {
//set up dummy security real
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
// job setup
WorkflowJob foo = j.jenkins.createProject(WorkflowJob.class, "foo");
foo.setDefinition(new CpsFlowDefinition(StringUtils.join(Arrays.asList(
"def x = input message:'Do you want chocolate?', id:'Icecream', ok: 'Purchase icecream', submitter:'alice,bob', submitterParameter: 'approval';",
"echo(\"after: ${x}\");"),"\n"),true));

// get the build going, and wait until workflow pauses
QueueTaskFuture<WorkflowRun> q = foo.scheduleBuild2(0);
WorkflowRun b = q.getStartCondition().get();
j.waitForMessage("input", b);

// make sure we are pausing at the right state that reflects what we wrote in the program
InputAction a = b.getAction(InputAction.class);
assertEquals(1, a.getExecutions().size());

InputStepExecution is = a.getExecution("Icecream");
assertEquals("Do you want chocolate?", is.getInput().getMessage());
assertEquals("alice,bob", is.getInput().getSubmitter());

// submit the input, and run workflow to the completion
JenkinsRule.WebClient wc = j.createWebClient();
wc.login("alice");
HtmlPage p = wc.getPage(b, a.getUrlName());
j.submit(p.getFormByName(is.getId()), "proceed");
assertEquals(0, a.getExecutions().size());
q.get();

// make sure 'x' gets 'alice'
j.assertLogContains("after: alice", b);
}

@Test
@Issue("JENKINS-31396")
public void test_submitter_parameter_no_submitter() throws Exception {
//set up dummy security real
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
// job setup
WorkflowJob foo = j.jenkins.createProject(WorkflowJob.class, "foo");
foo.setDefinition(new CpsFlowDefinition(StringUtils.join(Arrays.asList(
"def x = input message:'Do you want chocolate?', id:'Icecream', ok: 'Purchase icecream', submitterParameter: 'approval';",
"echo(\"after: ${x}\");"),"\n"),true));

// get the build going, and wait until workflow pauses
QueueTaskFuture<WorkflowRun> q = foo.scheduleBuild2(0);
WorkflowRun b = q.getStartCondition().get();
j.waitForMessage("input", b);

// make sure we are pausing at the right state that reflects what we wrote in the program
InputAction a = b.getAction(InputAction.class);
assertEquals(1, a.getExecutions().size());

InputStepExecution is = a.getExecution("Icecream");
assertEquals("Do you want chocolate?", is.getInput().getMessage());

// submit the input, and run workflow to the completion
JenkinsRule.WebClient wc = j.createWebClient();
wc.login("alice");
HtmlPage p = wc.getPage(b, a.getUrlName());
j.submit(p.getFormByName(is.getId()), "proceed");
assertEquals(0, a.getExecutions().size());
q.get();

// make sure 'x' gets 'alice'
j.assertLogContains("after: alice", b);
}

private void runAndAbort(JenkinsRule.WebClient webClient, WorkflowJob foo, String loginAs, boolean expectAbortOk) throws Exception {
// get the build going, and wait until workflow pauses
QueueTaskFuture<WorkflowRun> queueTaskFuture = foo.scheduleBuild2(0);
Expand Down

0 comments on commit 1006125

Please sign in to comment.