@Override public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) { if (defaultValue instanceof StringParameterValue) { StringParameterValue value = (StringParameterValue) defaultValue; return new StringParameterDefinition(getName(), value.value, getDescription()); } else { return this; } }
@Override public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) { if (defaultValue instanceof StringParameterValue) { StringParameterValue value = (StringParameterValue) defaultValue; return new StringParameterDefinition(getName(), value.value, getDescription()); } else { return this; } }
@Override public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) { if (defaultValue instanceof StringParameterValue) { StringParameterValue value = (StringParameterValue) defaultValue; return new StringParameterDefinition(getName(), value.value, getDescription()); } else { return this; } }
@Test public void parametersSingle() throws Exception { List<ParameterDefinition> p = new ArrayList<>(); p.add(new StringParameterDefinition("SOME_STRING", "some default", "Hey, a description with a ' in it.")); ParametersDirective params = new ParametersDirective(p); assertGenerateDirective(params, "parameters {\n" + " string defaultValue: 'some default', description: 'Hey, a description with a \\' in it.', name: 'SOME_STRING'" + trimParamOrEmpty() + "\n" + "}"); }
@Test public void parametersMultiple() throws Exception { List<ParameterDefinition> p = new ArrayList<>(); p.add(new StringParameterDefinition("SOME_STRING", "some default", "Hey, a description with a ' in it.")); p.add(new BooleanParameterDefinition("SOME_BOOL", true, "This will default to true.")); ParametersDirective params = new ParametersDirective(p); assertGenerateDirective(params, "parameters {\n" + " string defaultValue: 'some default', description: 'Hey, a description with a \\' in it.', name: 'SOME_STRING'" + trimParamOrEmpty() + "\n" + " booleanParam defaultValue: true, description: 'This will default to true.', name: 'SOME_BOOL'\n" + "}"); }
@Issue("JENKINS-29739") @Test public void generateSnippetForBuildTriggerSingle() throws Exception { FreeStyleProject ds = r.jenkins.createProject(FreeStyleProject.class, "ds1"); FreeStyleProject us = r.jenkins.createProject(FreeStyleProject.class, "us1"); ds.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("key", ""))); String snippet; if (StringParameterDefinition.DescriptorImpl.class.isAnnotationPresent(Symbol.class)) { snippet = "build job: 'ds1', parameters: [string(name: 'key', value: 'stuff')]"; } else { // TODO 2.x delete snippet = "build job: 'ds1', parameters: [[$class: 'StringParameterValue', name: 'key', value: 'stuff']]"; } st.assertGenerateSnippet("{'stapler-class':'" + BuildTriggerStep.class.getName() + "', 'job':'ds1', 'parameter': {'name':'key', 'value':'stuff'}}", snippet, us.getAbsoluteUrl() + "configure"); }
@Issue("JENKINS-26093") @Test public void generateSnippetForBuildTrigger() throws Exception { MockFolder d1 = r.createFolder("d1"); FreeStyleProject ds = d1.createProject(FreeStyleProject.class, "ds"); MockFolder d2 = r.createFolder("d2"); // Really this would be a WorkflowJob, but we cannot depend on that here, and it should not matter since we are just looking for Job: FreeStyleProject us = d2.createProject(FreeStyleProject.class, "us"); ds.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("key", ""), new BooleanParameterDefinition("flag", false, ""))); String snippet; if (StringParameterDefinition.DescriptorImpl.class.isAnnotationPresent(Symbol.class)) { snippet = "build job: '../d1/ds', parameters: [string(name: 'key', value: 'stuff'), booleanParam(name: 'flag', value: true)]"; } else { // TODO 2.x delete snippet = "build job: '../d1/ds', parameters: [[$class: 'StringParameterValue', name: 'key', value: 'stuff'], [$class: 'BooleanParameterValue', name: 'flag', value: true]]"; } st.assertGenerateSnippet("{'stapler-class':'" + BuildTriggerStep.class.getName() + "', 'job':'../d1/ds', 'parameter': [{'name':'key', 'value':'stuff'}, {'name':'flag', 'value':true}]}", snippet, us.getAbsoluteUrl() + "configure"); }
@Test public void fullInput() throws Exception { InputDirective input = new InputDirective("hello"); input.setId("banana"); input.setOk("Yeah, do it"); input.setSubmitter("bob"); input.setSubmitterParameter("subParam"); List<ParameterDefinition> params = new ArrayList<>(); params.add(new StringParameterDefinition("aString", "steve", "Hey, a string")); params.add(new BooleanParameterDefinition("aBool", true, "A boolean now")); input.setParameters(params); assertGenerateDirective(input, "input {\n" + " message 'hello'\n" + " id 'banana'\n" + " ok 'Yeah, do it'\n" + " submitter 'bob'\n" + " submitterParameter 'subParam'\n" + " parameters {\n" + // StringParameterDefinition added trim field in 2.90 " string defaultValue: 'steve', description: 'Hey, a string', name: 'aString'" + trimParamOrEmpty() + "\n" + " booleanParam defaultValue: true, description: 'A boolean now', name: 'aBool'\n" + " }\n" + "}"); }
@Issue("JENKINS-42367") @Test public void nullValue() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("echo(/TEXT=${params.TEXT}/)",true)); p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("TEXT", ""))); r.assertLogContains("TEXT=null", r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("TEXT", /* not possible via UI, but to simulate other ParameterValue impls */null))))); }
@Override public void evaluate() throws Throwable { WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p"); p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("param", ""))); p.setDefinition(new CpsFlowDefinition("echo \"run with ${param}\"", true)); WorkflowRun b1 = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("param", "some value")))); story.j.assertLogContains("run with some value", b1); // When we replay a parameterized build, we expect the original parameter values to be set. WorkflowRun b2 = (WorkflowRun) b1.getAction(ReplayAction.class).run("echo \"run again with ${param}\"", Collections.<String,String>emptyMap()).get(); story.j.assertLogContains("run again with some value", story.j.assertBuildStatusSuccess(b2)); } });
/** * Tests the schedule method of GerritTrigger. * It verifies that * {@link hudson.model.AbstractProject#scheduleBuild2(int, hudson.model.Cause, hudson.model.Action...)} * gets called with correct parameters when there are some default parameters present. */ @Test public void testScheduleWithDefaultParameters() { Queue queue = mockJenkinsQueue(); List<ParameterDefinition> list = new LinkedList<ParameterDefinition>(); list.add(new StringParameterDefinition("MOCK_PARAM", "mock_value")); AbstractProject project = mockProject(list); mockPluginConfig(0); PatchsetCreated event = Setup.createPatchsetCreated(); final GerritCause gerritCause = spy(new GerritCause(event, true)); GerritTrigger trigger = Setup.createDefaultTrigger(project); Setup.setTrigger(trigger, project); doReturn("http://mock.url").when(gerritCause).getUrl(); trigger.schedule(gerritCause, event); verify(queue).schedule2(same(project), eq(0), hasAllActions( hasCauseActionContainingCauseMatcher(gerritCause), hasParamActionMatcher("MOCK_PARAM", "mock_value"), hasParamActionMatcher(GERRIT_CHANGE_URL, "http://mock.url") )); }
@Issue("JENKINS-44621") @Test public void externalParamsNotRemoved() throws Exception { WorkflowRun b = getAndStartNonRepoBuild("simpleParameters"); j.assertBuildStatusSuccess(j.waitForCompletion(b)); WorkflowJob job = b.getParent(); ParametersDefinitionProperty paramProp = job.getProperty(ParametersDefinitionProperty.class); assertNotNull(paramProp); assertEquals(1, paramProp.getParameterDefinitions().size()); List<ParameterDefinition> newParams = new ArrayList<>(); newParams.addAll(paramProp.getParameterDefinitions()); newParams.add(new StringParameterDefinition("DO_NOT_DELETE", "something")); job.removeProperty(paramProp); job.addProperty(new ParametersDefinitionProperty(newParams)); job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true)); j.buildAndAssertSuccess(job); ParametersDefinitionProperty newProp = job.getProperty(ParametersDefinitionProperty.class); assertNotNull(newProp); assertEquals(1, newProp.getParameterDefinitions().size()); ParameterDefinition paramDef = newProp.getParameterDefinition("DO_NOT_DELETE"); assertNotNull(paramDef); } }
@Issue("JENKINS-42836") @Test public void usingParameterInScriptPath() throws Exception { sampleRepo.init(); sampleRepo.write("flow.groovy", "echo 'version one'"); sampleRepo.git("add", "flow.groovy"); sampleRepo.write("otherFlow.groovy", "echo 'version two'"); sampleRepo.git("add", "otherFlow.groovy"); sampleRepo.git("commit", "--all", "--message=commits"); WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); CpsScmFlowDefinition def = new CpsScmFlowDefinition(new GitSCM(Collections.singletonList(new UserRemoteConfig(sampleRepo.fileUrl(), null, null, null)), Collections.singletonList(new BranchSpec("master")), false, Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList()), "${SCRIPT_PATH}"); p.setDefinition(def); p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("SCRIPT_PATH", "flow.groovy"))); r.assertLogContains("version one", r.assertBuildStatusSuccess(p.scheduleBuild2(0))); r.assertLogContains("version two", r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("SCRIPT_PATH", "otherFlow.groovy"))))); } }
@Bug(4271) @Test public void parameterizedBuildsBranch() throws Exception { m.hg(repo, "init"); m.touchAndCommit(repo, "trunk"); m.hg(repo, "update", "null"); m.hg(repo, "branch", "b"); m.touchAndCommit(repo, "variant"); FreeStyleProject p = j.createFreeStyleProject(); p.setScm(new MercurialSCM(hgInstallation(), repo.getPath(), "${BRANCH}", null, null, null, false)); // SECURITY-170 - have to use ParametersDefinitionProperty p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("BRANCH", "b"))); String log = m.buildAndCheck(p, "variant", new ParametersAction( new StringParameterValue("BRANCH", "b"))); assertTrue(log, log.contains("--rev b")); assertFalse(log, log.contains("--rev ${BRANCH}")); m.touchAndCommit(repo, "further-variant"); // the following assertion commented out as a part of the fix to // HUDSON-6126 // assertTrue(pollSCMChanges(p)); m.buildAndCheck(p, "further-variant", new ParametersAction( new StringParameterValue("BRANCH", "b"))); }
@Issue("JENKINS-28447") @Test public void usingParameter() throws Exception { sampleRepo.init(); sampleRepo.write("flow.groovy", "echo 'version one'"); sampleRepo.git("add", "flow.groovy"); sampleRepo.git("commit", "--message=one"); sampleRepo.git("tag", "one"); sampleRepo.write("flow.groovy", "echo 'version two'"); sampleRepo.git("commit", "--all", "--message=two"); WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); CpsScmFlowDefinition def = new CpsScmFlowDefinition(new GitSCM(Collections.singletonList(new UserRemoteConfig(sampleRepo.fileUrl(), null, null, null)), Collections.singletonList(new BranchSpec("${VERSION}")), false, Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList()), "flow.groovy"); def.setLightweight(false); // TODO SCMFileSystem.of cannot pick up build parameters p.setDefinition(def); p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("VERSION", "master"))); r.assertLogContains("version two", r.assertBuildStatusSuccess(p.scheduleBuild2(0))); r.assertLogContains("version one", r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("VERSION", "one"))))); }
@Bug(9686) @Test public void pollingExpandsParameterDefaults() throws Exception { m.hg(repo, "init"); m.touchAndCommit(repo, "trunk"); m.hg(repo, "update", "null"); m.hg(repo, "branch", "b"); m.touchAndCommit(repo, "variant"); FreeStyleProject p = j.createFreeStyleProject(); p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("branch", "default"))); p.setScm(new MercurialSCM(hgInstallation(), repo.getPath(), "${branch}", null, null, null, false)); String log = m.buildAndCheck(p, "trunk", new ParametersAction(new StringParameterValue("branch", "default"))); assertTrue(log, log.contains("--rev default")); /* TODO cannot behave sensibly when workspace contains a branch build because the *current* trunk revision will be seen as new; would need to compare to all historical build records, or keep a separate workspace per branch: log = m.buildAndCheck(p, "variant", new ParametersAction(new StringParameterValue("branch", "b"))); assertTrue(log, log.contains("--rev b")); */ assertEquals(PollingResult.Change.NONE, m.pollSCMChanges(p).change); m.hg(repo, "update", "default"); m.touchAndCommit(repo, "trunk2"); assertEquals(PollingResult.Change.SIGNIFICANT, m.pollSCMChanges(p).change); }
@Issue("JENKINS-27295") @Test public void smokes() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("echo(/TEXT=${params.TEXT} FLAG=${params.FLAG ? 'yes' : 'no'} PASS=${params.PASS}/)",true)); p.addProperty(new ParametersDefinitionProperty( new StringParameterDefinition("TEXT", ""), new BooleanParameterDefinition("FLAG", false, null), new PasswordParameterDefinition("PASS", "", null))); WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction( new StringParameterValue("TEXT", "hello"), new BooleanParameterValue("FLAG", true), new PasswordParameterValue("PASS", "s3cr3t")))); r.assertLogContains("TEXT=hello", b); r.assertLogContains("FLAG=yes", b); r.assertLogContains("PASS=s3cr3t", b); }
@Issue("JENKINS-22568") @Test @Ignore("TODO use docker-fixtures for a reproducible test case") public void pollingWithDefaultParametersWithCurlyBraces() throws Exception { FreeStyleProject p = r.createFreeStyleProject(); String repo = "https://svn.jenkins-ci.org/"; String path = "trunk/hudson/test-projects/trivial-ant/"; p.setScm(new SubversionSCM("${REPO}" + path)); ParametersDefinitionProperty property = new ParametersDefinitionProperty(new StringParameterDefinition("REPO", repo)); p.addProperty(property); FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause(), new ParametersAction(new StringParameterValue("REPO", repo))).get(); r.assertBuildStatus(Result.SUCCESS,b); assertTrue(b.getWorkspace().child("build.xml").exists()); // as a baseline, this shouldn't detect any change TaskListener listener = r.createTaskListener(); PollingResult poll = p.poll(listener); assertFalse("Polling shouldn't have any changes.", poll.hasChanges()); }
@Issue("JENKINS-22568") @Test @Ignore("TODO use docker-fixtures for a reproducible test case") public void pollingWithDefaultParametersWithOutCurlyBraces() throws Exception { FreeStyleProject p = r.createFreeStyleProject(); String repo = "https://svn.jenkins-ci.org"; String path = "/trunk/hudson/test-projects/trivial-ant/"; p.setScm(new SubversionSCM("$REPO" + path)); ParametersDefinitionProperty property = new ParametersDefinitionProperty(new StringParameterDefinition("REPO", repo)); p.addProperty(property); FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause(), new ParametersAction(new StringParameterValue("REPO", repo))).get(); r.assertBuildStatus(Result.SUCCESS,b); assertTrue(b.getWorkspace().child("build.xml").exists()); // as a baseline, this shouldn't detect any change TaskListener listener = r.createTaskListener(); PollingResult poll = p.poll(listener); assertFalse("Polling shouldn't have any changes.", poll.hasChanges()); }
/** * Creates a new freestyle project and builds the project with a string * parameter. If the build is succesful, a rebuild of the last build is * done. The rebuild on the project level should point to the last build * * @throws Exception * Exception */ public void testWhenProjectWithParamsThenRebuildProjectExecutesRebuildOfLastBuild() throws Exception { FreeStyleProject project = createFreeStyleProject(); project.addProperty(new ParametersDefinitionProperty( new StringParameterDefinition("name", "defaultValue"))); // Build (#1) project.scheduleBuild2(0, new Cause.UserIdCause(), new ParametersAction(new StringParameterValue("name", "test"))) .get(); HtmlPage rebuildConfigPage = createWebClient().getPage(project, "1/rebuild"); // Rebuild (#2) submit(rebuildConfigPage.getFormByName("config")); HtmlPage projectPage = createWebClient().getPage(project); WebAssert.assertLinkPresentWithText(projectPage, "Rebuild Last"); HtmlAnchor rebuildHref = projectPage.getAnchorByText("Rebuild Last"); assertEquals("Rebuild Last should point to the second build", "/" + project.getUrl() + "lastCompletedBuild/rebuild", rebuildHref.getHrefAttribute()); }