本文介绍了从Build原因获取的JSONArray的getUpstreamRun的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 getUpstreamRun()以获取Run对象.

I am trying getUpstreamRun() in order to get Run object.

def causes = currentBuild.getBuildCauses()
// List<Cause.UpstreamCause> upstreamCauses = currentBuild.getBuildCauses('hudson.model.Cause$UpstreamCause')
def upstreamCauses = currentBuild.getBuildCauses('hudson.model.Cause$UpstreamCause')

for (Cause cause : upstreamCauses) {
  echo "currentCause: ${cause}"
  if (cause instanceof Cause.UpstreamCause) {
    Cause.UpstreamCause upstreamCause = (Cause.UpstreamCause)cause;
    Run<?, ?> upstreamRun = upstreamCause.getUpstreamRun();
    if (upstreamRun != null) {
      def env = upstreamRun.getEnvironment(TaskListener.NULL)
      println env
    }
  }
}

if (cause instanceof Cause.UpstreamCause)无法到达getUpstreamRun()函数调用时失败(如果不满足条件).看起来我缺少某种类型转换

It's failing (if condition is not satisfied) at if (cause instanceof Cause.UpstreamCause) unable to reach getUpstreamRun() function call. Looks like i am missing some kind of type casting

变量upstreamCauses看起来像这样,类型为"net.sf.json.JSONArray"

varible upstreamCauses looks like this and is of type "net.sf.json.JSONArray"

[[_class:hudson.model.Cause$UpstreamCause, 
 shortDescription:Started by upstream project "mod_production-controller- 
 g5/v1.0_handler_mod_production-controller-g5" build number 7, 
 upstreamBuild:7, 
 upstreamProject:mod_production-controller-g5/v1.0_handler_mod_production-controller-g5, 
  upstreamUrl:job/mod_production-controller-g5/job/v1.0_handler_mod_production-controller-g5/
]]

currentCause

currentCause

[_class:hudson.model.Cause$UpstreamCause, 
 shortDescription:Started by upstream project "mod_production-controller- 
 g5/v1.0_handler_mod_production-controller-g5" build number 7, 
 upstreamBuild:7, 
 upstreamProject:mod_production-controller-g5/v1.0_handler_mod_production-controller-g5, 
  upstreamUrl:job/mod_production-controller-g5/job/v1.0_handler_mod_production-controller-g5/
]

请注意如何将此JsonArray转换为适当的格式以获得运行项.任何帮助,我们将不胜感激.

Note how to convert this JsonArray to appropriate format to obtain run item. Any help is greatly appreciated.

推荐答案

如果currentCause是数组,则可能要提取其第一个成员:

If currentCause is an array, you may want to extract its first member:

Cause.UpstreamCause upstreamCause = (Cause.UpstreamCause)(cause[0])

这篇关于从Build原因获取的JSONArray的getUpstreamRun的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:59