尝试使用 PyGithub 库访问 Github 问题评论

trying to access Github issue comments using PyGithub library

我正在尝试使用 PyGithub 库访问问题评论。

这是我实现的功能,

    def get_issue_comments_dict(self, repository):
        """
        get issue comments
        outputs reponame: issue title, issue url, comments
        Return type: dict 
        """

        repo = self.user_obj.get_repo(repository)
        issues = repo.get_issues()
        issues_dict = {}
        i = 1
        for issue in issues:
            issue_dict = {}
            issue_dict['url'] = issue.url
            issue_dict['title'] = issue.title
            issue_dict['comments'] = [comment for comment in
                    issue.get_issue_comments()]
            issues_dict[i] = issue_dict
            i += 1
        return issues_dict

这就是我遇到的错误。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "GithubWrapper.py", line 164, in get_issue_comments_dict
    issue.get_issue_comments()]
AttributeError: 'Issue' object has no attribute 'get_issue_comments'

我做错了什么?

好的,首先,一个M最小的R可生产的E示例问题是:

import github

gh = github.Github()
repo = gh.get_repo('PyGithub/PyGithub')
for issue in repo.get_issues():
    comments = issue.get_issue_comments()

这导致:

AttributeError: 'Issue' object has no attribute 'get_issue_comments'

如何解决这个问题?

Python 从字面上告诉您 Issue 对象没有名为 get_issue_comments 的方法(或任何属性)。显然你调用了错误的方法。

那么你怎么知道哪些方法可用呢?我同意 documentation (在撰写本文时)非常有限。您还有许多其他选择:

使用帮助()

对于任何具有适当文档字符串的 Python 对象(模块、class、方法等),内置的 help() 函数确实非常有用;- )

issue = repo.get_issues()[0]
help(issue)

这将打印:

Help on Issue in module github.Issue object:

class Issue(github.GithubObject.CompletableGithubObject)
 |  Issue(requester, headers, attributes, completed)
 |
 |  This class represents Issues. The reference can be found here https://developer.github.com/v3/issues/
 |
 |  Method resolution order:
 |      Issue
 |      github.GithubObject.CompletableGithubObject
 |      github.GithubObject.GithubObject
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  __repr__(self)
 |      Return repr(self).
 |
...
 |
 |  get_comments(self, since=NotSet)
 |      :calls: `GET /repos/:owner/:repo/issues/:number/comments <http://developer.github.com/v3/issues/comments>`_
 |      :param since: datetime.datetime format YYYY-MM-DDTHH:MM:SSZ
 |      :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.IssueComment.IssueComment`
 |
...

如您所见,class 有详细的文档,显然它包含一个方法 'get_comments',您可以使用它。

使用 dir()

您还可以使用内置函数 dir():

查看对象包含哪些属性(例如方法)
issue = repo.get_issues()[0]
print(dir(issue))  # in an interactive shell you don't have to print()

这将打印:

['CHECK_AFTER_INIT_FLAG', '_CompletableGithubObject__complete', '_CompletableGithubObject__completed', '_GithubObject__makeSimpleAttribute', '_GithubObject__makeSimpleListAttribute', '_GithubObject__makeTransformedAttribute', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_assignee', '_assignees', '_body', '_closed_at', '_closed_by', '_comments', '_comments_url', '_completeIfNeeded', '_completeIfNotSet', '_created_at', '_events_url', '_headers', '_html_url', '_id', '_identity', '_initAttributes', '_labels', '_labels_url', '_locked', '_makeBoolAttribute', '_makeClassAttribute', '_makeDatetimeAttribute', '_makeDictAttribute', '_makeDictOfStringsToClassesAttribute', '_makeIntAttribute', '_makeListOfClassesAttribute', '_makeListOfDictsAttribute', '_makeListOfIntsAttribute', '_makeListOfListOfStringsAttribute', '_makeListOfStringsAttribute', '_makeStringAttribute', '_makeTimestampAttribute', '_milestone', '_number', '_parentUrl', '_pull_request', '_rawData', '_repository', '_requester', '_state', '_storeAndUseAttributes', '_title', '_updated_at', '_url', '_useAttributes', '_user', 'active_lock_reason', 'add_to_assignees', 'add_to_labels', 'as_pull_request', 'assignee', 'assignees', 'body', 'closed_at', 'closed_by', 'comments', 'comments_url', 'create_comment', 'create_reaction', 'created_at', 'delete_labels', 'edit', 'etag', 'events_url', 'get__repr__', 'get_comment', 'get_comments', 'get_events', 'get_labels', 'get_reactions', 'html_url', 'id', 'labels', 'labels_url', 'last_modified', 'lock', 'locked', 'milestone', 'number', 'pull_request', 'raw_data', 'raw_headers', 'remove_from_assignees', 'remove_from_labels', 'repository', 'setCheckAfterInitFlag', 'set_labels', 'state', 'title', 'unlock', 'update', 'updated_at', 'url', 'user']

在这里您还会看到它不包含名称 'get_issue_comments',但 确实 包含名称 'get_comments'.

解决方案

更改以下行:

issue_dict['comments'] = [comment for comment in issue.get_issue_comments()]

至:

issue_dict['comments'] = [comment for comment in issue.get_comments()]

对于那些自己看不到评论的人,要获得评论,您应该获得 正文 条评论,如下所示:

issues['comment'] = [comment.body for comment in issue.get_comments()]