简体   繁体   English

使用Jenkins Groovy管道脚本解析数据

[英]Parse Data Using Jenkins Groovy Pipeline Script

I am retrieving JSON object from a URL using httpRequest in a groovy script. 我正在使用Groovy脚本中的httpRequest从URL检索JSON对象。

pipeline {
  agent any
  stages {
      stage ('Extract Data') {
          steps {
            script {
              def response = httpRequest \
              authentication: 'user', \
              httpMode: 'GET', \
              url: "https://example.com/data"
              writeFile file: 'output.json', text: response.content

              def data = readFile(file: 'output.json')
              def details = new groovy.json.JsonSlurperClassic().parseText(data)

              echo "Data: ${details.fields.customfield}"                                        

              }
            }
        }
   }
}

I am interested in the customfield string. 我对customfield字符串感兴趣。 The format of the string is: 字符串的格式为:

Application!01.01.01 TestSuite1,TestSuite2,TestSuite3,TestSuite4 Product!01.01.01,Product2!01.01.02

I would like to parse the string into 3 data sets: 我想将字符串解析为3个数据集:

  1. Map of Applications [Application: version] (there will always be one Appliction) 应用程序图[应用程序:版本](总会有一个Appliction)
  2. List of TestSuites [TestSuite1,...,TestSuite] TestSuite列表[TestSuite1,...,TestSuite]
  3. Map of Prodcts [Product1: version,..., ProductN: version]. 产品地图[产品1:版本,...,产品N:版本]。

However, I am not sure how to do this. 但是,我不确定如何执行此操作。 Are there any Jenkins Groovy libraries that I can use to do this in a declarative pipeline? 我可以在声明式管道中使用任何Jenkins Groovy库吗?

EDIT Based on the answer below I can see that I can make a map in the following way: 编辑根据以下答案,我可以看到我可以通过以下方式制作地图:

def applications = groups[0].split(',').collect { it.split('!') }.collectEntries { [(it):it] }

In the example I have: 在示例中,我有:

application = [Application: Application]

How do I get: application = [Application: 01.01.01] 我如何获得:应用程序= [应用程序:01.01.01]

EDIT2 Note the following output: EDIT2请注意以下输出:

def applications = groups[0].split(',').collect { it.split('!') }
[[Application, 01.01.01]]

There're no libraries I'm aware of that will have functionality to parse the data but, since you know the format of the data it's easy to parse them manually . 据我所知,没有一个库具有解析数据的功能,但是,由于您知道数据的格式,因此很容易手动解析它们。

There are 3 groups in the input ( applications , suites , products ) separated by a 输入中有3组( applicationssuitesproducts ),由 character. 字符。 To get the groups you need: 要获得组,您需要:

def input = "Application!01.01.01 TestSuite1,TestSuite2,TestSuite3,TestSuite4 Product!01.01.01,Product2!01.01.02"
def groups = input.split(' ')

To process the applications you need to split group 0 with , character (just in case there are many applications). 要处理你需要拆分组0的应用程序,字符(以防万一有很多的应用程序)。 You got a list of pairs in format: name!version . 您会得到以下格式的成对列表: name!version Every pair must be splitted with ! 每对必须分开! , so you get a list of lists in format: [[name, version]] . ,因此您将获得格式为[[name, version]]的列表列表。 From the last structure it's easy to create a map. 从最后的结构很容易创建地图。 All steps together: 所有步骤合在一起:

def applications = groups[0].split(',').collect { it.split('!') }.collectEntries { [(it[0]):it[1]] }

Getting the list of suites is easy, just split group 1 with , character: 获取套件列表很容易,只需将第一个分组用,字符分开:

def suites = groups[1].split(',')

Finally, products are analogical to the list of applications but this time group 2 should be used: 最后,产品类似于应用程序列表,但是应该使用第二组:

def products = groups[2].split(',').collect { it.split('!') }.collectEntries { [(it[0]):it[1]] }

You can simplifier your issue by using pipeline utility step: readJSON 您可以使用管道实用程序步骤来简化问题: readJSON

def data = readJSON(file: 'output.json')

echo data.fields.customfield

I found a method. 我找到了一种方法。 Groovy can convert the values of an Object array and convert them into a map with the toSpreadMap() . Groovy可以转换一个Object数组的值,并使用toSpreadMap()将它们转换为一个映射。 However, the array must have an even number of elements. 但是,数组必须具有偶数个元素。

def appList = ['DevOpsApplication', '01.01.01']
def appMap = appList.toSpreadMap()

For some better answers please refer to this 对于一些更好的答案,请参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM