Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-40926] Persist input submission parameters and user on…
… node
  • Loading branch information
abayer committed Apr 5, 2017
1 parent 1031ad8 commit fa9a728
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 12 deletions.
Expand Up @@ -30,6 +30,7 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.interceptor.RequirePOST;

import javax.annotation.CheckForNull;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -156,24 +157,33 @@ public HttpResponse doSubmit(StaplerRequest request) throws IOException, Servlet
@RequirePOST
public HttpResponse doProceed(StaplerRequest request) throws IOException, ServletException, InterruptedException {
preSubmissionCheck();
Object v = parseValue(request);
Map<String,Object> v = parseValue(request);
return proceed(v);
}

/**
* Processes the acceptance (approval) request.
* This method is used by both {@link #doProceedEmpty()} and {@link #doProceed(StaplerRequest)}
*
* @param v An object that represents the parameters sent in the request
* @param params A map that represents the parameters sent in the request
* @return A HttpResponse object that represents Status code (200) indicating the request succeeded normally.
*/
public HttpResponse proceed(Object v) {
public HttpResponse proceed(@CheckForNull Map<String,Object> params) {
User user = User.current();
String approverId = null;
if (user != null){
run.addAction(new ApproverAction(user.getId()));
approverId = user.getId();
run.addAction(new ApproverAction(approverId));
listener.getLogger().println("Approved by " + hudson.console.ModelHyperlinkNote.encodeTo(user));
}
node.addAction(new InputSubmittedAction(approverId, params));

Object v;
if (params != null && params.size() == 1) {
v = params.values().iterator().next();
} else {
v = params;
}
outcome = new Outcome(v, null);
postSettlement();
getContext().onSuccess(v);
Expand Down Expand Up @@ -280,7 +290,7 @@ private boolean canSettle(Authentication a) {
/**
* Parse the submitted {@link ParameterValue}s
*/
private Object parseValue(StaplerRequest request) throws ServletException, IOException, InterruptedException {
private Map<String,Object> parseValue(StaplerRequest request) throws ServletException, IOException, InterruptedException {
Map<String, Object> mapResult = new HashMap<String, Object>();
List<ParameterDefinition> defs = input.getParameters();

Expand Down Expand Up @@ -313,12 +323,9 @@ private Object parseValue(StaplerRequest request) throws ServletException, IOExc
mapResult.put(valueName, a.getName());
}

switch (mapResult.size()) {
case 0:
return null; // no value if there's no parameter
case 1:
return mapResult.values().iterator().next();
default:
if (mapResult.isEmpty()) {
return null;
} else {
return mapResult;
}
}
Expand Down
@@ -0,0 +1,70 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.support.steps.input;

import org.jenkinsci.plugins.workflow.actions.PersistentAction;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.LinkedHashMap;
import java.util.Map;

public class InputSubmittedAction implements PersistentAction {

private final Map<String,Object> parameters = new LinkedHashMap<>();
private final String approver;

public InputSubmittedAction(String approver, @CheckForNull Map<String,Object> parameters) {
this.approver = approver;
if (parameters != null) {
this.parameters.putAll(parameters);
}
}

@Nonnull
public Map<String,Object> getParameters() {
return parameters;
}

@CheckForNull
public String getApprover() {
return approver;
}

@Override
public String getIconFileName() {
return null;
}

@Override
public String getDisplayName() {
return "Input Submitted";
}

@Override
public String getUrlName() {
return null;
}
}
Expand Up @@ -27,6 +27,7 @@
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.google.common.base.Predicate;
import hudson.model.BooleanParameterDefinition;
import hudson.model.Job;
import hudson.model.Result;
Expand All @@ -39,6 +40,8 @@
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Assert;
Expand All @@ -48,8 +51,12 @@
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Arrays;
import java.util.Map;

import org.jvnet.hudson.test.MockAuthorizationStrategy;

import javax.annotation.Nullable;

/**
* @author Kohsuke Kawaguchi
*/
Expand Down Expand Up @@ -120,7 +127,25 @@ public void parameter() throws Exception {
//make sure the approver name corresponds to the submitter
ApproverAction action = b.getAction(ApproverAction.class);
assertNotNull(action);
assertEquals("alice", action.getUserId());;
assertEquals("alice", action.getUserId());

DepthFirstScanner scanner = new DepthFirstScanner();

FlowNode nodeWithInputSubmittedAction = scanner.findFirstMatch(e.getCurrentHeads(), null, new Predicate<FlowNode>() {
@Override
public boolean apply(@Nullable FlowNode input) {
return input != null && input.getAction(InputSubmittedAction.class) != null;
}
});
assertNotNull(nodeWithInputSubmittedAction);
InputSubmittedAction inputSubmittedAction = nodeWithInputSubmittedAction.getAction(InputSubmittedAction.class);
assertNotNull(inputSubmittedAction);

assertEquals("alice", inputSubmittedAction.getApprover());
Map<String,Object> submittedParams = inputSubmittedAction.getParameters();
assertEquals(1, submittedParams.size());
assertTrue(submittedParams.containsKey("chocolate"));
assertEquals(false, submittedParams.get("chocolate"));
}

@Test
Expand Down

0 comments on commit fa9a728

Please sign in to comment.