Skip to content
Advertisement

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

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"])"]}

Advertisement

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},
            }],
        },
)
Advertisement