type() {
+ return TopLevelItem.class;
}
@Override
public String getDefaultMetaVariable() {
- return "JOB";
+ return "JOB"; // TODO or should we pick up default value, ITEM?
}
}
diff --git a/core/src/main/java/hudson/model/AbstractProject.java b/core/src/main/java/hudson/model/AbstractProject.java
index a1f9eb8d0e4ab09270edbfb8a69c4a3fa6f653de..9fbcfda90b407701b73bc65b9497adbf19a0068a 100644
--- a/core/src/main/java/hudson/model/AbstractProject.java
+++ b/core/src/main/java/hudson/model/AbstractProject.java
@@ -80,7 +80,6 @@ import hudson.triggers.TriggerDescriptor;
import hudson.util.AlternativeUiTextProvider;
import hudson.util.AlternativeUiTextProvider.Message;
import hudson.util.DescribableList;
-import hudson.util.EditDistance;
import hudson.util.FormValidation;
import hudson.widgets.BuildHistoryWidget;
import hudson.widgets.HistoryWidget;
@@ -2299,6 +2298,7 @@ public abstract class AbstractProject,R extends A
/**
* Finds a {@link AbstractProject} that has the name closest to the given name.
+ * @see Items#findNearest
*/
public static AbstractProject findNearest(String name) {
return findNearest(name,Hudson.getInstance());
@@ -2308,15 +2308,10 @@ public abstract class AbstractProject
,R extends A
* Finds a {@link AbstractProject} whose name (when referenced from the specified context) is closest to the given name.
*
* @since 1.419
+ * @see Items#findNearest
*/
public static AbstractProject findNearest(String name, ItemGroup context) {
- List projects = Hudson.getInstance().getAllItems(AbstractProject.class);
- String[] names = new String[projects.size()];
- for( int i=0; i REVERSE_INTEGER_COMPARATOR = new Comparator() {
diff --git a/core/src/main/java/hudson/model/Items.java b/core/src/main/java/hudson/model/Items.java
index 746a5aa4fbba066714aa45289c943be9cce23c41..5ca561beaa59cd96aee46e8cc0c95b266c918139 100644
--- a/core/src/main/java/hudson/model/Items.java
+++ b/core/src/main/java/hudson/model/Items.java
@@ -32,6 +32,7 @@ import hudson.XmlFile;
import hudson.matrix.Axis;
import hudson.triggers.Trigger;
import hudson.util.DescriptorList;
+import hudson.util.EditDistance;
import hudson.util.XStream2;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
@@ -39,6 +40,7 @@ import org.apache.commons.lang.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.*;
+import javax.annotation.CheckForNull;
/**
* Convenience methods related to {@link Item}.
@@ -279,6 +281,25 @@ public class Items {
return r;
}
+ /**
+ * Finds an item whose name (when referenced from the specified context) is closest to the given name.
+ * @param the type of item being considered
+ * @param type same as {@code T}
+ * @param name the supplied name
+ * @param context a context to start from (used to compute relative names)
+ * @return the closest available item
+ * @since 1.538
+ */
+ public static @CheckForNull T findNearest(Class type, String name, ItemGroup context) {
+ List projects = Jenkins.getInstance().getAllItems(type);
+ String[] names = new String[projects.size()];
+ for (int i = 0; i < projects.size(); i++) {
+ names[i] = projects.get(i).getRelativeNameFrom(context);
+ }
+ String nearest = EditDistance.findNearest(name, names);
+ return Jenkins.getInstance().getItem(nearest, context, type);
+ }
+
/**
* Used to load/save job configuration.
*
diff --git a/test/src/test/java/hudson/cli/GetJobCommandTest.java b/test/src/test/java/hudson/cli/GetJobCommandTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..2ecbf318ba5435d40b663fc1e3a73bedf3abebd0
--- /dev/null
+++ b/test/src/test/java/hudson/cli/GetJobCommandTest.java
@@ -0,0 +1,66 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2012 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.cli;
+
+import edu.umd.cs.findbugs.annotations.SuppressWarnings;
+import hudson.model.FreeStyleProject;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.Collections;
+import java.util.Locale;
+import org.apache.commons.io.input.NullInputStream;
+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;
+
+@SuppressWarnings("DM_DEFAULT_ENCODING")
+public class GetJobCommandTest {
+
+ @Rule public JenkinsRule j = new JenkinsRule();
+
+ @Bug(20236)
+ @Test public void withFolders() throws Exception {
+ MockFolder d = j.createFolder("d");
+ FreeStyleProject p = d.createProject(FreeStyleProject.class, "p");
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ PrintStream outS = new PrintStream(out);
+ int result = new GetJobCommand().main(Collections.singletonList("d/p"), Locale.ENGLISH, new NullInputStream(0), outS, outS);
+ outS.flush();
+ String output = out.toString();
+ assertEquals(output, 0, result);
+ assertEquals(p.getConfigFile().asString(), output);
+ out = new ByteArrayOutputStream();
+ outS = new PrintStream(out);
+ result = new GetJobCommand().main(Collections.singletonList("d"), Locale.ENGLISH, new NullInputStream(0), outS, outS);
+ outS.flush();
+ output = out.toString();
+ assertEquals(output, 0, result);
+ assertEquals(d.getConfigFile().asString(), output);
+ }
+
+}