以编程方式 select 来自字符串列表的 WarningNext Generation Parsers

Programmatically select WarningNext Generation Parsers from a list of strings

如何提供 Warnings Next Generation Plugin 将激活解析器的字符串列表?

我想在我的共享库中有一个如下所示的通用方法

  def withWarningNG(def listOfParsers, closure) {
    def result
    try {
      result = closure()
    } finally {
      recordIssues(tools: listOfParsers)
    }
    return result
  }


// And use it in various Jenkinsfiles thus:

 node() {
   mySharedLibVar.withWarningNG(['gcc', 'java']) {
     ... do something ...
   }

这将确保我们始终尝试 运行 解析器,即使单元测试未通过构建也是如此。我意识到当出现编译错误时,解析器不会 运行。

我知道我可以通过 Declarative 管道来做到这一点。然而,当我们尝试这种方法时,我们使用的各种例程都会出现问题。随着我们将更多构建逻辑迁移到 sharedLibrary 声明中,可能会变得更有用。目前,我想在标准管道中执行此操作。

我们可以通过

轻松传递字符串并将其作为函数执行
def foo() { return "bar"}

def myFunction="foo"

"${foo}"()

因此,我们可以创建一个像这样的方法来接受分析器列表。

/**
 * Run a set of WarningsNextGeneration analyzers
 * @param analyzersList  list of strings mapping to https://github.com/jenkinsci/warnings-ng-plugin/blob/master/SUPPORTED-FORMATS.md
 * @param closure jenkins pipeline code to execute
 * @return result of that pipeline code
 */
def withWarningNG(def analyzersList, closure) {
    def result
    try {
        result = closure()
    } finally {
        for (analyzer in analyzersList) {
            println "Configure Recorder for ${analyzer}"
            recordIssues(tools: ["${analyzer}"()], enabledForFailure: true)
        }
    }
    return result
}