Skip to content

Commit

Permalink
Let .pulls accept just a username.
Browse files Browse the repository at this point in the history
Before this, it needed both a username and a repo.  Check this out:

```
@threebean │ .pulls
  threebot │ threebean: (pulls <username[/repo]>) -- List the pending pull requests on github repos.
@threebean │ .pulls what/is/this
  threebot │ threebean: Must be GitHub repo of the format <username/repo> or just <username>
@threebean │ .pulls lmacken
  threebot │ threebean: One moment, please...  Looking up lmacken.
  threebot │ threebean: @msabramo's "README.rst: syntax highlight + tweaks" lmacken/tbgrep#8 filed
           │ 4 days ago
  threebot │ threebean: @Qalthos's "Update action_hooks" lmacken/pyramid-openshift-quickstart#7
           │ filed a year ago                                                                                            e
@threebean │ .pulls fedora-infra
  threebot │ threebean: One moment, please...
  threebot │ threebean: @ralphbean's "Handle timezones." fedora-infra/bugzilla2fedmsg#4 filed 41
           │ minutes ago
  threebot │ threebean: @ralphbean's "Remove unused imports." fedora-infra/fedmsg#300 filed 2 days
           │ ago
  threebot │ threebean: @pypingou's "Let the unorphan action call the unorphan API endpoint"
           │ fedora-infra/packagedb-cli#21 filed 3 days ago
  threebot │ threebean: @ralphbean's "Getting ready for the 1.2.0 release." fedora-infra/kitchen#5
           │ filed 4 days ago
  threebot │ threebean: ... and 6 more.
@threebean │ .pulls fedora-infra/fedmsg
  threebot │ threebean: @ralphbean's "Remove unused imports." fedora-infra/fedmsg#300 filed 2 days
           │ ago
```
  • Loading branch information
ralphbean committed Nov 17, 2014
1 parent e014656 commit 090860d
Showing 1 changed file with 60 additions and 27 deletions.
87 changes: 60 additions & 27 deletions plugin.py
Expand Up @@ -226,31 +226,77 @@ def _load_json(self, url):
return json

def pulls(self, irc, msg, args, slug):
"""<username/repo>
"""<username[/repo]>
List the pending pull requests on a github repo.
List the latest pending pull requests on github repos.
"""

if slug.count('/') != 1:
irc.reply('Must be GitHub repo of the format <username/repo>')
if not slug.strip():
irc.reply('Must be GitHub repo of the format '
'<username/repo> or just <username>')
elif slug.count('/') == 0:
username, repo = slug.strip(), None
elif slug.count('/') == 1:
username, repo = slug.strip().split('/')
else:
irc.reply('Must be GitHub repo of the format '
'<username/repo> or just <username>')
return

username, repo = slug.strip().split('/')
if repo:
repos = [repo]
else:
irc.reply('One moment, please... Looking up %s.' % username)
repos = list(self.yield_github_repos(username))

results = sum([
list(self.yield_github_pulls(username, repo)) for repo in repos
], [])

# Reverse-sort by time (newest-first)
def comparator(a, b):
return cmp(arrow.get(b['created_at']), arrow.get(a['created_at']))
results.sort(comparator)

if not results:
irc.reply('No pending pull requests on {slug}'.format(slug=slug))
else:
n = 4
for pull in results[:n]:
irc.reply('@{user}\'s "{title}" {url} filed {age}'.format(
user=pull['user']['login'],
title=pull['title'],
url=pull['html_url'],
age=arrow.get(pull['created_at']).humanize(),
))

if len(results) > n:
irc.reply('... and %i more.' % (len(results) - n))
pulls = wrap(pulls, ['text'])

def yield_github_repos(self, username):
self.log.info("Finding github repos for %r" % username)
tmpl = "https://api.github.com/users/{username}/repos?per_page=100"
url = tmpl.format(username=username)
auth = dict(access_token=self.github_oauth_token)
for result in self.yield_github_results(url, auth):
yield result['name']

def yield_github_pulls(self, username, repo):
self.log.info("Finding github pull requests for %r %r" % (username, repo))
tmpl = "https://api.github.com/repos/{username}/{repo}/" + \
"pulls?per_page=100"
url = tmpl.format(username=username, repo=repo)
auth = dict(access_token=self.github_oauth_token)
for result in self.yield_github_results(url, auth):
yield result

def yield_github_results(self, url, auth):
results = []
link = dict(next=url)
while 'next' in link:
response = requests.get(link['next'], params=auth)

if response.status_code == 404:
irc.reply('No such GitHub repository %r' % slug)
return

# And.. if we didn't get good results, just bail.
if response.status_code != 200:
raise IOError(
Expand All @@ -259,10 +305,13 @@ def pulls(self, irc, msg, args, slug):

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

for result in results:
yield result

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

Expand All @@ -275,22 +324,6 @@ def pulls(self, irc, msg, args, slug):
) for part in field.split(', ')
])

if not results:
irc.reply('No pending pull requests on {slug}'.format(slug=slug))
else:
n = 4
for pull in results[:n]:
irc.reply('@{user}\'s "{title}" {url}'.format(
user=pull['user']['login'],
title=pull['title'],
url=pull['html_url']))

if len(results) > n:
irc.reply('... and %i more.' % (len(results) - n))


pulls = wrap(pulls, ['text'])

def whoowns(self, irc, msg, args, package):
"""<package>
Expand Down

0 comments on commit 090860d

Please sign in to comment.