-
Notifications
You must be signed in to change notification settings - Fork 61
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
Export some data from InputAction to the HTTP API #120
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,8 +57,9 @@ | |
import hudson.util.FormValidation.Kind; | ||
import hudson.util.Secret; | ||
import jenkins.model.IdStrategy; | ||
import jenkins.model.InterruptedBuildAction; | ||
import jenkins.model.Jenkins; | ||
import net.sf.json.JSONArray; | ||
import net.sf.json.JSONObject; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl; | ||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
|
@@ -78,6 +79,7 @@ | |
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import org.jvnet.hudson.test.MockAuthorizationStrategy; | ||
|
@@ -87,9 +89,8 @@ | |
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.arrayContaining; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
@@ -611,5 +612,86 @@ public void test_unsafe_ids_generate_formValidation() throws Exception { | |
assertThat("> should be rejected", d.doCheckId("this-is-also>-not-ok"), JenkinsMatchers.hasKind(Kind.ERROR)); | ||
} | ||
|
||
@Test | ||
public void test_api_contains_waitingForInput() 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:'Continue?';", | ||
"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(); | ||
Comment on lines
+626
to
+627
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These can be collapsed using (IIRC) |
||
j.waitForMessage("Continue?", b); | ||
|
||
final JenkinsRule.WebClient webClient = j.createWebClient(); | ||
JenkinsRule.JSONWebResponse json = webClient.getJSON(b.getUrl() + "api/json?depth=1"); | ||
JSONArray actions = json.getJSONObject().getJSONArray("actions"); | ||
Optional<Object> obj = actions.stream().filter(oo -> | ||
((JSONObject)oo).get("_class").equals("org.jenkinsci.plugins.workflow.support.steps.input.InputAction") | ||
).findFirst(); | ||
assertTrue(obj.isPresent()); | ||
JSONObject o = (JSONObject)obj.get(); | ||
assertTrue(o.has("waitingForInput")); | ||
assertTrue(o.getBoolean("waitingForInput")); | ||
|
||
InputAction inputAction = b.getAction(InputAction.class); | ||
InputStepExecution is = inputAction.getExecutions().get(0); | ||
HtmlPage p = webClient.getPage(b, inputAction.getUrlName()); | ||
j.submit(p.getFormByName(is.getId()), "proceed"); | ||
|
||
json = webClient.getJSON(b.getUrl() + "api/json?depth=1"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use |
||
actions = json.getJSONObject().getJSONArray("actions"); | ||
obj = actions.stream().filter(oo -> | ||
((JSONObject)oo).get("_class").equals("org.jenkinsci.plugins.workflow.support.steps.input.InputAction") | ||
).findFirst(); | ||
assertTrue(obj.isPresent()); | ||
o = (JSONObject)obj.get(); | ||
assertTrue(o.has("waitingForInput")); | ||
assertFalse(o.getBoolean("waitingForInput")); | ||
} | ||
|
||
@Test | ||
public void test_api_contains_details() 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 chosen = input message: 'Can we settle on this thing?', ok: 'Yep', parameters: [choice(choices: ['Apple', 'Blueberry', 'Banana'], description: 'The fruit in question.', name: 'fruit')], submitter: 'bobby', submitterParameter: 'dd'", | ||
"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("Can we settle on this thing?", b); | ||
|
||
final JenkinsRule.WebClient webClient = j.createWebClient(); | ||
final JenkinsRule.JSONWebResponse json = webClient.getJSON(b.getUrl() + "api/json?depth=2"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
final JSONArray actions = json.getJSONObject().getJSONArray("actions"); | ||
final Optional<Object> obj = actions.stream().filter(oo -> | ||
((JSONObject)oo).get("_class").equals("org.jenkinsci.plugins.workflow.support.steps.input.InputAction") | ||
).findFirst(); | ||
assertTrue(obj.isPresent()); | ||
final JSONObject o = (JSONObject)obj.get(); | ||
assertTrue(o.has("waitingForInput")); | ||
assertTrue(o.getBoolean("waitingForInput")); | ||
|
||
assertTrue(o.has("executions")); | ||
JSONObject exs = o.getJSONArray("executions").getJSONObject(0); | ||
assertEquals("Can we settle on this thing?", exs.getString("displayName")); | ||
assertTrue(exs.has("input")); | ||
JSONObject input = exs.getJSONObject("input"); | ||
assertEquals("Can we settle on this thing?", input.getString("displayName")); | ||
assertEquals("Yep", input.getString("ok")); | ||
assertEquals("bobby", input.getString("submitter")); | ||
assertTrue(input.has("parameters")); | ||
JSONObject param = input.getJSONArray("parameters").getJSONObject(0); | ||
assertEquals("fruit", param.getString("name")); | ||
assertEquals("ChoiceParameterDefinition", param.getString("type")); | ||
assertThat(param.getJSONArray("choices").toArray(), arrayContaining("Apple", "Blueberry", "Banana")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo (also probably gratuitous comment)