4

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)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
DBS
  • 1,107
  • 1
  • 12
  • 24
  • Hi. Is there anything about a pull-request that would distinguish it by a non-pull request, data wise? – GetSet Jan 17 '20 at 23:27
  • 1
    I see this note in the GitHub docs: GitHub's REST API v3 considers every pull request an issue, but not every issue is a pull request. For this reason, "Issues" endpoints may return both issues and pull requests in the response. You can identify pull requests by the pull_request key. – DBS Jan 17 '20 at 23:44
  • That sounds promising for your particular concerns – GetSet Jan 17 '20 at 23:45

1 Answers1

2

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)
Shashank V
  • 10,007
  • 2
  • 25
  • 41