Is there any way to add components to jira issue using python jira client?

2.6k views Asked by At

I was working on a project where I have to update jira issue fields like components , epic links,etc. I am using jira python client(https://pypi.org/project/jira/) to do this task.

ticketObj = authJira.issue('ABC-12345')
print(ticketObj.fields.components)

This is outputing below results

[]

Because components is a array So If want to update a field in the jissue I have to do the below things

ticketObj.update(components = ['component 1'])

But this method is giving below error

JiraError HTTP 400 url: https://jira.yourdomain.com/rest/api/2/issue/1234567
    text: Can not instantiate value of type [simple type, class com.atlassian.jira.rest.api.issue.FieldOperation] from JSON String; no single-String constructor/factory method (through reference chain: com.atlassian.jira.rest.v2.issue.IssueUpdateBean["update"])
    
    response headers = {...}
    response text = {"errorMessages":["Can not instantiate value of type [simple type, class com.atlassian.jira.rest.api.issue.FieldOperation] from JSON String; no single-String constructor/factory method (through reference chain: com.atlassian.jira.rest.v2.issue.IssueUpdateBean[\"update\"])"]}
3

There are 3 answers

0
Subramanya Krishna On BEST ANSWER

I'm the author of this question, I researched everywhere and I sadly didn't find the method to add components using jira python client,so I created new method to add component which uses rest api provided by .

    def addComponentToTicket(self,ticketKey:str,component:str):
        payload = json.dumps({"update": {"components": [{
            "add": {"name": component}
        }], }})
        headers = { 
                           "Accept": "application/json",
                           "Content-Type": "application/json",
                           "Authorization":f'Bearer {self.personalAccessToken}',
                           "X-Atlassian-Token": "no-check",
                        }
        response = requests.put(f'{self.REST_URL}issue/{ticketKey}',
                                data=payload,
                                headers=headers,
                                verify=False)

I got another cleaner method, If We want to update any fields of issue in jira we could use the Jira api method of updating by passing dictionary to update argument.

ticketObj.update(
    update=
        {
            "components": [{
                "add": {"name": component},
            }],
        },
)
0
Extro On

This works for the current version of Jira Cloud (Python 3)...

def addComponentToTicket(self, ticketKey: str, component: str):
    payload = json.dumps({"update": {"components": [{"add": {"name": component}}]}})
    url = self.server_url + f'/rest/api/latest/issue/{ticketKey}'
    response = requests.put(url,
                            auth=(f'{email}', f'{jira_token}'),
                            data=payload,
                            headers={"Content-Type": "application/json"},
                            verify=False)
    if (response.status_code >= 200) and (response.status_code < 300):
        return response
    else:
        print(f'addComponentToTicket() Error: {response.status_code} - {response.text}')

    return response


def setComponentsOfTicket(self, ticketKey: str, component_list: list):
    comp_set = []
    for c in component_list:
        comp_set.append({"name": c})
    payload = json.dumps({"update": {"components": [{"set": comp_set}]}})
    url = self.server_url + f'/rest/api/latest/issue/{ticketKey}'
    response = requests.put(url,
                            auth=(f'{email}', f'{jira_token}'),
                            data=payload,
                            headers={"Content-Type": "application/json"},
                            verify=False)
    if (response.status_code >= 200) and (response.status_code < 300):
        return response
    else:
        print(f'setComponentsOfTicket() Error: {response.status_code} - {response.text}')

    return response


def clearComponentsFromTicket(self, ticketKey: str):
    payload = json.dumps({"update": {"components": [{"set": []}]}})
    url = self.server_url + f'/rest/api/latest/issue/{ticketKey}'
    response = requests.put(url,
                            auth=(f'{email}', f'{jira_token}'),
                            data=payload,
                            headers={"Content-Type": "application/json"},
                            verify=False)
    if (response.status_code >= 200) and (response.status_code < 300):
        return response
    else:
        print(f'clearComponentsFromTicket() Error: {response.status_code} - {response.text}')

    return response

# Set the methods into the JIRA class... 
JIRA.addComponentToTicket = addComponentToTicket
JIRA.setComponentsOfTicket = setComponentsOfTicket
JIRA.clearComponentsFromTicket = clearComponentsFromTicket

0
mnokka On

I found following working (when updating component in existing issue)

 issue.update(notify=False,update={"components": [{"add": {"name": str(NAME),}}],},)

NAME being name of the component (must exist in the project)