在 GitLab MR 中,我們可以添加評論,我們還可以添加功能以在 Jenkins 管道的不同階段發送自動評論。
如果我有特定 MR 的 MR-ID,并且我想獲取對該特定 MR 所做的所有 MR 評論,那么我如何在 Jenkins-Groovy 環境中執行此操作?是否有環境變數可以幫助我?
uj5u.com熱心網友回復:
通常,您需要使用GitLab API。
考慮到對合并請求的評論是通過注釋完成的,您將使用列出所有合并請求注釋
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 沒有提供專用的環境變數,除了你要傳遞給你的作業的引數,比如專案 ID 和 MR ID。
你可以看到像這樣的Jenkinsfile 的Groovy 示例:
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")
}
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/352761.html
