我正在try 通过使用jira-pony库在Python中从Jira Service Management Cloud检索工作日志(log)条目.

除了从每个工作日志(log)中获取 comments 之外,一切都运行良好.文件对此相当模糊.

我的代码目前看起来像这样:

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)

有什么 idea 如何获取每个工作日志(log)的 comments 吗?

推荐答案

解决问题: 代码正在工作,但我的项目中有一个工作日志(log)没有 comments .因此,工作日志(log)对象没有"Comment"属性.

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

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.")

Python相关问答推荐

如何使用没有Selenium的Python在百思买着陆页面上处理国家/地区 Select ?

Deliveryter Notebook -无法在for循环中更新matplotlib情节(保留之前的情节),也无法使用动画子功能对情节进行动画

_repr_html_实现自定义__getattr_时未显示

使用@ guardlasses. guardlass和注释的Python继承

NP.round解算数据后NP.unique

Julia CSV for Python中的等效性Pandas index_col参数

如何获取numpy数组的特定索引值?

pyscript中的压痕问题

对象的`__call__`方法的setattr在Python中不起作用'

把一个pandas文件夹从juyter笔记本放到堆栈溢出问题中的最快方法?

如何从数据库上传数据到html?

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

mypy无法推断类型参数.List和Iterable的区别

什么是合并两个embrame的最佳方法,其中一个有日期范围,另一个有日期没有任何共享列?

在Python中计算连续天数

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

提高算法效率的策略?

Autocad使用pyautocad/comtypes将对象从一个图形复制到另一个图形

Pandas:计数器的滚动和,复位