Skip to content

Commit

Permalink
Merge pull request #234 from mathstuf/github-involved-issues
Browse files Browse the repository at this point in the history
GitHub involved issues
  • Loading branch information
ralphbean committed Sep 1, 2015
2 parents c4bbd95 + 67b93eb commit 6ff7cfc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
11 changes: 11 additions & 0 deletions bugwarrior/docs/services/github.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ by adding the following configuration option::

github.filter_pull_requests = True

Get involved issues
+++++++++++++++++++

Instead of fetching issues and pull requests based on ``{{username}}``'s owned
repositories, you may instead get those that ``{{username}}`` is involved in.
This includes all issues and pull requests where the user is the author, the
assignee, mentioned in, or has commented on. To do so, add the following
configuration option::

github.involved_issues = True

Provided UDA Fields
-------------------

Expand Down
24 changes: 22 additions & 2 deletions bugwarrior/services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ def __init__(self, *args, **kw):
self.filter_pull_requests = self.config_get_default(
'filter_pull_requests', default=False, to_type=asbool
)
self.involved_issues = self.config_get_default(
'involved_issues', default=False, to_type=asbool
)

@classmethod
def get_keyring_service(cls, config, section):
Expand All @@ -197,6 +200,18 @@ def get_owned_repo_issues(self, tag):
issues[issue['url']] = (tag, issue)
return issues

def get_involved_issues(self, user):
""" Grab all 'interesting' issues """
issues = {}
for issue in githubutils.get_involved_issues(user, auth=self.auth):
url = issue['html_url']
tag = re.match('.*github\\.com/(.*)/(issues|pull)/[^/]*$', url)
if tag is None:
log.name(self.target).critical(" Unrecognized issue URL: {0}.", url)
continue
issues[url] = (tag.group(1), issue)
return issues

def get_directly_assigned_issues(self):
project_matcher = re.compile(
r'.*/repos/(?P<owner>[^/]+)/(?P<project>[^/]+)/.*'
Expand Down Expand Up @@ -264,10 +279,15 @@ def issues(self):
repos = filter(self.filter_repos, all_repos)

issues = {}
for repo in repos:
if self.involved_issues:
issues.update(
self.get_owned_repo_issues(user + "/" + repo['name'])
self.get_involved_issues(user)
)
else:
for repo in repos:
issues.update(
self.get_owned_repo_issues(user + "/" + repo['name'])
)
issues.update(self.get_directly_assigned_issues())
log.name(self.target).debug(" Found {0} issues.", len(issues))
issues = filter(self.include, issues.values())
Expand Down
21 changes: 18 additions & 3 deletions bugwarrior/services/githubutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def get_repos(username, auth):
return _getter(url, auth)


def get_involved_issues(username, auth):
""" username should be a string
auth should be a tuple of username and password.
"""

tmpl = "https://api.github.com/search/issues?q=involves%3A{username}&per_page=100"
url = tmpl.format(username=username)
return _getter(url, auth, subkey='items')


def get_issues(username, repo, auth):
""" username and repo should be strings
auth should be a tuple of username and password.
Expand Down Expand Up @@ -73,7 +83,7 @@ def get_pulls(username, repo, auth):
return _getter(url, auth)


def _getter(url, auth):
def _getter(url, auth, subkey=None):
""" Pagination utility. Obnoxious. """

kwargs = {}
Expand All @@ -98,10 +108,15 @@ def _getter(url, auth):

if callable(response.json):
# Newer python-requests
results += response.json()
json_res = response.json()
else:
# Older python-requests
results += response.json
json_res = response.json

if subkey is not None:
json_res = json_res[subkey]

results += json_res

link = _link_field_to_dict(response.headers.get('link', None))

Expand Down

0 comments on commit 6ff7cfc

Please sign in to comment.