2014-08-26 05:06:23 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-08-27 23:39:31 +00:00
|
|
|
"""Repositories, users or issues on GitHub"""
|
|
|
|
|
2014-08-26 05:06:23 +00:00
|
|
|
import re
|
|
|
|
from urllib.parse import quote
|
|
|
|
|
2015-01-04 22:57:09 +00:00
|
|
|
from nemubot.exception import IRCException
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.hooks import hook
|
|
|
|
from nemubot.tools import web
|
2014-08-26 05:06:23 +00:00
|
|
|
|
|
|
|
nemubotversion = 3.4
|
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2014-08-26 05:06:23 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
def help_full():
|
|
|
|
return ("!github /repo/: Display information about /repo/.\n"
|
|
|
|
"!github_user /user/: Display information about /user/.")
|
2014-08-26 05:06:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def info_repos(repo):
|
2014-12-17 06:32:34 +00:00
|
|
|
return web.getJSON("https://api.github.com/search/repositories?q=%s" %
|
|
|
|
quote(repo), timeout=10)
|
2014-08-26 05:06:23 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-08-26 05:06:23 +00:00
|
|
|
def info_user(username):
|
2014-12-17 06:32:34 +00:00
|
|
|
user = web.getJSON("https://api.github.com/users/%s" % quote(username),
|
|
|
|
timeout=10)
|
2014-08-26 05:06:23 +00:00
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
user["repos"] = web.getJSON("https://api.github.com/users/%s/"
|
|
|
|
"repos?sort=updated" % quote(username),
|
|
|
|
timeout=10)
|
2014-08-26 05:06:23 +00:00
|
|
|
|
|
|
|
return user
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-08-26 05:06:23 +00:00
|
|
|
def info_issue(repo, issue=None):
|
|
|
|
rp = info_repos(repo)
|
|
|
|
if rp["items"]:
|
|
|
|
fullname = rp["items"][0]["full_name"]
|
|
|
|
else:
|
|
|
|
fullname = repo
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
if issue is not None:
|
|
|
|
return [web.getJSON("https://api.github.com/repos/%s/issues/%s" %
|
|
|
|
(quote(fullname), quote(issue)))]
|
|
|
|
else:
|
|
|
|
return web.getJSON("https://api.github.com/repos/%s/issues?"
|
|
|
|
"sort=updated" % quote(fullname))
|
2014-08-26 05:06:23 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-08-28 12:28:56 +00:00
|
|
|
def info_commit(repo, commit=None):
|
|
|
|
rp = info_repos(repo)
|
|
|
|
if rp["items"]:
|
|
|
|
fullname = rp["items"][0]["full_name"]
|
|
|
|
else:
|
|
|
|
fullname = repo
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
if commit is not None:
|
|
|
|
return [web.getJSON("https://api.github.com/repos/%s/commits/%s" %
|
|
|
|
(quote(fullname), quote(commit)))]
|
|
|
|
else:
|
|
|
|
return web.getJSON("https://api.github.com/repos/%s/commits" %
|
|
|
|
quote(fullname))
|
2014-08-28 12:28:56 +00:00
|
|
|
|
2014-08-26 05:06:23 +00:00
|
|
|
|
|
|
|
@hook("cmd_hook", "github")
|
|
|
|
def cmd_github(msg):
|
2015-06-01 15:06:13 +00:00
|
|
|
if not len(msg.args):
|
2014-08-26 05:06:23 +00:00
|
|
|
raise IRCException("indicate a repository name to search")
|
|
|
|
|
2015-06-01 15:06:13 +00:00
|
|
|
repos = info_repos(" ".join(msg.args))
|
2014-08-26 05:06:23 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
res = Response(channel=msg.channel,
|
|
|
|
nomore="No more repository",
|
|
|
|
count=" (%d more repo)")
|
2014-08-26 05:06:23 +00:00
|
|
|
|
|
|
|
for repo in repos["items"]:
|
|
|
|
homepage = ""
|
|
|
|
if repo["homepage"] is not None:
|
|
|
|
homepage = repo["homepage"] + " - "
|
2014-11-13 01:51:49 +00:00
|
|
|
res.append_message("Repository %s: %s%s Main language: %s; %d forks; %d stars; %d watchers; %d opened_issues; view it at %s" %
|
|
|
|
(repo["full_name"],
|
|
|
|
homepage,
|
|
|
|
repo["description"],
|
|
|
|
repo["language"], repo["forks"],
|
|
|
|
repo["stargazers_count"],
|
|
|
|
repo["watchers_count"],
|
|
|
|
repo["open_issues_count"],
|
|
|
|
repo["html_url"]))
|
2014-08-26 05:06:23 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-08-26 05:06:23 +00:00
|
|
|
@hook("cmd_hook", "github_user")
|
|
|
|
def cmd_github(msg):
|
2015-06-01 15:06:13 +00:00
|
|
|
if not len(msg.args):
|
2014-08-26 05:06:23 +00:00
|
|
|
raise IRCException("indicate a user name to search")
|
|
|
|
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel, nomore="No more user")
|
2014-08-26 05:06:23 +00:00
|
|
|
|
2015-06-01 15:06:13 +00:00
|
|
|
user = info_user(" ".join(msg.args))
|
2014-08-26 05:06:23 +00:00
|
|
|
|
|
|
|
if "login" in user:
|
|
|
|
if user["repos"]:
|
2014-11-13 01:51:49 +00:00
|
|
|
kf = (" Known for: " +
|
|
|
|
", ".join([repo["name"] for repo in user["repos"]]))
|
2014-08-26 05:06:23 +00:00
|
|
|
else:
|
|
|
|
kf = ""
|
|
|
|
if "name" in user:
|
|
|
|
name = user["name"]
|
|
|
|
else:
|
|
|
|
name = user["login"]
|
2014-11-13 01:51:49 +00:00
|
|
|
res.append_message("User %s: %d public repositories; %d public gists; %d followers; %d following; view it at %s.%s" %
|
|
|
|
(name,
|
|
|
|
user["public_repos"],
|
|
|
|
user["public_gists"],
|
|
|
|
user["followers"],
|
|
|
|
user["following"],
|
|
|
|
user["html_url"],
|
|
|
|
kf))
|
2014-08-26 05:06:23 +00:00
|
|
|
else:
|
|
|
|
raise IRCException("User not found")
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
@hook("cmd_hook", "github_issue")
|
|
|
|
def cmd_github(msg):
|
2015-06-01 15:06:13 +00:00
|
|
|
if not len(msg.args):
|
2014-08-28 12:28:56 +00:00
|
|
|
raise IRCException("indicate a repository to view its issues")
|
|
|
|
|
|
|
|
issue = None
|
2015-06-01 15:06:13 +00:00
|
|
|
|
|
|
|
li = re.match("^#?([0-9]+)$", msg.args[0])
|
|
|
|
ri = re.match("^#?([0-9]+)$", msg.args[-1])
|
|
|
|
if li is not None:
|
2015-06-01 15:14:54 +00:00
|
|
|
issue = li.group(1)
|
2015-06-01 15:06:13 +00:00
|
|
|
del msg.args[0]
|
|
|
|
elif ri is not None:
|
2015-06-01 15:14:54 +00:00
|
|
|
issue = ri.group(1)
|
2015-06-01 15:06:13 +00:00
|
|
|
del msg.args[-1]
|
|
|
|
|
|
|
|
repo = " ".join(msg.args)
|
2014-08-26 05:06:23 +00:00
|
|
|
|
2014-08-28 12:28:56 +00:00
|
|
|
count = " (%d more issues)" if issue is None else None
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel, nomore="No more issue", count=count)
|
2014-08-26 05:06:23 +00:00
|
|
|
|
|
|
|
issues = info_issue(repo, issue)
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
if issues is None:
|
|
|
|
raise IRCException("Repository not found")
|
|
|
|
|
2014-08-26 05:06:23 +00:00
|
|
|
for issue in issues:
|
2014-08-28 12:28:56 +00:00
|
|
|
res.append_message("%s%s issue #%d: \x03\x02%s\x03\x02 opened by %s on %s: %s" %
|
|
|
|
(issue["state"][0].upper(),
|
|
|
|
issue["state"][1:],
|
|
|
|
issue["number"],
|
|
|
|
issue["title"],
|
|
|
|
issue["user"]["login"],
|
|
|
|
issue["created_at"],
|
|
|
|
issue["body"].replace("\n", " ")))
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
@hook("cmd_hook", "github_commit")
|
|
|
|
def cmd_github(msg):
|
2015-06-01 15:06:13 +00:00
|
|
|
if not len(msg.args):
|
2014-08-28 12:28:56 +00:00
|
|
|
raise IRCException("indicate a repository to view its commits")
|
|
|
|
|
|
|
|
commit = None
|
2015-06-01 15:06:13 +00:00
|
|
|
if re.match("^[a-fA-F0-9]+$", msg.args[0]):
|
|
|
|
commit = msg.args[0]
|
|
|
|
del msg.args[0]
|
|
|
|
elif re.match("^[a-fA-F0-9]+$", msg.args[-1]):
|
|
|
|
commit = msg.args[-1]
|
|
|
|
del msg.args[-1]
|
|
|
|
|
|
|
|
repo = " ".join(msg.args)
|
2014-08-28 12:28:56 +00:00
|
|
|
|
|
|
|
count = " (%d more commits)" if commit is None else None
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel, nomore="No more commit", count=count)
|
2014-08-28 12:28:56 +00:00
|
|
|
|
|
|
|
commits = info_commit(repo, commit)
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
if commits is None:
|
|
|
|
raise IRCException("Repository not found")
|
|
|
|
|
2014-08-28 12:28:56 +00:00
|
|
|
for commit in commits:
|
2014-11-13 01:51:49 +00:00
|
|
|
res.append_message("Commit %s by %s on %s: %s" %
|
|
|
|
(commit["sha"][:10],
|
|
|
|
commit["commit"]["author"]["name"],
|
|
|
|
commit["commit"]["author"]["date"],
|
|
|
|
commit["commit"]["message"].replace("\n", " ")))
|
2014-08-26 05:06:23 +00:00
|
|
|
return res
|