无法通过 python-jira 访问 jira 工作日志中的注释

问题描述 投票:0回答:1

我正在尝试使用 jira-python 库从 python 中的 Jira Service Management Cloud 检索工作日志条目。

除了从每个工作日志中获取评论之外,一切正常。文档对此相当模糊。

我的代码目前如下所示:

jira = JIRA(server, basic_auth=(email,token))

# define date filter
startDate = datetime.date(2024, 2, 1)
endDate = datetime.date(2024, 2, 27)

#define jql to filter issues
jql = "project in (XY) AND issuetype in ('[System] Incident', '[System] Service request') AND status = Closed AND resolutiondate >= {} AND resolutiondate <= {}".format(startDate, endDate)

#incremental fetch of issues and storage into issues_all
pos = 0;
batch = 100;
issues_all = []
while True:
    issues_batch = jira.search_issues(jql_str=jql, startAt=pos, maxResults=batch, fields=['worklog','customfield_10202','customfield_10198','customfield_10191'])
    if issues_batch == []:
        break
    else:
        issues_all += issues_batch
    pos += batch

with open('jiraExport.csv', 'w', newline='', encoding='utf-8') as file:
    writer = csv.writer(file, delimiter=";")
    fields = ["Issue", "Issue ID","Warranty Basis","Warranty","Warranty Reason","Worklog ID", "Author Name", "Author Email","Date", "Time Spent", "Description"]

    writer.writerow(fields)

    for issue in issues_all:
        current_issue = jira.issue(issue)
        worklogs = current_issue.fields.worklog.worklogs
        for worklog in worklogs:
            worklog_fields = []
            worklog_fields.append(current_issue.key)
            worklog_fields.append(worklog.issueId)
            worklog_fields.append(current_issue.fields.customfield_10202)
            worklog_fields.append(current_issue.fields.customfield_10198)
            worklog_fields.append(current_issue.fields.customfield_10191)
            worklog_fields.append(worklog.id)
            worklog_fields.append(worklog.author.displayName)
            worklog_fields.append(worklog.author.emailAddress)
            worklog_fields.append(functions.datetime_to_date(worklog.started))
            worklog_fields.append(functions.seconds_to_industryHours(worklog.timeSpentSeconds))
            worklog_fields.append(worklog.comment) <-----
            writer.writerow(worklog_fields)

我发现一些引用worklog.comment的东西应该可以工作,但是在运行我的代码时我总是收到以下错误:

Traceback (most recent call last):
  File "C:\Users\xy\AppData\Local\Programs\Python\Python312\Lib\site-packages\jira\resources.py", line 193, in __getattr__
    return self[item]  # type: ignore
           ~~~~^^^^^^
TypeError: 'Worklog' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\Development\Projects\Jira Interface\main.py", line 58, in <module>
    worklog_fields.append(worklog.comment)
                          ^^^^^^^^^^^^^^^
  File "C:\Users\xy\AppData\Local\Programs\Python\Python312\Lib\site-packages\jira\resources.py", line 198, in __getattr__
    raise AttributeError(
AttributeError: <class 'jira.resources.Worklog'> object has no attribute 'comment' ('Worklog' object is not subscriptable)

有什么想法如何获取每个工作日志的评论吗?

python jira-rest-api python-jira
1个回答
0
投票

找出问题所在: 代码正在运行,但我的项目中有一个没有评论的工作日志。因此,工作日志对象没有“注释”属性。

通过检查对象是否具有该属性,我的代码现在运行良好:

if hasattr(worklog, 'comment') and worklog.raw['comment']:
     print("Worklog comment: ", worklog.raw['comment'])
else:
     print("Worklog has an empty comment or no comment attribute.")
© www.soinside.com 2019 - 2024. All rights reserved.