首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Groovy集合与字符串格式完全匹配

使用Groovy集合与字符串格式完全匹配
EN

Stack Overflow用户
提问于 2018-12-24 19:21:11
回答 1查看 173关注 0票数 0

关于如何使用groovy集合匹配确切的字符串格式,我有一个问题。

代码语言:javascript
复制
   def createPullRequest(projectSlug, repoSlug, title, description, sourceBranch, targetBranch) {
    //this is reading in the array with the user names
    def names = BitbutkcetUtil.getGroupUsers(teamName, activeOnly)

            def prResponse = this.steps.httpRequest(
                    acceptType: 'APPLICATION_JSON',
                    authentication: this.userId,
                    contentType: 'APPLICATION_JSON',
                    httpMode: 'POST',
                    ignoreSslErrors: true,
                    quiet: true,
                    requestBody: """
                                {
                                    "title": "${title}",
                                    "description": "${description}",
                                    "state": "OPEN",
                                    "open": true,
                                    "closed": false,
                                    "fromRef": { "id": "${sourceBranch}" },
                                    "toRef": { "id": "${targetBranch}" },
                                    "locked": false,
                                    "reviewers": [
                                        //I want to replace this hardcoded names with the string values inside the array `names`
                                        { "user": { "name": "HardCoded1" } },
                                        { "user": { "name": "HardCoded2" } },
                                        { "user": { "name": "HardCoded3" } },
                                        { "user": { "name": "HardCoded4" } }
                                    ]
                                }
                            """,
                    responseHandle: 'STRING',
                    url: "https://bitbucket.absolute.com/rest/api/latest/projects/${projectSlug}/repos/${repoSlug}/pull-requests",
                    validResponseCodes: '200:299')
            def pullRequest = this.steps.readJSON(text: prResponse.content)
            prResponse.close()
            return pullRequest['id']
        }

我想要做的是将reviewers中的reviewers名称替换为数组names中的字符串元素。我想要使用这个集合,但我必须符合确切的格式。

代码语言:javascript
复制
 { "user": { "name": "HardCoded1" } },
 { "user": { "name": "HardCoded2" } },
 { "user": { "name": "HardCoded3" } },
 { "user": { "name": "HardCoded4" } }

现在,我有了[reviewers: names.collect{ [user: [name: it]] }],它输出了以下内容:

代码语言:javascript
复制
[reviewers:[[user:[name:name1]],
[user:[name:name2]],
[user:[name:name3]],
[user:[name:name4]]]] 

如何使输出符合以下格式?

代码语言:javascript
复制
    "reviewers": [
  //I want to replace this hardcoded names with the string values inside the array `names`
    { "user": { "name": "HardCoded1" } },
    { "user": { "name": "HardCoded2" } },
    { "user": { "name": "HardCoded3" } },
    { "user": { "name": "HardCoded4" } }
    ]

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-24 21:31:22

您看到的是在映射元素列表上调用toString()方法的结果。对于有效的JSON表示,可以将collect()方法的结果传递给JsonOutput.toJSON()。就像这样:

代码语言:javascript
复制
requestBody: """
            {
                "title": "${title}",
                "description": "${description}",
                "state": "OPEN",
                "open": true,
                "closed": false,
                "fromRef": { "id": "${sourceBranch}" },
                "toRef": { "id": "${targetBranch}" },
                "locked": false,
                "reviewers": ${JsonOutput.toJson(names.collect{ [user: [name: it]] })}
            }
        """

JsonOutput.toJSON()首次在Jenkins管道中使用时,可能需要脚本的批准。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53917043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档