2018-11-30 21:54:51 +00:00
|
|
|
import itertools, json, urllib.parse
|
2018-10-05 22:29:59 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2018-11-30 21:53:47 +00:00
|
|
|
FORM_ENCODED = "application/x-www-form-urlencoded"
|
|
|
|
|
2018-10-26 10:25:28 +00:00
|
|
|
COMMIT_URL = "https://github.com/%s/commit/%s"
|
2018-11-17 22:07:32 +00:00
|
|
|
COMMIT_RANGE_URL = "https://github.com/%s/compare/%s...%s"
|
2018-11-17 20:59:24 +00:00
|
|
|
CREATE_URL = "https://github.com/%s/tree/%s"
|
2018-10-26 10:25:28 +00:00
|
|
|
|
2018-11-17 22:32:44 +00:00
|
|
|
DEFAULT_EVENTS = [
|
|
|
|
"push",
|
|
|
|
"commit_comment",
|
|
|
|
"pull_request",
|
|
|
|
"pull_request_review",
|
|
|
|
"pull_request_review_comment",
|
|
|
|
"issue_comment",
|
|
|
|
"issues",
|
|
|
|
"create",
|
|
|
|
"delete",
|
2018-12-10 18:47:54 +00:00
|
|
|
"release",
|
|
|
|
"fork"
|
2018-11-17 22:32:44 +00:00
|
|
|
]
|
|
|
|
|
2018-11-06 17:23:27 +00:00
|
|
|
COMMENT_ACTIONS = {
|
2018-11-06 17:05:40 +00:00
|
|
|
"created": "commented",
|
|
|
|
"edited": "edited a comment",
|
|
|
|
"deleted": "deleted a comment"
|
|
|
|
}
|
|
|
|
|
2018-10-06 08:54:21 +00:00
|
|
|
@utils.export("channelset", {"setting": "github-hook",
|
|
|
|
"help": ("Disable/Enable showing BitBot's github commits in the "
|
2018-12-05 12:13:29 +00:00
|
|
|
"current channel"), "array": True})
|
2018-11-08 17:48:23 +00:00
|
|
|
@utils.export("channelset", {"setting": "github-hide-prefix",
|
|
|
|
"help": "Hide/show command-like prefix on Github hook outputs",
|
|
|
|
"validate": utils.bool_or_none})
|
2018-10-05 22:29:59 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
@utils.hook("api.post.github")
|
|
|
|
def github(self, event):
|
2018-11-30 21:53:47 +00:00
|
|
|
payload = event["data"].decode("utf8")
|
2018-12-06 12:01:23 +00:00
|
|
|
if event["headers"]["Content-Type"] == FORM_ENCODED:
|
2018-11-30 21:53:47 +00:00
|
|
|
payload = urllib.parse.parse_qs(urllib.parse.unquote(payload)
|
|
|
|
)["payload"][0]
|
|
|
|
data = json.loads(payload)
|
2018-10-05 22:29:59 +00:00
|
|
|
|
2018-12-08 09:00:12 +00:00
|
|
|
github_event = event["headers"]["X-GitHub-Event"]
|
2018-11-06 16:08:02 +00:00
|
|
|
if github_event == "ping":
|
2018-12-08 09:10:44 +00:00
|
|
|
return ""
|
2018-11-06 16:08:02 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
full_name = data["repository"]["full_name"]
|
|
|
|
hooks = self.bot.database.channel_settings.find_by_setting(
|
|
|
|
"github-hook")
|
2018-11-17 22:32:44 +00:00
|
|
|
targets = []
|
|
|
|
|
2018-11-18 10:25:32 +00:00
|
|
|
repo_hooked = False
|
2018-11-17 22:32:44 +00:00
|
|
|
for i, (server_id, channel_name, hooked_repos) in list(
|
2018-11-06 14:01:30 +00:00
|
|
|
enumerate(hooks))[::-1]:
|
2018-11-17 22:32:44 +00:00
|
|
|
if full_name in hooked_repos:
|
2018-11-18 10:25:32 +00:00
|
|
|
repo_hooked = True
|
2018-11-17 22:32:44 +00:00
|
|
|
server = self.bot.get_server(server_id)
|
2018-11-17 22:33:31 +00:00
|
|
|
if server and channel_name in server.channels:
|
2018-11-17 22:34:51 +00:00
|
|
|
channel = server.channels.get(channel_name)
|
2018-11-17 22:32:44 +00:00
|
|
|
github_events = channel.get_setting("github-events",
|
|
|
|
DEFAULT_EVENTS)
|
|
|
|
if github_event in github_events:
|
2018-11-17 22:35:32 +00:00
|
|
|
targets.append([server, channel])
|
2018-11-17 22:32:44 +00:00
|
|
|
|
|
|
|
if not targets:
|
2018-12-08 09:10:44 +00:00
|
|
|
return "" if repo_hooked else None
|
2018-11-06 13:43:06 +00:00
|
|
|
|
|
|
|
outputs = None
|
|
|
|
if github_event == "push":
|
|
|
|
outputs = self.push(event, full_name, data)
|
|
|
|
elif github_event == "commit_comment":
|
|
|
|
outputs = self.commit_comment(event, full_name, data)
|
|
|
|
elif github_event == "pull_request":
|
|
|
|
outputs = self.pull_request(event, full_name, data)
|
|
|
|
elif github_event == "pull_request_review":
|
|
|
|
outputs = self.pull_request_review(event, full_name, data)
|
|
|
|
elif github_event == "pull_request_review_comment":
|
|
|
|
outputs = self.pull_request_review_comment(event, full_name, data)
|
|
|
|
elif github_event == "issue_comment":
|
|
|
|
outputs = self.issue_comment(event, full_name, data)
|
|
|
|
elif github_event == "issues":
|
|
|
|
outputs = self.issues(event, full_name, data)
|
2018-11-17 20:59:24 +00:00
|
|
|
elif github_event == "create":
|
|
|
|
outputs = self.create(event, full_name, data)
|
2018-11-17 21:10:03 +00:00
|
|
|
elif github_event == "delete":
|
|
|
|
outputs = self.delete(event, full_name, data)
|
2018-11-17 21:21:32 +00:00
|
|
|
elif github_event == "release":
|
|
|
|
outputs = self.release(event, full_name, data)
|
2018-12-10 17:17:40 +00:00
|
|
|
elif github_event == "status":
|
|
|
|
outputs = self.status(event, full_name, data)
|
2018-12-10 18:47:54 +00:00
|
|
|
elif github_event == "fork":
|
|
|
|
outputs = self.fork(event, full_name, data)
|
2018-11-06 13:43:06 +00:00
|
|
|
|
|
|
|
if outputs:
|
2018-11-17 22:39:14 +00:00
|
|
|
for server, channel in targets:
|
2018-11-06 14:02:03 +00:00
|
|
|
for output in outputs:
|
2018-11-17 21:41:27 +00:00
|
|
|
output = "(%s) %s" % (full_name, output)
|
2018-11-27 14:25:12 +00:00
|
|
|
self.events.on("send.stdout").call(target=channel,
|
2018-11-27 15:07:22 +00:00
|
|
|
module_name="Github", server=server, message=output,
|
2018-11-27 14:25:12 +00:00
|
|
|
hide_prefix=channel.get_setting(
|
|
|
|
"github-hide-prefix", False))
|
2018-10-06 08:54:21 +00:00
|
|
|
|
2018-12-08 09:10:44 +00:00
|
|
|
return ""
|
2018-11-06 14:09:13 +00:00
|
|
|
|
2018-12-11 22:27:04 +00:00
|
|
|
def _short_url(self, url):
|
|
|
|
page = utils.http.get_url("https://git.io", method="POST",
|
|
|
|
post_data={"url": url})
|
|
|
|
return page.headers["Location"]
|
|
|
|
|
2018-11-16 20:39:37 +00:00
|
|
|
def _change_count(self, n, symbol, color):
|
2018-11-17 08:21:40 +00:00
|
|
|
return utils.irc.color("%s%d" % (symbol, n), color)+utils.irc.bold("")
|
2018-11-16 20:39:37 +00:00
|
|
|
def _added(self, n):
|
2018-11-17 08:20:34 +00:00
|
|
|
return self._change_count(n, "+", utils.consts.GREEN)
|
2018-11-16 20:39:37 +00:00
|
|
|
def _removed(self, n):
|
2018-11-17 08:20:34 +00:00
|
|
|
return self._change_count(n, "-", utils.consts.RED)
|
2018-11-16 20:39:37 +00:00
|
|
|
def _modified(self, n):
|
2018-11-26 14:47:47 +00:00
|
|
|
return self._change_count(n, "~", utils.consts.PURPLE)
|
2018-11-16 20:39:37 +00:00
|
|
|
|
|
|
|
def _short_hash(self, hash):
|
|
|
|
return hash[:8]
|
|
|
|
|
2018-11-17 08:15:28 +00:00
|
|
|
def _flat_unique(self, commits, key):
|
|
|
|
return set(itertools.chain(*(commit[key] for commit in commits)))
|
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
def push(self, event, full_name, data):
|
|
|
|
outputs = []
|
2018-11-18 10:27:19 +00:00
|
|
|
branch = data["ref"].split("/", 2)[2]
|
2018-11-20 12:40:26 +00:00
|
|
|
branch = utils.irc.color(branch, utils.consts.LIGHTBLUE)
|
2018-11-18 10:27:19 +00:00
|
|
|
|
2018-11-17 20:59:24 +00:00
|
|
|
if len(data["commits"]) <= 3:
|
2018-11-16 20:23:34 +00:00
|
|
|
for commit in data["commits"]:
|
2018-11-16 20:39:37 +00:00
|
|
|
id = self._short_hash(commit["id"])
|
2018-11-17 20:33:23 +00:00
|
|
|
message = commit["message"].split("\n")[0].strip()
|
2018-11-16 20:23:34 +00:00
|
|
|
author = commit["author"]["name"] or commit["author"]["login"]
|
|
|
|
author = utils.irc.bold(author)
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(COMMIT_URL % (full_name, id))
|
2018-11-06 13:43:06 +00:00
|
|
|
|
2018-11-16 20:39:37 +00:00
|
|
|
added = self._added(len(commit["added"]))
|
|
|
|
removed = self._removed(len(commit["removed"]))
|
2018-11-17 08:22:39 +00:00
|
|
|
modified = self._modified(len(commit["modified"]))
|
2018-11-12 08:44:15 +00:00
|
|
|
|
2018-11-17 21:44:33 +00:00
|
|
|
outputs.append("[%s/%s/%s files] commit by %s to %s: %s - %s"
|
|
|
|
% (added, removed, modified, author, branch, message, url))
|
2018-11-16 20:39:37 +00:00
|
|
|
else:
|
2018-11-17 08:28:48 +00:00
|
|
|
first_id = self._short_hash(data["before"])
|
2018-11-16 20:39:37 +00:00
|
|
|
last_id = self._short_hash(data["commits"][-1]["id"])
|
2018-11-17 09:40:44 +00:00
|
|
|
pusher = utils.irc.bold(data["pusher"]["name"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(
|
|
|
|
COMMIT_RANGE_URL % (full_name, first_id, last_id))
|
2018-11-16 20:39:37 +00:00
|
|
|
|
2018-11-17 08:15:28 +00:00
|
|
|
commits = data["commits"]
|
|
|
|
added = self._added(len(self._flat_unique(commits, "added")))
|
|
|
|
removed = self._removed(len(self._flat_unique(commits, "removed")))
|
2018-11-17 08:24:31 +00:00
|
|
|
modified = self._modified(len(self._flat_unique(commits,
|
2018-11-17 08:15:28 +00:00
|
|
|
"modified")))
|
|
|
|
|
2018-11-17 21:44:33 +00:00
|
|
|
outputs.append("[%s/%s/%s files] %s pushed %d commits to %s - %s"
|
|
|
|
% (added, removed, modified, pusher, len(data["commits"]),
|
|
|
|
branch, url))
|
2018-11-12 08:35:16 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
return outputs
|
|
|
|
|
2018-11-06 17:05:40 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
def commit_comment(self, event, full_name, data):
|
|
|
|
action = data["action"]
|
|
|
|
commit = data["commit_id"][:8]
|
2018-11-12 17:16:17 +00:00
|
|
|
commenter = utils.irc.bold(data["comment"]["user"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["comment"]["html_url"])
|
2018-11-17 21:44:52 +00:00
|
|
|
return ["[commit/%s] %s commented" % (commit, commenter, action)]
|
2018-11-06 13:43:06 +00:00
|
|
|
|
|
|
|
def pull_request(self, event, full_name, data):
|
|
|
|
action = data["action"]
|
2018-11-07 08:23:07 +00:00
|
|
|
action_desc = action
|
2018-11-20 10:27:28 +00:00
|
|
|
branch = data["pull_request"]["base"]["ref"]
|
|
|
|
colored_branch = utils.irc.color(branch, utils.consts.LIGHTBLUE)
|
|
|
|
|
|
|
|
if action == "opened":
|
|
|
|
action_desc = "requested merge into %s" % colored_branch
|
|
|
|
elif action == "closed":
|
2018-11-07 11:47:54 +00:00
|
|
|
if data["pull_request"]["merged"]:
|
2018-11-19 20:56:42 +00:00
|
|
|
action_desc = "%s into %s" % (
|
|
|
|
utils.irc.color("merged", utils.consts.GREEN),
|
2018-11-20 10:27:28 +00:00
|
|
|
colored_branch)
|
2018-11-07 11:47:54 +00:00
|
|
|
else:
|
|
|
|
action_desc = utils.irc.color("closed without merging",
|
2018-11-13 16:02:26 +00:00
|
|
|
utils.consts.RED)
|
2018-11-15 07:06:20 +00:00
|
|
|
elif action == "synchronize":
|
|
|
|
action_desc = "committed to"
|
2018-12-09 19:36:23 +00:00
|
|
|
elif action == "labeled":
|
2018-12-09 20:57:29 +00:00
|
|
|
action_desc = "labeled as '%s'" % data["label"]["name"]
|
|
|
|
elif action == "unlabeled":
|
|
|
|
action_desc = "unlabeled as '%s'" % data["label"]["name"]
|
2018-11-07 08:23:07 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
pr_title = data["pull_request"]["title"]
|
2018-11-12 17:16:17 +00:00
|
|
|
author = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["pull_request"]["html_url"])
|
2018-11-17 21:44:52 +00:00
|
|
|
return ["[pr] %s %s: %s - %s" % (author, action_desc, pr_title, url)]
|
2018-11-06 13:43:06 +00:00
|
|
|
|
|
|
|
def pull_request_review(self, event, full_name, data):
|
2018-11-07 19:21:59 +00:00
|
|
|
if data["review"]["state"] == "commented":
|
|
|
|
return []
|
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
|
|
|
pr_title = data["pull_request"]["title"]
|
2018-12-06 18:35:46 +00:00
|
|
|
reviewer = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["review"]["html_url"])
|
2018-11-17 21:44:52 +00:00
|
|
|
return ["[pr] %s %s a review on: %s - %s" % (reviewer, action, pr_title,
|
|
|
|
url)]
|
2018-11-06 13:43:06 +00:00
|
|
|
|
|
|
|
def pull_request_review_comment(self, event, full_name, data):
|
|
|
|
action = data["action"]
|
|
|
|
pr_title = data["pull_request"]["title"]
|
|
|
|
commenter = data["comment"]["user"]["login"]
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["comment"]["html_url"])
|
2018-11-17 21:41:27 +00:00
|
|
|
return ["[pr] %s %s on a review: %s - %s" %
|
|
|
|
(commenter, COMMENT_ACTIONS[action], pr_title, url)]
|
2018-11-06 13:43:06 +00:00
|
|
|
|
|
|
|
def issues(self, event, full_name, data):
|
|
|
|
action = data["action"]
|
2018-12-09 20:57:29 +00:00
|
|
|
action_desc = action
|
|
|
|
if action == "labeled":
|
|
|
|
action_desc = "labeled as '%s'" % data["label"]["name"]
|
|
|
|
elif action == "unlabeled":
|
|
|
|
action_desc = "unlabeled as '%s'" % data["label"]["name"]
|
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
issue_title = data["issue"]["title"]
|
2018-11-12 17:16:17 +00:00
|
|
|
author = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["issue"]["html_url"])
|
2018-12-10 21:07:38 +00:00
|
|
|
return ["[issue] %s %s: %s - %s" %
|
|
|
|
(author, action_desc, issue_title, url)]
|
2018-11-06 13:43:06 +00:00
|
|
|
def issue_comment(self, event, full_name, data):
|
|
|
|
action = data["action"]
|
2018-11-06 15:01:02 +00:00
|
|
|
issue_title = data["issue"]["title"]
|
|
|
|
type = "pr" if "pull_request" in data["issue"] else "issue"
|
2018-11-12 17:16:17 +00:00
|
|
|
commenter = utils.irc.bold(data["comment"]["user"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["comment"]["html_url"])
|
2018-11-17 21:41:27 +00:00
|
|
|
return ["[%s] %s %s on: %s - %s" %
|
|
|
|
(type, commenter, COMMENT_ACTIONS[action], issue_title,
|
2018-11-07 19:27:22 +00:00
|
|
|
url)]
|
2018-11-17 20:59:24 +00:00
|
|
|
|
|
|
|
def create(self, event, full_name, data):
|
2018-11-19 21:36:24 +00:00
|
|
|
ref = data["ref"]
|
2018-11-20 12:40:26 +00:00
|
|
|
ref_color = utils.irc.color(ref, utils.consts.LIGHTBLUE)
|
2018-11-19 21:34:36 +00:00
|
|
|
type = data["ref_type"]
|
2018-11-17 21:06:27 +00:00
|
|
|
sender = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(CREATE_URL % (full_name, ref))
|
2018-11-19 21:36:24 +00:00
|
|
|
return ["%s created a %s: %s - %s" % (sender, type, ref_color, url)]
|
2018-11-17 21:10:03 +00:00
|
|
|
|
|
|
|
def delete(self, event, full_name, data):
|
|
|
|
ref = data["ref"]
|
|
|
|
type = data["ref_type"]
|
|
|
|
sender = utils.irc.bold(data["sender"]["login"])
|
2018-11-17 21:44:52 +00:00
|
|
|
return ["%s deleted a %s: %s" % (sender, type, ref)]
|
2018-11-17 21:21:32 +00:00
|
|
|
|
|
|
|
def release(self, event, full_name, data):
|
|
|
|
action = data["action"]
|
2018-11-17 22:17:14 +00:00
|
|
|
tag = data["release"]["tag_name"]
|
2018-11-17 21:21:32 +00:00
|
|
|
name = data["release"]["name"] or ""
|
|
|
|
if name:
|
|
|
|
name = ": %s"
|
|
|
|
author = utils.irc.bold(data["release"]["author"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["release"]["html_url"])
|
2018-11-17 21:44:52 +00:00
|
|
|
return ["%s %s a release%s - %s" % (author, action, name, url)]
|
2018-12-10 17:17:40 +00:00
|
|
|
|
|
|
|
def status(self, event, full_name, data):
|
|
|
|
context = data["context"]
|
|
|
|
state = data["state"]
|
|
|
|
url = data["target_url"]
|
|
|
|
commit = self._short_id(data["sha"])
|
|
|
|
return ["[%s status] %s is '%s' - %s" %
|
|
|
|
(commit, context, state, url)]
|
2018-12-10 18:47:54 +00:00
|
|
|
|
|
|
|
def fork(self, event, full_name, data):
|
|
|
|
forker = utils.irc.bold(data["sender"]["login"])
|
|
|
|
fork_full_name = utils.irc.color(data["forkee"]["full_name"],
|
|
|
|
utils.consts.LIGHTBLUE)
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["forkee"]["html_url"])
|
2018-12-10 18:47:54 +00:00
|
|
|
return ["%s forked into %s - %s" %
|
|
|
|
(forker, fork_full_name, url)]
|