2018-10-05 22:29:59 +00:00
|
|
|
import json
|
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2018-10-26 10:25:28 +00:00
|
|
|
COMMIT_URL = "https://github.com/%s/commit/%s"
|
|
|
|
|
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-11-06 13:01:30 +00:00
|
|
|
"current channel"), "hidden": True})
|
2018-10-05 22:29:59 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
@utils.hook("api.post.github")
|
|
|
|
def github(self, event):
|
2018-11-06 14:08:35 +00:00
|
|
|
data = json.loads(event["data"])
|
2018-10-05 22:29:59 +00:00
|
|
|
|
2018-11-06 16:08:02 +00:00
|
|
|
github_event = event["headers"]["X-GitHub-Event"]
|
|
|
|
if github_event == "ping":
|
|
|
|
return True
|
|
|
|
|
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-06 14:01:30 +00:00
|
|
|
for i, (server_id, channel_name, values) in list(
|
|
|
|
enumerate(hooks))[::-1]:
|
|
|
|
if not full_name in values:
|
2018-11-06 13:43:06 +00:00
|
|
|
hooks.pop(i)
|
2018-11-06 14:01:49 +00:00
|
|
|
if not hooks:
|
2018-11-06 14:09:13 +00:00
|
|
|
return 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)
|
|
|
|
|
|
|
|
if outputs:
|
|
|
|
for server_id, channel_name, _ in hooks:
|
2018-11-06 14:02:03 +00:00
|
|
|
for output in outputs:
|
2018-10-06 08:54:21 +00:00
|
|
|
server = self.bot.get_server(server_id)
|
|
|
|
channel = server.get_channel(channel_name)
|
2018-11-06 13:43:06 +00:00
|
|
|
trigger = self._make_trigger(channel, server, output)
|
|
|
|
self.bot.trigger(trigger)
|
2018-10-06 08:54:21 +00:00
|
|
|
|
2018-11-06 14:09:13 +00:00
|
|
|
return True
|
|
|
|
|
2018-10-07 07:10:52 +00:00
|
|
|
def _make_trigger(self, channel, server, line):
|
2018-11-07 11:27:34 +00:00
|
|
|
return lambda: self.events.on("send.stdout").call(target=channel,
|
|
|
|
module_name="Github", server=server, message=line, hide_prefix=True)
|
2018-11-06 13:43:06 +00:00
|
|
|
|
|
|
|
def push(self, event, full_name, data):
|
|
|
|
outputs = []
|
|
|
|
for commit in data["commits"]:
|
|
|
|
id = commit["id"]
|
|
|
|
|
|
|
|
message = commit["message"].split("\n")
|
|
|
|
message = [line.strip() for line in message]
|
|
|
|
message = " ".join(message)
|
|
|
|
|
|
|
|
author = "%s <%s>" % (commit["author"]["username"],
|
|
|
|
commit["author"]["email"])
|
|
|
|
modified_count = len(commit["modified"])
|
|
|
|
added_count = len(commit["added"])
|
|
|
|
removed_count = len(commit["removed"])
|
|
|
|
url = COMMIT_URL % (full_name, id[:8])
|
|
|
|
|
|
|
|
outputs.append("(%s) [files: +%d ∆%d -%d] commit by '%s': %s - %s"
|
|
|
|
% (full_name, added_count, modified_count, removed_count,
|
|
|
|
author, message, url))
|
|
|
|
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]
|
|
|
|
commenter = data["comment"]["user"]["login"]
|
|
|
|
url = data["comment"]["html_url"]
|
2018-11-06 17:05:40 +00:00
|
|
|
return ["(%s) [commit/%s] %s commented" %
|
2018-11-06 13:43:06 +00:00
|
|
|
(full_name, commit, commenter, action)]
|
|
|
|
|
|
|
|
def pull_request(self, event, full_name, data):
|
|
|
|
action = data["action"]
|
2018-11-07 08:23:07 +00:00
|
|
|
action_desc = action
|
|
|
|
if action == "closed":
|
2018-11-07 11:47:54 +00:00
|
|
|
if data["pull_request"]["merged"]:
|
|
|
|
action_desc = utils.irc.color("merged", utils.irc.COLOR_GREEN)
|
|
|
|
else:
|
|
|
|
action_desc = utils.irc.color("closed without merging",
|
|
|
|
utils.irc.COLOR_RED)
|
2018-11-07 08:23:07 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
pr_title = data["pull_request"]["title"]
|
|
|
|
author = data["sender"]["login"]
|
|
|
|
url = data["pull_request"]["html_url"]
|
2018-11-07 19:27:22 +00:00
|
|
|
return ["(%s) [pr] %s %s: %s - %s" %
|
|
|
|
(full_name, 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"]
|
|
|
|
reviewer = data["review"]["user"]["login"]
|
|
|
|
url = data["review"]["html_url"]
|
2018-11-07 19:27:22 +00:00
|
|
|
return ["(%s) [pr] %s %s a review on: %s - %s" %
|
|
|
|
(full_name, 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"]
|
|
|
|
url = data["comment"]["html_url"]
|
2018-11-07 19:27:22 +00:00
|
|
|
return ["(%s) [pr] %s %s on a review: %s - %s" %
|
|
|
|
(full_name, 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"]
|
|
|
|
issue_title = data["issue"]["title"]
|
|
|
|
author = data["sender"]["login"]
|
|
|
|
url = data["issue"]["html_url"]
|
2018-11-07 19:27:22 +00:00
|
|
|
return ["(%s) [issue] %s %s: %s - %s" %
|
|
|
|
(full_name, author, action, 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-06 13:43:06 +00:00
|
|
|
commenter = data["comment"]["user"]["login"]
|
|
|
|
url = data["comment"]["html_url"]
|
2018-11-07 19:27:22 +00:00
|
|
|
return ["(%s) [%s] %s %s on: %s - %s" %
|
|
|
|
(full_name, type, commenter, COMMENT_ACTIONS[action], issue_title,
|
|
|
|
url)]
|