snippetMinor
How to add label to Pull Request via Github API?
Viewed 0 times
pullrequestgithubviahowlabelapiadd
Problem
According to GitHub API user can add label to an issue using this POST request.
I tried using this request but with PR number instead to automatically set label on PR. Here is the function I wrote:
And the response I get is
So I get that this request can't be used to labeling PRs.
My question: Is there other method which could be triggered from Jenkinsfile, to add or change PR label?
Closest thing to the question I asked was this question. Commenting API works on issues and PRs but that's not a case for labels.
I tried using this request but with PR number instead to automatically set label on PR. Here is the function I wrote:
def setLabel (String repository, List labels) {
try {
def response = httpRequest httpMode: 'POST',
url: "https:[Repo_URL]/${repository}/issues/${CHANGE_ID}/labels",
authentication: 'ors_git_service_account',
requestBody: """{
"labels": labels
}"""
} catch (e) {
wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
echo "\u001B[31magdSlack.setLabels: '" + e.toString() + "'\u001B[0m"
}
throw e
}
}And the response I get is
HTTP/1.1 400 Bad RequestSo I get that this request can't be used to labeling PRs.
My question: Is there other method which could be triggered from Jenkinsfile, to add or change PR label?
Closest thing to the question I asked was this question. Commenting API works on issues and PRs but that's not a case for labels.
Solution
I made it work by changing the way
Turns out the list representation while interpolated into string is missing quotation marks around each list element.
labels argument is passed into requestBody.Turns out the list representation while interpolated into string is missing quotation marks around each list element.
def setLabels (String repository, List label) {
try {
String labelsAsString = "[\"${label.join('", "')}\"]"
def response = httpRequest httpMode: 'POST',
url: "[REPO_URL]/${repository}/issues/${CHANGE_ID}/labels",
authentication: 'ors_git_service_account',
requestBody: """{
"labels": ${labelsAsString}
}"""
} catch (e) {
wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
echo "\u001B[31magdSlack.setLabels: '" + e.toString() + "'\u001B[0m"
}
throw e
}
}Code Snippets
def setLabels (String repository, List label) {
try {
String labelsAsString = "[\"${label.join('", "')}\"]"
def response = httpRequest httpMode: 'POST',
url: "[REPO_URL]/${repository}/issues/${CHANGE_ID}/labels",
authentication: 'ors_git_service_account',
requestBody: """{
"labels": ${labelsAsString}
}"""
} catch (e) {
wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
echo "\u001B[31magdSlack.setLabels: '" + e.toString() + "'\u001B[0m"
}
throw e
}
}Context
StackExchange DevOps Q#12891, answer score: 2
Revisions (0)
No revisions yet.