<p>I made it work by changing the way <code>labels</code> argument is passed into <code>requestBody</code>.</p>
<p>Turns out the list representation while interpolated into string is missing quotation marks around each list element.</p>
<pre><code>def setLabels (String repository, List label) {
    try {
        String labelsAsString = &quot;[\&quot;${label.join('&quot;, &quot;')}\&quot;]&quot;
        def response = httpRequest httpMode: 'POST',
                        url: &quot;[REPO_URL]/${repository}/issues/${CHANGE_ID}/labels&quot;,
                        authentication: 'ors_git_service_account',
                        requestBody: &quot;&quot;&quot;{
                            &quot;labels&quot;: ${labelsAsString}
                        }&quot;&quot;&quot;
    } catch (e) {
        wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
            echo &quot;\u001B[31magdSlack.setLabels: '&quot; + e.toString() + &quot;'\u001B[0m&quot;
        }
        throw e
    }
}
</code></pre>

CC BY-SA 4.0
2020-12-01T14:29:08.883Z
2020-12-01T14:29:08.883Z
w8eight
<p>According to GitHub API user can add label to an issue using <a href="https://developer.github.com/v3/issues/labels/#add-labels-to-an-issue" rel="nofollow noreferrer">this</a> POST request.</p>
<p>I tried using this request but with PR number instead to automatically set label on PR. Here is the function I wrote:</p>
<pre><code>def setLabel (String repository, List labels) {
    try {
        def response = httpRequest httpMode: 'POST',
                    url: &quot;https:[Repo_URL]/${repository}/issues/${CHANGE_ID}/labels&quot;,
                    authentication: 'ors_git_service_account',
                    requestBody: &quot;&quot;&quot;{
                        &quot;labels&quot;: labels
                    }&quot;&quot;&quot;
    } catch (e) {
        wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
            echo &quot;\u001B[31magdSlack.setLabels: '&quot; + e.toString() + &quot;'\u001B[0m&quot;
        }
        throw e
    }
}
</code></pre>
<p>And the response I get is <code>HTTP/1.1 400 Bad Request</code></p>
<p>So I get that this request can't be used to labeling PRs.</p>
<p>My question: <strong>Is there other method which could be triggered from Jenkinsfile, to add or change PR label?</strong></p>
<p>Closest thing to the question I asked was <a href="https://stackoverflow.com/questions/16744069/create-comment-on-pull-request">this question</a>. Commenting API works on issues and PRs but that's not a case for labels.</p>

<jenkins><github><jenkinsfile>
How to add label to Pull Request via Github API?
w8eight
# How to add label to Pull Request via Github API?

(Asked by: username_0 on 2020-12-01)

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.

**Tags:** jenkins, github, jenkinsfile


---

## 1 answers

### Answer by username_0 on 2020-12-01. Score: 2 | Accepted answer

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
    }
}


---

