Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
roginluo
Jenkins
提交
d0c45040
J
Jenkins
项目概览
roginluo
/
Jenkins
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d0c45040
编写于
10月 24, 2013
作者:
J
Jesse Glick
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Get/UpdateJobCommand should work with the most generic possible type—AbstractItem.
上级
304b2c07
变更
10
显示空白变更内容
内联
并排
Showing
10 changed file
with
227 addition
and
57 deletion
+227
-57
changelog.html
changelog.html
+3
-0
core/src/main/java/hudson/cli/GetJobCommand.java
core/src/main/java/hudson/cli/GetJobCommand.java
+2
-2
core/src/main/java/hudson/cli/UpdateJobCommand.java
core/src/main/java/hudson/cli/UpdateJobCommand.java
+2
-2
core/src/main/java/hudson/cli/handlers/AbstractItemOptionHandler.java
...n/java/hudson/cli/handlers/AbstractItemOptionHandler.java
+48
-0
core/src/main/java/hudson/cli/handlers/AbstractProjectOptionHandler.java
...ava/hudson/cli/handlers/AbstractProjectOptionHandler.java
+5
-21
core/src/main/java/hudson/cli/handlers/GenericItemOptionHandler.java
...in/java/hudson/cli/handlers/GenericItemOptionHandler.java
+72
-0
core/src/main/java/hudson/cli/handlers/TopLevelItemOptionHandler.java
...n/java/hudson/cli/handlers/TopLevelItemOptionHandler.java
+5
-24
core/src/main/java/hudson/model/AbstractProject.java
core/src/main/java/hudson/model/AbstractProject.java
+3
-8
core/src/main/java/hudson/model/Items.java
core/src/main/java/hudson/model/Items.java
+21
-0
test/src/test/java/hudson/cli/GetJobCommandTest.java
test/src/test/java/hudson/cli/GetJobCommandTest.java
+66
-0
未找到文件。
changelog.html
浏览文件 @
d0c45040
...
...
@@ -61,6 +61,9 @@ Upcoming changes</a>
<li
class=
bug
>
Pass full list of all possible jobs to ViewJobFilter when recurse option is set
(
<a
href=
"https://issues.jenkins-ci.org/browse/JENKINS-20143"
>
issue 20143
</a>
)
<li
class=
rfe
>
<code>
get-job
</code>
and
<code>
update-job
</code>
CLI commands can now work with folders, or indeed any
<code>
AbstractItem
</code>
.
(
<a
href=
"https://issues.jenkins-ci.org/browse/JENKINS-20236"
>
issue 20236
</a>
)
<li
class=
rfe
>
Added API allowing plugins to hide entries from the context menu even while they appear in the sidepanel.
(
<a
href=
"https://issues.jenkins-ci.org/browse/JENKINS-19173"
>
issue 19173
</a>
)
...
...
core/src/main/java/hudson/cli/GetJobCommand.java
浏览文件 @
d0c45040
...
...
@@ -24,7 +24,7 @@
package
hudson.cli
;
import
hudson.Extension
;
import
hudson.model.Abstract
Project
;
import
hudson.model.Abstract
Item
;
import
hudson.model.Item
;
import
hudson.util.IOUtils
;
import
org.kohsuke.args4j.Argument
;
...
...
@@ -35,7 +35,7 @@ import org.kohsuke.args4j.Argument;
@Extension
public
class
GetJobCommand
extends
CLICommand
{
@Argument
(
metaVar
=
"JOB"
,
usage
=
"Name of the job"
,
required
=
true
)
public
Abstract
Project
<?,?>
job
;
public
Abstract
Item
job
;
@Override
public
String
getShortDescription
()
{
...
...
core/src/main/java/hudson/cli/UpdateJobCommand.java
浏览文件 @
d0c45040
...
...
@@ -24,7 +24,7 @@
package
hudson.cli
;
import
hudson.Extension
;
import
hudson.model.Abstract
Project
;
import
hudson.model.Abstract
Item
;
import
org.kohsuke.args4j.Argument
;
import
javax.xml.transform.Source
;
...
...
@@ -36,7 +36,7 @@ import javax.xml.transform.stream.StreamSource;
@Extension
public
class
UpdateJobCommand
extends
CLICommand
{
@Argument
(
metaVar
=
"JOB"
,
usage
=
"Name of the job"
,
required
=
true
)
public
Abstract
Project
<?,?>
job
;
public
Abstract
Item
job
;
@Override
public
String
getShortDescription
()
{
...
...
core/src/main/java/hudson/cli/handlers/AbstractItemOptionHandler.java
0 → 100644
浏览文件 @
d0c45040
/*
* 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.cli.handlers
;
import
hudson.model.AbstractItem
;
import
org.kohsuke.MetaInfServices
;
import
org.kohsuke.args4j.CmdLineParser
;
import
org.kohsuke.args4j.OptionDef
;
import
org.kohsuke.args4j.spi.OptionHandler
;
import
org.kohsuke.args4j.spi.Setter
;
/**
* Refers to an {@link AbstractItem} by name.
* @since 1.538
*/
@MetaInfServices
(
OptionHandler
.
class
)
public
class
AbstractItemOptionHandler
extends
GenericItemOptionHandler
<
AbstractItem
>
{
public
AbstractItemOptionHandler
(
CmdLineParser
parser
,
OptionDef
option
,
Setter
<
AbstractItem
>
setter
)
{
super
(
parser
,
option
,
setter
);
}
@Override
protected
Class
<
AbstractItem
>
type
()
{
return
AbstractItem
.
class
;
}
}
core/src/main/java/hudson/cli/handlers/AbstractProjectOptionHandler.java
浏览文件 @
d0c45040
...
...
@@ -24,42 +24,26 @@
package
hudson.cli.handlers
;
import
hudson.model.AbstractProject
;
import
jenkins.model.Jenkins
;
import
org.kohsuke.args4j.CmdLineException
;
import
org.kohsuke.args4j.CmdLineParser
;
import
org.kohsuke.args4j.OptionDef
;
import
org.kohsuke.args4j.spi.OptionHandler
;
import
org.kohsuke.args4j.spi.Parameters
;
import
org.kohsuke.args4j.spi.Setter
;
import
org.kohsuke.MetaInfServices
;
import
org.kohsuke.args4j.spi.OptionHandler
;
/**
* Refer to {@link AbstractProject} by its name.
*
* @author Kohsuke Kawaguchi
*/
@MetaInfServices
@MetaInfServices
(
OptionHandler
.
class
)
@SuppressWarnings
(
"rawtypes"
)
public
class
AbstractProjectOptionHandler
extends
OptionHandler
<
AbstractProject
>
{
public
class
AbstractProjectOptionHandler
extends
GenericItem
OptionHandler
<
AbstractProject
>
{
public
AbstractProjectOptionHandler
(
CmdLineParser
parser
,
OptionDef
option
,
Setter
<
AbstractProject
>
setter
)
{
super
(
parser
,
option
,
setter
);
}
@Override
public
int
parseArguments
(
Parameters
params
)
throws
CmdLineException
{
Jenkins
h
=
Jenkins
.
getInstance
();
String
src
=
params
.
getParameter
(
0
);
AbstractProject
s
=
h
.
getItemByFullName
(
src
,
AbstractProject
.
class
);
if
(
s
==
null
)
{
AbstractProject
nearest
=
AbstractProject
.
findNearest
(
src
);
if
(
nearest
!=
null
)
throw
new
CmdLineException
(
owner
,
"No such job '"
+
src
+
"' perhaps you meant '"
+
nearest
.
getFullName
()
+
"'?"
);
else
throw
new
CmdLineException
(
owner
,
"No such job '"
+
src
+
"'"
);
}
setter
.
addValue
(
s
);
return
1
;
@Override
protected
Class
<
AbstractProject
>
type
()
{
return
AbstractProject
.
class
;
}
@Override
...
...
core/src/main/java/hudson/cli/handlers/GenericItemOptionHandler.java
0 → 100644
浏览文件 @
d0c45040
/*
* 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.cli.handlers
;
import
hudson.model.Item
;
import
hudson.model.Items
;
import
jenkins.model.Jenkins
;
import
org.kohsuke.args4j.CmdLineException
;
import
org.kohsuke.args4j.CmdLineParser
;
import
org.kohsuke.args4j.OptionDef
;
import
org.kohsuke.args4j.spi.OptionHandler
;
import
org.kohsuke.args4j.spi.Parameters
;
import
org.kohsuke.args4j.spi.Setter
;
/**
* Refers to an {@link Item} by its name.
* May be subclassed to handle specific kinds of items.
* (Use {@code @MetaInfServices(OptionHandler.class)} to register the subclass.)
* @param <T> the kind of item being handled
* @since 1.538
*/
public
abstract
class
GenericItemOptionHandler
<
T
extends
Item
>
extends
OptionHandler
<
T
>
{
protected
GenericItemOptionHandler
(
CmdLineParser
parser
,
OptionDef
option
,
Setter
<
T
>
setter
)
{
super
(
parser
,
option
,
setter
);
}
protected
abstract
Class
<
T
>
type
();
@Override
public
int
parseArguments
(
Parameters
params
)
throws
CmdLineException
{
Jenkins
j
=
Jenkins
.
getInstance
();
String
src
=
params
.
getParameter
(
0
);
T
s
=
j
.
getItemByFullName
(
src
,
type
());
if
(
s
==
null
)
{
T
nearest
=
Items
.
findNearest
(
type
(),
src
,
j
);
if
(
nearest
!=
null
)
{
throw
new
CmdLineException
(
owner
,
"No such job '"
+
src
+
"'; perhaps you meant '"
+
nearest
.
getFullName
()
+
"'?"
);
}
else
{
throw
new
CmdLineException
(
owner
,
"No such job '"
+
src
+
"'"
);
}
}
setter
.
addValue
(
s
);
return
1
;
}
@Override
public
String
getDefaultMetaVariable
()
{
return
"ITEM"
;
}
}
core/src/main/java/hudson/cli/handlers/TopLevelItemOptionHandler.java
浏览文件 @
d0c45040
package
hudson.cli.handlers
;
import
hudson.model.AbstractProject
;
import
jenkins.model.Jenkins
;
import
hudson.model.TopLevelItem
;
import
org.kohsuke.MetaInfServices
;
import
org.kohsuke.args4j.CmdLineException
;
import
org.kohsuke.args4j.CmdLineParser
;
import
org.kohsuke.args4j.OptionDef
;
import
org.kohsuke.args4j.spi.OptionHandler
;
import
org.kohsuke.args4j.spi.Parameters
;
import
org.kohsuke.args4j.spi.Setter
;
/**
...
...
@@ -16,33 +12,18 @@ import org.kohsuke.args4j.spi.Setter;
*
* @author Kohsuke Kawaguchi
*/
@MetaInfServices
public
class
TopLevelItemOptionHandler
extends
OptionHandler
<
TopLevelItem
>
{
@MetaInfServices
(
OptionHandler
.
class
)
public
class
TopLevelItemOptionHandler
extends
GenericItem
OptionHandler
<
TopLevelItem
>
{
public
TopLevelItemOptionHandler
(
CmdLineParser
parser
,
OptionDef
option
,
Setter
<
TopLevelItem
>
setter
)
{
super
(
parser
,
option
,
setter
);
}
@Override
@SuppressWarnings
(
"rawtypes"
)
public
int
parseArguments
(
Parameters
params
)
throws
CmdLineException
{
Jenkins
h
=
Jenkins
.
getInstance
();
String
src
=
params
.
getParameter
(
0
);
TopLevelItem
s
=
h
.
getItemByFullName
(
src
,
TopLevelItem
.
class
);
if
(
s
==
null
)
{
AbstractProject
nearest
=
AbstractProject
.
findNearest
(
src
);
if
(
nearest
!=
null
)
throw
new
CmdLineException
(
owner
,
"No such job '"
+
src
+
"' perhaps you meant '"
+
nearest
.
getFullName
()
+
"'?"
);
else
throw
new
CmdLineException
(
owner
,
"No such job '"
+
src
+
"'"
);
}
setter
.
addValue
(
s
);
return
1
;
@Override
protected
Class
<
TopLevelItem
>
type
()
{
return
TopLevelItem
.
class
;
}
@Override
public
String
getDefaultMetaVariable
()
{
return
"JOB"
;
return
"JOB"
;
// TODO or should we pick up default value, ITEM?
}
}
core/src/main/java/hudson/model/AbstractProject.java
浏览文件 @
d0c45040
...
...
@@ -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<P extends AbstractProject<P,R>,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<P extends AbstractProject<P,R>,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
<
AbstractProject
>
projects
=
Hudson
.
getInstance
().
getAllItems
(
AbstractProject
.
class
);
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
(
AbstractProject
)
Jenkins
.
getInstance
().
getItem
(
nearest
,
context
);
return
Items
.
findNearest
(
AbstractProject
.
class
,
name
,
context
);
}
private
static
final
Comparator
<
Integer
>
REVERSE_INTEGER_COMPARATOR
=
new
Comparator
<
Integer
>()
{
...
...
core/src/main/java/hudson/model/Items.java
浏览文件 @
d0c45040
...
...
@@ -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 <T> 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
extends
Item
>
T
findNearest
(
Class
<
T
>
type
,
String
name
,
ItemGroup
context
)
{
List
<
T
>
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.
*
...
...
test/src/test/java/hudson/cli/GetJobCommandTest.java
0 → 100644
浏览文件 @
d0c45040
/*
* 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
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录