有没有什么方法可以使用python jira客户端向jira issue添加组件?

mwg9r5ms  于 2023-06-20  发布在  Python
关注(0)|答案(3)|浏览(101)

我正在做一个项目,我必须更新jira的问题领域,如组件,史诗链接等。我使用jira python客户端(https://pypi.org/project/jira/)来完成这个任务。

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

这是输出以下结果

[]

因为组件是一个数组,所以如果要更新jissue中的字段,我必须做以下事情

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

但这种方法给出了以下误差

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

gwbalxhn1#

我发现以下工作(当更新现有问题中的组件时)

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

NAME是组件的名称(必须存在于项目中)

gajydyqb

gajydyqb2#

我是这个问题的作者,我到处研究,很遗憾我没有找到使用jira python客户端添加组件的方法,所以我创建了一个新的方法来添加组件,使用提供的rest API。

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)

我有另一个更干净的方法,如果我们想更新jira中的任何问题字段,我们可以使用Jira API方法通过传递字典来更新参数。

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

az31mfrm3#

这适用于当前版本的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

相关问题