2

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:

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 Request

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.

1 Answer 1

2

I made it work by changing the way 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
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.