如何在jira-python 1.0.7中获取附件链接和附件ID?

时间:2016-06-16 09:49:09

标签: jira jira-rest-api python-jira

我正在使用JIRA-python 1.0.7。鉴于一个特定的问题,我试图获得附件ID和附件uri。 JIRA-python文档说我可以使用以下属性访问附件 issue.fields.attachment issue.fields.attachment.id 但是我得到了这些错误

In [23]: issue.fields.attachment
--------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/lib/python3.4/site-packages/jira/jirashell.py in <module>()
----> 1 issue.fields.attachment
AttributeError: type object 'PropertyHolder' has no attribute 'attachment'

该文件还建议我可以使用以下方法获取附件资源

jira.attachment(ID)

In [24]: issue=jira.issue('OPENNLP-830')
In [25]: issue.id
Out[25]: '12931716'
In [26]: jira.attachment(12931716)
---------------------------------------------------------------------------
JIRAError                                 Traceback (most recent call last)
/usr/lib/python3.4/site-packages/jira/jirashell.py in <module>()
----> 1 jira.attachment(12931716)
JIRAError: JiraError HTTP 404 url: https://issues.apache.org/jira/rest/api/2/attachment/12931716 details: /tmp/jiraerror-0n8wkhwj.tmp

我哪里出错了。请建议 的修改 我的实际代码

#!usr/bin/python

from jira import JIRA
import psycopg2
from creating_db import create_db  

def main():
    #Establishing connection with JIRA 
    options = {'server': 'https://issues.apache.org/jira'}
    jira = JIRA(options)
    #Creating the necessary database
    create_db()
    #Connecting to the database
    try:
        conn=psycopg2.connect("dbname='issue_db' host='master' user='hema' password='password'")
    except psycopg2.Error as e:
        print (e)
    cur=conn.cursor ()
    #This command returns an object of type ResultList. ResultList has an attribute called 'total' which gives us the total number of issues filled so far in a given project.
    issue_count=jira.search_issues('project=OPENNLP')
    #Iteratively receiving the issues 
    for count in range(0, issue_count.total,50):
        issue_in_proj = jira.search_issues('project=OPENNLP', startAt = count, maxResults = 50)
        issue_attachment=[issue.fields.attachment for issue in issue_in_proj] # the documentation says its issue.fields.attachment and not attachments
        for i in range( len(issue_in_proj)):
            for j in range(len(issue_attachment[i]):
                cur.execute('''INSERT INTO attachments VALUES(%s,%s)''',  [(issue_id[i],issue_attachment[i][j].id)])
    conn.commit()
    cur.close
    conn.close 

if __name__=="__main__":
    main()

2 个答案:

答案 0 :(得分:1)

我今天的版本1.0.7遇到了完全相同的问题:未找到属性'attachment',但是当我使用&lt; tab&gt;时它确实被发现了。

您是否可以先使用Behavior<TextBox>来检索问题,就像我一样?它返回的对象由于某种原因缺少'attachment'属性。

但是,您可以通过调用issues = jira.search_issues()来检索特定问题的新对象。此对象将具有“附件”属性(因此issue = jira.issue(issues[i].key))。

希望这有帮助!

答案 1 :(得分:0)

jira中的解析器没有解析出JSON文档中其余响应返回的attachment密钥。你需要 issues = jira.search_issues("assignee in (currentUser())", fields=["attachment"]