Search code examples
pythonpython-3.xpygithub

Return only issues using PyGithub


I'm trying to get the number of issues in a repository, but the code below is returning issues and pull requests. How do I just get issues? I think I'm missing something simple here. I read the api documentation and it states GitHub treats all pull requests as issues.

repo = g.get_repo("someRepo")
label = repo.get_label('someLabel')
myIssues = repo.get_issues(state='open',labels=[label])
count = 0
for issue in myIssues:
    count = count + 1
print(count)

Solution

  • For issues which are just issues, the pull_request is None.

    >>> repo = g.get_repo("PyGithub/PyGithub")
    >>> open_issues = repo.get_issues(state='open')
    >>> for issue in open_issues:
    ...     print(issue)
    ...
    Issue(title="Correct Repository.create_git_tag_and_release()", number=1362)
    Issue(title="search_topics returns different number of repositories compared to searching in browser.", number=1352)
    >>> print(open_issues[0].pull_request)
    <github.IssuePullRequest.IssuePullRequest object at 0x7f320954cb70>
    >>> print(open_issues[1].pull_request)
    None
    >>>
    

    So you can count only those issues for which issue.pull_request is None.

    repo = g.get_repo("someRepo")
    label = repo.get_label('someLabel')
    myIssues = repo.get_issues(state='open',labels=[label])
    count = 0
    for issue in myIssues:
        if not issue.pull_request:
           count = count + 1
    print(count)
    

    You can also replace your count logic as follows:

    count = sum(not issue.pull_request for issue in myIssues)