首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Groovy/Jenkins -如何在http请求体中使用for循环

Groovy/Jenkins -如何在http请求体中使用for循环
EN

Stack Overflow用户
提问于 2018-12-22 06:47:34
回答 1查看 716关注 0票数 0

我尝试动态地读入一个数组(每个元素都是一个字符串),并使用这些字符串值替换当前硬编码的用户名。用于在Bitbucket中创建拉流请求。

下面的#1和#2都属于同一个类BitbucketUtil.groovy

1:

代码语言: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']
        }

2:

代码语言:javascript
复制
   def getGroupUsers(groupName, activeOnly) {
        def getUsersResponse = this.steps.httpRequest(
                acceptType: 'APPLICATION_JSON',
                authentication: this.userId,
                ignoreSslErrors: true,
                quiet: true,
                responseHandle: 'STRING',
                url: "https://bitbucket.absolute.com/rest/api/latest/admin/groups/more-members?context=pd-teamthunderbird",
                validResponseCodes: '200:299')
        def usersPayload = this.steps.readJSON(text: getUsersResponse.content)['values']
        getUsersResponse.close()
        def users = []
        usersPayload.each { user ->
            if (!activeOnly || (activeOnly && user['active'])) {
                users.add(user['name'])
            }
        }
        return users
        //this is returning an array with string elements inside
    }

我猜使用函数getGroupUsers (groupName参数是teamName),我可以在createPullRequest函数中替换"reviewers"中的硬编码字符串。但我不确定如何在“审阅者”下面使用for循环,这样我才能动态地放值:

代码语言: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-22 08:43:09

如果您的名称已定义,并且您的最终目标是在其中的某个位置创建一个包含所有名称的映射列表,那么您只需从名称中collect映射即可。例如。

代码语言:javascript
复制
def names = ["HardCoded1", "HardCoded2"]
println([reviewers: names.collect{ [user: [name: it]] }])
// => [reviewers:[[user:[name:HardCoded1]], [user:[name:HardCoded2]]]]

如果您的目标是创建一个JSON主体,请不要连接字符串。使用Groovy提供的工具来创建JSON。例如。

代码语言:javascript
复制
groovy.json.JsonOutput.toJson([
    title: title,
    state: "OPEN",
    reviewers: names.collect{ [user: [name: it]] }],
    // ...
])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53891589

复制
相关文章

相似问题

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