nemubot/modules/github.py

205 lines
6.4 KiB
Python
Raw Normal View History

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 json
import re
import urllib.error
from urllib.parse import quote
from urllib.request import urlopen
from hooks import hook
nemubotversion = 3.4
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-11-13 01:51:49 +00:00
raw = urlopen("https://api.github.com/search/repositories?q=%s" %
quote(repo), timeout=10)
2014-08-26 05:06:23 +00:00
return json.loads(raw.read().decode())
2014-11-13 01:51:49 +00:00
2014-08-26 05:06:23 +00:00
def info_user(username):
2014-11-13 01:51:49 +00:00
raw = urlopen("https://api.github.com/users/%s" % quote(username),
timeout=10)
2014-08-26 05:06:23 +00:00
user = json.loads(raw.read().decode())
2014-11-13 01:51:49 +00:00
raw = urlopen("https://api.github.com/users/%s/repos?sort=updated" %
quote(username), timeout=10)
2014-08-26 05:06:23 +00:00
user["repos"] = json.loads(raw.read().decode())
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
try:
if issue is not None:
2014-11-13 01:51:49 +00:00
raw = urlopen("https://api.github.com/repos/%s/issues/%s" %
(quote(fullname), quote(issue)), timeout=10)
return [json.loads(raw.read().decode())]
2014-08-26 05:06:23 +00:00
else:
2014-11-13 01:51:49 +00:00
raw = urlopen("https://api.github.com/repos/%s/issues?sort=updated"
% quote(fullname), timeout=10)
2014-08-26 05:06:23 +00:00
return json.loads(raw.read().decode())
except urllib.error.HTTPError:
raise IRCException("Repository not found")
2014-11-13 01:51:49 +00:00
def info_commit(repo, commit=None):
rp = info_repos(repo)
if rp["items"]:
fullname = rp["items"][0]["full_name"]
else:
fullname = repo
try:
if commit is not None:
2014-11-13 01:51:49 +00:00
raw = urlopen("https://api.github.com/repos/%s/commits/%s" %
(quote(fullname), quote(commit)), timeout=10)
return [json.loads(raw.read().decode())]
else:
2014-11-13 01:51:49 +00:00
raw = urlopen("https://api.github.com/repos/%s/commits" %
quote(fullname), timeout=10)
return json.loads(raw.read().decode())
except urllib.error.HTTPError:
raise IRCException("Repository not found")
2014-08-26 05:06:23 +00:00
@hook("cmd_hook", "github")
def cmd_github(msg):
if len(msg.cmds) < 2:
raise IRCException("indicate a repository name to search")
repos = info_repos(" ".join(msg.cmds[1:]))
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):
if len(msg.cmds) < 2:
raise IRCException("indicate a user name to search")
res = Response(channel=msg.channel, nomore="No more user")
2014-08-26 05:06:23 +00:00
user = info_user(" ".join(msg.cmds[1:]))
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):
if len(msg.cmds) < 2:
raise IRCException("indicate a repository to view its issues")
issue = None
if len(msg.cmds) > 2:
li = re.match("^#?([0-9]+)$", msg.cmds[1])
ri = re.match("^#?([0-9]+)$", msg.cmds[-1])
if li is not None:
issue = msg.cmds[1]
del msg.cmds[1]
elif ri is not None:
issue = msg.cmds[-1]
del msg.cmds[-1]
2014-08-26 05:06:23 +00:00
repo = " ".join(msg.cmds[1:])
count = " (%d more issues)" if issue is None else None
res = Response(channel=msg.channel, nomore="No more issue", count=count)
2014-08-26 05:06:23 +00:00
issues = info_issue(repo, issue)
for issue in issues:
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):
if len(msg.cmds) < 2:
raise IRCException("indicate a repository to view its commits")
commit = None
if len(msg.cmds) > 2:
if re.match("^[a-fA-F0-9]+$", msg.cmds[1]):
commit = msg.cmds[1]
del msg.cmds[1]
elif re.match("^[a-fA-F0-9]+$", msg.cmds[-1]):
commit = msg.cmds[-1]
del msg.cmds[-1]
repo = " ".join(msg.cmds[1:])
count = " (%d more commits)" if commit is None else None
res = Response(channel=msg.channel, nomore="No more commit", count=count)
commits = info_commit(repo, commit)
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