Skip to content

Commit

Permalink
[FIXED JENKINS-16462] Run parameters do not support folders.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jan 24, 2013
1 parent 1979302 commit de9002b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 10 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -58,6 +58,9 @@
<li class=bug>
Bogus “Build Record Root Directory” inadequately diagnosed.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-16457">issue 16457</a>)
<li class=bug>
Run parameters do not support folders.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-16462">issue 16462</a>)
</ul>
</div><!--=TRUNK-END=-->

Expand Down
Expand Up @@ -36,6 +36,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.CheckForNull;

/**
* Data representation of the auto-completion candidates.
Expand Down Expand Up @@ -86,7 +87,7 @@ public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object o)
* @param container
* The nearby contextual {@link ItemGroup} to resolve relative job names from.
*/
public static <T extends Item> AutoCompletionCandidates ofJobNames(final Class<T> type, final String value, Item self, ItemGroup container) {
public static <T extends Item> AutoCompletionCandidates ofJobNames(final Class<T> type, final String value, @CheckForNull Item self, ItemGroup container) {
if (self==container)
container = self.getParent();

Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/hudson/model/Run.java
Expand Up @@ -101,7 +101,6 @@
import jenkins.model.JenkinsLocationConfiguration;
import jenkins.util.io.OnMaster;
import net.sf.json.JSONObject;
import org.apache.commons.io.input.NullInputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.jelly.XMLOutput;
import org.kohsuke.accmod.Restricted;
Expand Down Expand Up @@ -2028,7 +2027,7 @@ public final EnvVars getCharacteristicEnvVars() {
}

public String getExternalizableId() {
return project.getName() + "#" + getNumber();
return project.getFullName() + "#" + getNumber();
}

public static Run<?,?> fromExternalizableId(String id) {
Expand All @@ -2039,7 +2038,7 @@ public static Run<?,?> fromExternalizableId(String id) {
String jobName = id.substring(0, hash);
int number = Integer.parseInt(id.substring(hash + 1));

Job<?,?> job = (Job<?,?>) Jenkins.getInstance().getItem(jobName);
Job<?,?> job = Jenkins.getInstance().getItemByFullName(jobName, Job.class);
return job.getBuildByNumber(number);
}

Expand Down
8 changes: 7 additions & 1 deletion core/src/main/java/hudson/model/RunParameterDefinition.java
Expand Up @@ -30,6 +30,7 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import hudson.Extension;
import org.kohsuke.stapler.QueryParameter;

public class RunParameterDefinition extends SimpleParameterDefinition {

Expand Down Expand Up @@ -65,7 +66,7 @@ public String getProjectName() {
}

public Job getProject() {
return (Job) Jenkins.getInstance().getItem(projectName);
return Jenkins.getInstance().getItemByFullName(projectName, Job.class);
}

@Extension
Expand All @@ -84,6 +85,11 @@ public String getHelpFile() {
public ParameterDefinition newInstance(StaplerRequest req, JSONObject formData) throws FormException {
return req.bindJSON(RunParameterDefinition.class, formData);
}

public AutoCompletionCandidates doAutoCompleteProjectName(@QueryParameter String value) {
return AutoCompletionCandidates.ofJobNames(Job.class, value, null, Jenkins.getInstance());
}

}

@Override
Expand Down
Expand Up @@ -30,11 +30,7 @@ THE SOFTWARE.
<f:textbox name="parameter.name" value="${instance.name}" />
</f:entry>
<f:entry title="${%Project}" help="/help/parameter/run-project.html">
<select name="parameter.projectName">
<j:forEach var="project" items="${app.items}">
<f:option selected="${instance.project==project}">${project.displayName}</f:option>
</j:forEach>
</select>
<f:textbox name="parameter.projectName" value="${instance.projectName}" autoCompleteField="projectName"/>
</f:entry>
<f:entry title="${%Description}" help="/help/parameter/description.html">
<f:textarea name="parameter.description" value="${instance.description}" />
Expand Down
66 changes: 66 additions & 0 deletions test/src/test/java/hudson/model/RunParameterDefinitionTest.java
@@ -0,0 +1,66 @@
/*
* The MIT License
*
* Copyright 2013 Jesse Glick.
*
* 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 hudson.model;

import hudson.EnvVars;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockFolder;

public class RunParameterDefinitionTest {

@Rule public JenkinsRule j = new JenkinsRule();

@Bug(16462)
@Test public void inFolders() throws Exception {
MockFolder dir = j.createFolder("dir");
MockFolder subdir = dir.createProject(MockFolder.class, "sub dir");
FreeStyleProject p = subdir.createProject(FreeStyleProject.class, "some project");
p.scheduleBuild2(0).get();
FreeStyleBuild build2 = p.scheduleBuild2(0).get();
p.scheduleBuild2(0).get();
String id = build2.getExternalizableId();
assertEquals("dir/sub dir/some project#2", id);
assertEquals(build2, Run.fromExternalizableId(id));
RunParameterDefinition def = new RunParameterDefinition("build", "dir/sub dir/some project", "my build");
assertEquals("dir/sub dir/some project", def.getProjectName());
assertEquals(p, def.getProject());
EnvVars env = new EnvVars();
def.getDefaultParameterValue().buildEnvVars(null, env);
assertEquals(j.jenkins.getRootUrl() + "job/dir/job/sub%20dir/job/some%20project/3/", env.get("build"));
RunParameterValue val = def.createValue(id);
assertEquals(build2, val.getRun());
assertEquals("dir/sub dir/some project", val.getJobName());
assertEquals("2", val.getNumber());
val.buildEnvVars(null, env);
assertEquals(j.jenkins.getRootUrl() + "job/dir/job/sub%20dir/job/some%20project/2/", env.get("build"));
assertEquals("dir/sub dir/some project", env.get("build.jobName"));
assertEquals("2", env.get("build.number"));
}

}

0 comments on commit de9002b

Please sign in to comment.