Is there a way to append multiple labels to an existing issue via the Jira API?

Carlye Petoff September 27, 2022

Hello all, 

Based on my research so far, I see that there is a way to append one label at a time to an existing issue, or you can also update multiple labels on an existing ticket, but that will then remove any existing labels the issue had originally. I'm looking for a way to append multiple labels to an existing issue, without removing any labels that already exist. I'm looking to do this within one API request. Any ideas if this is possible? 

What I'm working with:

  • Version 3 of the Jira Cloud Platform REST API
  • Python

 

Appreciate any insight!

 

3 answers

2 accepted

5 votes
Answer accepted
Brant Schroeder
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 27, 2022

@Carlye Petoff Here is the latest REST API documentation so you know how to properly form your edit call.  https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-issueidorkey-put You will need to use the labels ADD to not remove any of the existing labels.

2 votes
Answer accepted
Oliver Siebenmarck _Polymetis Apps_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 28, 2022

Hi @Carlye Petoff ,

Both, @Brant Schroeder and @Nicolas Grossi , are correct and there answers point to the right direction.

But I just could not pass up the opportunity to help in a label-related topic and write some Python code for fun. So, here you go, a quick example written by someone who hasn't done Python in ages:

 

import requests
from requests.auth import HTTPBasicAuth

def
addLabels(baseUrl, user, apiKey, issueKey, labels):
"Add multiple labels to an issue"
auth = HTTPBasicAuth(user, apiKey)
url = baseUrl + "/rest/api/3/issue/" + issueKey

headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}

payload = """{
"update": {
"labels": [
"""

for label in labels:
payload = payload + "{\"add\": \""+ label+ "\"},"
payload = payload[:-1] + "]}}"

response = requests.request(
"PUT",
url,
data=payload,
headers=headers,
auth=auth
)
return response.status_code

print(addLabels("https://YOURCOMPANY.atlassian.net", "YOUR_EMAIL", "API_TOKEN", "ISSUE-58", ["label_1", "label_2", "label_3"]))

Hope that helps,
 Oliver

0 votes
Carlye Petoff September 28, 2022

Thanks all. I appreciate the responses. I figured out what I was doing wrong with the syntax:

If you want to add multiple labels without removing any of the existing labels, you should format like this:

payload = json.dumps({ "update": {"labels": [{"add": "label1"}, {"add":"label2"}, {"add":"label3"}, {"add":"label4"}]}})

 

Originally I was doing this, which is incorrect:

payload = json.dumps({ "update": { "labels" : [{"add":"label1", "label2", "label3"}] }})

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events