【问题标题】:Is there any way to add components to jira issue using python jira client?有没有办法使用 python jira 客户端将组件添加到 jira 问题?
【发布时间】:2022-09-25 14:28:00
【问题描述】:

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

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

这是输出以下结果

[]

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

    标签: python jira atlassian-plugin-sdk


    【解决方案1】:

    我是这个问题的作者,我到处研究,遗憾的是没有找到使用 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},
                }],
            },
    )
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-30
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 1970-01-01
        相关资源
        最近更新 更多