如何在 Jenkins 工作中获取 MR 的所有 GitLab 评论
how to fetch all GitLab comments of an MR in a Jenkins job
在 GitLab MR 中,我们可以添加评论,我们还可以添加功能以在 Jenkins 管道的不同阶段发送自动评论。
如果我有特定 MR 的 MR-ID,并且我想获取在该特定 MR 上完成的所有 MR 评论,那么我如何在 Jenkins-Groovy 环境中执行此操作?有环境变量可以帮助我吗?
一般来说,您需要使用 GitLab API。
考虑Comments on merge requests are done through notes, you would use 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
除了您要传递给作业的参数(例如项目 ID 和 MR ID)之外,Jenkins 没有提供专用的环境变量。
可以看到Groovy examples like this 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")
}
...
}
在 GitLab MR 中,我们可以添加评论,我们还可以添加功能以在 Jenkins 管道的不同阶段发送自动评论。
如果我有特定 MR 的 MR-ID,并且我想获取在该特定 MR 上完成的所有 MR 评论,那么我如何在 Jenkins-Groovy 环境中执行此操作?有环境变量可以帮助我吗?
一般来说,您需要使用 GitLab API。
考虑Comments on merge requests are done through notes, you would use 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
除了您要传递给作业的参数(例如项目 ID 和 MR ID)之外,Jenkins 没有提供专用的环境变量。
可以看到Groovy examples like this 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")
}
...
}