【问题标题】:how to fetch all GitLab comments of an MR in a Jenkins job如何在 Jenkins 作业中获取 MR 的所有 GitLab 评论
【发布时间】:2021-12-21 01:29:09
【问题描述】:

在 GitLab MR 中,我们可以添加 cmets,我们还可以添加功能以在 Jenkins 管道的不同阶段发送自动 cmets。

如果我有特定 MR 的 MR-ID,并且我想获取在该特定 MR 上完成的所有 MR cmets,那么我该如何在 Jenkins-Groovy 环境中进行呢?是否有环境变量可以帮助我?

【问题讨论】:

    标签: jenkins groovy gitlab jenkins-pipeline jenkins-groovy


    【解决方案1】:

    一般情况下,您需要使用GitLab API。

    考虑到Comments on merge requests 是通过注释完成的,您将使用List all merge request notes

    GET /projects/:id/merge_requests/:merge_request_iid/notes
    GET /projects/:id/merge_requests/:merge_request_iid/notes?sort=asc&order_by=updated_at
    

    Jenkins 没有提供专门的环境变量,除了您将传递给 Job 的参数,例如项目 ID 和 MR ID。

    你可以看到Groovy examples这样Jenkinsfile:

    stage ("Merge Pull Request") {
        // GET PULL REQUEST ID
        sh "curl -H \"PRIVATE-TOKEN: ${approval_token}\" \"https://gitlab.com/api/v4/projects/${projectID}/merge_requests\" --output resultMerge.json"
        def jsonMerge = readJSON file: "resultMerge.json"                    
        echo "Request from: ${jsonMerge[0].author.name}"
        // STATUS VALIDATION
        if (jsonMerge[0].state == "opened") {
            // GET ALL COMMENTS ON PULL REQUEST
            sh "curl -H \"PRIVATE-TOKEN: ${approval_token}\" \"https://gitlab.com/api/v4/projects/${projectID}/merge_requests/${jsonMerge[0].iid}/notes\" --output comment.json"
            def commentJson = readJSON file: "comment.json"
            def checking = false
            // LOOP ALL COMMENT TO GET APPROVAL
            commentJson.each { res ->
                // CHECK IF CURRENT INDEX HAS SYSTEM FALSE 
                if (!res.system && !checking) {
                    // IF COMMENT HAS VALUE: APPROVED AND AUTHOR IS VALID
                    if (res.body == "Approved" && approval.contains(res.author.username)) {
                        addGitLabMRComment(comment: "Pull Request Approved by Jenkins")
                        acceptGitLabMR(useMRDescription: true, removeSourceBranch: false)
                    } else {
                        currentBuild.result = 'ABORTED'
                        error("Sorry, your approval is not valid")
                    }
                    checking = true
                }
            }
        } else {
            error("Pull Request ${jsonMerge[0].title} ${jsonMerge[0].iid} is already ${jsonMerge[0].state}. Please Create a new Pull Request")
        }
        ...
    }
    

    【讨论】:

    • 抱歉@VonC 我是 stackoverflow 的新手,不知道我们可以接受并支持答案。感谢分享链接。我也知道将来该怎么做。
    • @Aayush 没问题:不要忘记重新查看您过去的问题:其中一些可能有值得支持/接受勾选的答案。
    猜你喜欢
    • 1970-01-01
    • 2013-02-11
    • 2018-11-23
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多