我目前的情况是我需要触发状态" icon-red"在Jenkins服务器上,由给定的用户特定视图(my-views)选择。问题是列表很长,我们不想手动触发它们。 这就是我开始使用Groovy脚本(Jenkins'脚本控制台)的想法。
我可以使用以下代码触发给定全局视图的所有红色作业:
def viewName = "globalviewname"
def jobsToBuild = Jenkins.instance.getView(viewName).items.findAll { job ->
job.getBuildStatusIconClassName() == "icon-red"
}
jobsToBuild.each { job ->
println "Scheduling matching job ${job.name}"
job.scheduleBuild(new Cause.UserIdCause())
}
但是,我缺乏如何访问当前用户视图的方式(稍后会成为参数):调用
Jenkins.instance.getViews()
仅提供所有全局视图的列表。我正在玩
Jenkins.instance.getMyViewsTabBar()
(另见http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html#getMyViewsTabBar()),但显然我没有掌握它。
有关如何访问与用户特定列表视图相关联的项目列表的任何线索?
答案 0 :(得分:0)
我想我自己找到了它:
假设变量username
包含我们想要获取的视图的用户名,变量viewname
包含我们想要检索的视图的名称,下面的原型Groovy编码可以解决这个问题。我:
def user = User.get(username, false, null)
if (user == null) {
throw new Error("User does not exists")
}
println "Reading data from user "+user.toString()
// retrieve all UserProperties of this user and filter on the MyViewsProperty
def allMyViewsProperties = user.getAllProperties().findAll {
uprop -> (uprop instanceof hudson.model.MyViewsProperty)
}
if (allMyViewsProperties.size() == 0) {
throw new Error("MyViewsProperties does not exists")
}
// retrieve all views which are assigned to the MyViewsProperty.
// note that there also is a AllViewsProperty
def allPersonalViewsOfUser = allMyViewsProperties[0].getViews()
// further narrow down only to ListViews (special use case for me)
def allPersonalListViews = allPersonalViewsOfUser.findAll {
view -> view instanceof hudson.model.ListView
}
// based on its name, filter on the view we want to retrieve
def listView = allPersonalListViews.findAll { view -> viewname.equals(view.getViewName()) }
if (listView.size() != 1) {
throw new Error("selected view does not exist");
}
// get the view now
def view = listView[0]
鉴于这一切,现在可以通过运行
轻松触发此视图状态为红色的所有作业def jobsToBuild = view.items.findAll { job ->
job.getBuildStatusIconClassName() == "icon-red"
}
jobsToBuild.each { job ->
println "Scheduling matching job ${job.name}"
job.scheduleBuild(new Cause.UserIdCause())
}