Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
LinuxSuRen
jenkins
提交
18833611
J
jenkins
项目概览
LinuxSuRen
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
18833611
编写于
9月 23, 2015
作者:
I
Ing. Pavel Janousek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[JENKINS-30521] reload-job CLI command moved from Core to CLI
上级
7fb4696a
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
353 addition
and
5 deletion
+353
-5
core/src/main/java/hudson/cli/ReloadJobCommand.java
core/src/main/java/hudson/cli/ReloadJobCommand.java
+120
-0
core/src/main/java/hudson/model/AbstractItem.java
core/src/main/java/hudson/model/AbstractItem.java
+0
-1
core/src/main/resources/hudson/cli/Messages.properties
core/src/main/resources/hudson/cli/Messages.properties
+2
-0
core/src/main/resources/hudson/cli/Messages_pt_BR.properties
core/src/main/resources/hudson/cli/Messages_pt_BR.properties
+4
-1
core/src/main/resources/hudson/model/Messages.properties
core/src/main/resources/hudson/model/Messages.properties
+0
-1
core/src/main/resources/hudson/model/Messages_pt_BR.properties
...src/main/resources/hudson/model/Messages_pt_BR.properties
+0
-2
test/src/test/java/hudson/cli/ReloadJobCommandTest.java
test/src/test/java/hudson/cli/ReloadJobCommandTest.java
+227
-0
未找到文件。
core/src/main/java/hudson/cli/ReloadJobCommand.java
0 → 100644
浏览文件 @
18833611
/*
* The MIT License
*
* Copyright (c) 2015 Red Hat, Inc.
*
* 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
hudson.Extension
;
import
hudson.cli.handlers.ViewOptionHandler
;
import
hudson.model.AbstractItem
;
import
hudson.model.AbstractProject
;
import
hudson.model.Item
;
import
hudson.model.ViewGroup
;
import
hudson.model.View
;
import
jenkins.model.Jenkins
;
import
org.kohsuke.args4j.Argument
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
/**
* @author pjanouse
* @since TODO
*/
@Extension
public
class
ReloadJobCommand
extends
CLICommand
{
@Argument
(
usage
=
"Name of the job(s) to reload"
,
required
=
true
,
multiValued
=
true
)
private
List
<
String
>
jobs
;
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
ReloadJobCommand
.
class
.
getName
());
@Override
public
String
getShortDescription
()
{
return
Messages
.
ReloadJobCommand_ShortDescription
();
}
@Override
protected
int
run
()
throws
Exception
{
boolean
errorOccurred
=
false
;
final
Jenkins
jenkins
=
Jenkins
.
getInstance
();
if
(
jenkins
==
null
)
{
stderr
.
println
(
"The Jenkins instance has not been started, or was already shut down!"
);
return
-
1
;
}
final
HashSet
<
String
>
hs
=
new
HashSet
<
String
>();
hs
.
addAll
(
jobs
);
for
(
String
job_s:
hs
)
{
AbstractItem
job
=
null
;
try
{
// TODO: JENKINS-30786
Item
item
=
jenkins
.
getItemByFullName
(
job_s
);
if
(
item
instanceof
AbstractItem
)
{
job
=
(
AbstractItem
)
item
;
}
else
if
(
item
!=
null
)
{
LOGGER
.
log
(
Level
.
WARNING
,
"Unsupported item type: {0}"
,
item
.
getClass
().
getName
());
}
if
(
job
==
null
)
{
// TODO: JENKINS-30785
AbstractProject
project
=
AbstractProject
.
findNearest
(
job_s
);
if
(
project
==
null
)
{
stderr
.
format
(
"No such job \u2018%s\u2019 exists.\n"
,
job_s
);
}
else
{
stderr
.
format
(
"No such job \u2018%s\u2019 exists. Perhaps you meant \u2018%s\u2019?"
,
job_s
,
project
.
getFullName
());
}
errorOccurred
=
true
;
continue
;
}
try
{
job
.
checkPermission
(
AbstractItem
.
CONFIGURE
);
}
catch
(
Exception
e
)
{
stderr
.
println
(
e
.
getMessage
());
errorOccurred
=
true
;
continue
;
}
job
.
doReload
();
}
catch
(
Exception
e
)
{
final
String
errorMsg
=
String
.
format
(
"Unexpected exception occurred during reloading of job '%s': %s"
,
job
==
null
?
"(null)"
:
job
.
getFullName
(),
e
.
getMessage
());
stderr
.
println
(
errorMsg
);
LOGGER
.
warning
(
errorMsg
);
errorOccurred
=
true
;
//noinspection UnnecessaryContinue
continue
;
}
}
return
errorOccurred
?
1
:
0
;
}
}
core/src/main/java/hudson/model/AbstractItem.java
浏览文件 @
18833611
...
...
@@ -680,7 +680,6 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet
*
* @since 1.556
*/
@CLIMethod
(
name
=
"reload-job"
)
@RequirePOST
public
void
doReload
()
throws
IOException
{
checkPermission
(
CONFIGURE
);
...
...
core/src/main/resources/hudson/cli/Messages.properties
浏览文件 @
18833611
...
...
@@ -83,3 +83,5 @@ Cannot build {0} for unknown reasons.
DeleteNodeCommand.ShortDescription
=
Deletes node(s)
ReloadJobCommand.ShortDescription
=
Reload job(s)
core/src/main/resources/hudson/cli/Messages_pt_BR.properties
浏览文件 @
18833611
...
...
@@ -114,4 +114,7 @@ InstallPluginCommand.ShortDescription=Instala um plugin a partir de um arquivo,
# Deletes a node
CLI.delete-node.shortDescription
=
Remover o n
\u
00f3
# Deletes a job
DeleteJobCommand.ShortDescription
=
Remover uma job
DeleteJobCommand.ShortDescription
=
Remover uma job(s)
ReloadJobCommand.ShortDescription
=
\
Recarrega job(s) do disco.
core/src/main/resources/hudson/model/Messages.properties
浏览文件 @
18833611
...
...
@@ -91,7 +91,6 @@ BallColor.Unstable=Unstable
Build.post_build_steps_failed
=
Post-build steps failed
CLI.clear-queue.shortDescription
=
Clears the build queue.
CLI.reload-job.shortDescription
=
Reloads this job from disk.
CLI.disable-job.shortDescription
=
Disables a job.
CLI.enable-job.shortDescription
=
Enables a job.
CLI.disconnect-node.shortDescription
=
Disconnects from a node.
...
...
core/src/main/resources/hudson/model/Messages_pt_BR.properties
浏览文件 @
18833611
...
...
@@ -477,8 +477,6 @@ AbstractProject.WipeOutPermission.Description=Esta permiss\u00e3o deixa que seja
ChoiceParameterDefinition.MissingChoices
=
Exige escolhas.
# This permission allows users to run jobs as them on slaves.
Computer.BuildPermission.Description
=
Esta permiss
\u
00e3o deixa que usu
\u
00e1rios executem jobs em slaves.
# Reloads this job from disk.
CLI.reload-job.shortDescription
=
Recarrega este job do disco.
# No such slave "{0}" exists. Did you mean "{1}"?
Computer.NoSuchSlaveExists
=
N
\u
00e3o existe o slave {0}. Voc
\u
00ea quis dizer "{1}"?
# Aborted
...
...
test/src/test/java/hudson/cli/ReloadJobCommandTest.java
0 → 100644
浏览文件 @
18833611
/*
* The MIT License
*
* Copyright 2015 Red Hat, Inc.
*
* 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
hudson.FilePath
;
import
hudson.model.FreeStyleProject
;
import
hudson.model.Job
;
import
hudson.tasks.Shell
;
import
jenkins.model.Jenkins
;
import
org.junit.Before
;
import
org.junit.Ignore
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.jvnet.hudson.test.JenkinsRule
;
import
java.io.File
;
import
static
hudson
.
cli
.
CLICommandInvoker
.
Matcher
.
failedWith
;
import
static
hudson
.
cli
.
CLICommandInvoker
.
Matcher
.
hasNoStandardOutput
;
import
static
hudson
.
cli
.
CLICommandInvoker
.
Matcher
.
succeededSilently
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
/**
* @author pjanouse
*/
public
class
ReloadJobCommandTest
{
private
CLICommandInvoker
command
;
@Rule
public
final
JenkinsRule
j
=
new
JenkinsRule
();
@Before
public
void
setUp
()
{
command
=
new
CLICommandInvoker
(
j
,
"reload-job"
);
}
@Test
public
void
reloadJobShouldFailWithoutJobConfigurePermission
()
throws
Exception
{
FreeStyleProject
project
=
j
.
createFreeStyleProject
(
"aProject"
);
project
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
assertThat
(
project
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
changeProjectOnTheDisc
(
project
,
"echo 1"
,
"echo 2"
);
final
CLICommandInvoker
.
Result
result
=
command
.
authorizedTo
(
Job
.
READ
,
Jenkins
.
READ
)
.
invokeWithArgs
(
"aProject"
);
assertThat
(
result
,
failedWith
(
1
));
assertThat
(
result
,
hasNoStandardOutput
());
assertThat
(
result
.
stderr
(),
containsString
(
"user is missing the Job/Configure permission"
));
assertThat
(
project
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
}
@Test
public
void
reloadJobShouldFailWithoutJobReadPermission
()
throws
Exception
{
FreeStyleProject
project
=
j
.
createFreeStyleProject
(
"aProject"
);
project
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
assertThat
(
project
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
changeProjectOnTheDisc
(
project
,
"echo 1"
,
"echo 2"
);
final
CLICommandInvoker
.
Result
result
=
command
.
authorizedTo
(
Job
.
CONFIGURE
,
Jenkins
.
READ
)
.
invokeWithArgs
(
"aProject"
);
assertThat
(
result
,
failedWith
(
1
));
assertThat
(
result
,
hasNoStandardOutput
());
assertThat
(
result
.
stderr
(),
containsString
(
"No such job \u2018aProject\u2019 exists."
));
assertThat
(
project
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
}
@Test
public
void
reloadJobShouldSucceed
()
throws
Exception
{
FreeStyleProject
project
=
j
.
createFreeStyleProject
(
"aProject"
);
project
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
assertThat
(
project
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
changeProjectOnTheDisc
(
project
,
"echo 1"
,
"echo 2"
);
final
CLICommandInvoker
.
Result
result
=
command
.
authorizedTo
(
Job
.
READ
,
Job
.
CONFIGURE
,
Jenkins
.
READ
)
.
invokeWithArgs
(
"aProject"
);
assertThat
(
result
,
succeededSilently
());
assertThat
(
project
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 2"
));
}
@Test
public
void
reloadJobShouldFailIfJobDoesNotExist
()
throws
Exception
{
final
CLICommandInvoker
.
Result
result
=
command
.
authorizedTo
(
Job
.
READ
,
Job
.
CONFIGURE
,
Jenkins
.
READ
)
.
invokeWithArgs
(
"never_created"
);
assertThat
(
result
,
failedWith
(
1
));
assertThat
(
result
,
hasNoStandardOutput
());
assertThat
(
result
.
stderr
(),
containsString
(
"No such job \u2018never_created\u2019 exists."
));
}
@Test
public
void
reloadJobShouldFailIfJobDoesNotExistButNearExists
()
throws
Exception
{
FreeStyleProject
project
=
j
.
createFreeStyleProject
(
"never_created"
);
final
CLICommandInvoker
.
Result
result
=
command
.
authorizedTo
(
Job
.
READ
,
Job
.
CONFIGURE
,
Jenkins
.
READ
)
.
invokeWithArgs
(
"never_created1"
);
assertThat
(
result
,
failedWith
(
1
));
assertThat
(
result
,
hasNoStandardOutput
());
assertThat
(
result
.
stderr
(),
containsString
(
"No such job \u2018never_created1\u2019 exists. Perhaps you meant \u2018never_created\u2019?"
));
}
@Test
public
void
reloadJobManyShouldSucceed
()
throws
Exception
{
FreeStyleProject
project1
=
j
.
createFreeStyleProject
(
"aProject1"
);
project1
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
FreeStyleProject
project2
=
j
.
createFreeStyleProject
(
"aProject2"
);
project2
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
FreeStyleProject
project3
=
j
.
createFreeStyleProject
(
"aProject3"
);
project3
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
assertThat
(
project1
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
assertThat
(
project2
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
assertThat
(
project3
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
changeProjectOnTheDisc
(
project1
,
"echo 1"
,
"echo 2"
);
changeProjectOnTheDisc
(
project2
,
"echo 1"
,
"echo 2"
);
changeProjectOnTheDisc
(
project3
,
"echo 1"
,
"echo 2"
);
final
CLICommandInvoker
.
Result
result
=
command
.
authorizedTo
(
Job
.
READ
,
Job
.
CONFIGURE
,
Jenkins
.
READ
)
.
invokeWithArgs
(
"aProject1"
,
"aProject2"
,
"aProject3"
);
assertThat
(
result
,
succeededSilently
());
assertThat
(
project1
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 2"
));
assertThat
(
project2
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 2"
));
assertThat
(
project3
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 2"
));
}
@Test
public
void
reloadJobManyShouldFailIfAJobDoesNotExist
()
throws
Exception
{
FreeStyleProject
project1
=
j
.
createFreeStyleProject
(
"aProject1"
);
project1
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
FreeStyleProject
project2
=
j
.
createFreeStyleProject
(
"aProject2"
);
project2
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
assertThat
(
project1
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
assertThat
(
project2
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
changeProjectOnTheDisc
(
project1
,
"echo 1"
,
"echo 2"
);
changeProjectOnTheDisc
(
project2
,
"echo 1"
,
"echo 2"
);
final
CLICommandInvoker
.
Result
result
=
command
.
authorizedTo
(
Job
.
READ
,
Job
.
CONFIGURE
,
Jenkins
.
READ
)
.
invokeWithArgs
(
"aProject1"
,
"aProject2"
,
"never_created"
);
assertThat
(
result
,
failedWith
(
1
));
assertThat
(
result
,
hasNoStandardOutput
());
assertThat
(
result
.
stderr
(),
containsString
(
"No such job \u2018never_created\u2019 exists."
));
assertThat
(
project1
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 2"
));
assertThat
(
project2
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 2"
));
}
@Test
public
void
reloadJobManyShouldSucceedEvenAJobIsSpecifiedTwice
()
throws
Exception
{
FreeStyleProject
project1
=
j
.
createFreeStyleProject
(
"aProject1"
);
project1
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
FreeStyleProject
project2
=
j
.
createFreeStyleProject
(
"aProject2"
);
project2
.
getBuildersList
().
add
(
new
Shell
(
"echo 1"
));
assertThat
(
project1
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
assertThat
(
project2
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 1"
));
changeProjectOnTheDisc
(
project1
,
"echo 1"
,
"echo 2"
);
changeProjectOnTheDisc
(
project2
,
"echo 1"
,
"echo 2"
);
final
CLICommandInvoker
.
Result
result
=
command
.
authorizedTo
(
Job
.
READ
,
Job
.
CONFIGURE
,
Jenkins
.
READ
)
.
invokeWithArgs
(
"aProject1"
,
"aProject2"
,
"aProject1"
);
assertThat
(
result
,
succeededSilently
());
assertThat
(
project1
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 2"
));
assertThat
(
project2
.
scheduleBuild2
(
0
).
get
().
getLog
(),
containsString
(
"echo 2"
));
}
/**
* Modify a project directly on the disc
*
* @param project modified project
* @param oldstr old configuration item - for rewrite
* @param newstr rew configuration item - after rewrite
* @throws Exception if an issue occurred
*/
private
void
changeProjectOnTheDisc
(
final
FreeStyleProject
project
,
final
String
oldstr
,
final
String
newstr
)
throws
Exception
{
FilePath
fp
=
new
FilePath
(
new
File
(
project
.
getRootDir
()+
"/config.xml"
));
fp
.
write
(
fp
.
readToString
().
replace
(
oldstr
,
newstr
),
null
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录