bitbot-3.11-fork/modules/github.py

159 lines
6.2 KiB
Python
Raw Normal View History

import json
from src import ModuleManager, utils
COMMIT_URL = "https://github.com/%s/commit/%s"
COMMENT_ACTIONS = {
"created": "commented",
"edited": "edited a comment",
"deleted": "deleted a comment"
}
@utils.export("channelset", {"setting": "github-hook",
"help": ("Disable/Enable showing BitBot's github commits in the "
"current channel")})
@utils.export("channelset", {"setting": "github-hide-prefix",
"help": "Hide/show command-like prefix on Github hook outputs",
"validate": utils.bool_or_none})
class Module(ModuleManager.BaseModule):
@utils.hook("api.post.github")
def github(self, event):
data = json.loads(event["data"])
2018-11-06 16:08:02 +00:00
github_event = event["headers"]["X-GitHub-Event"]
if github_event == "ping":
return True
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:
hooks.pop(i)
if not hooks:
return None
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:
for output in outputs:
server = self.bot.get_server(server_id)
channel = server.channels.get(channel_name)
trigger = self._make_trigger(channel, server, output)
self.bot.trigger(trigger)
return True
def _make_trigger(self, channel, server, line):
return lambda: self.events.on("send.stdout").call(target=channel,
module_name="Github", server=server, message=line,
hide_prefix=channel.get_setting("github-hide-prefix", False))
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"])
2018-11-12 17:16:17 +00:00
author = utils.irc.bold(author)
url = COMMIT_URL % (full_name, id[:8])
added = utils.irc.color("+%d" % len(commit["added"]),
2018-11-13 16:02:26 +00:00
utils.consts.GREEN)
added = added+utils.irc.bold("")
removed = utils.irc.color("-%d" % len(commit["removed"]),
2018-11-13 16:02:26 +00:00
utils.consts.RED)
removed = removed+utils.irc.bold("")
modified = utils.irc.color("±%d" % len(commit["modified"]),
2018-11-13 16:02:26 +00:00
utils.consts.PURPLE)
2018-11-12 08:48:21 +00:00
outputs.append("(%s) [%s/%s/%s files] commit by '%s': %s - %s"
% (full_name, added, removed, modified, author, message, url))
return outputs
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"])
url = data["comment"]["html_url"]
return ["(%s) [commit/%s] %s commented" %
(full_name, commit, commenter, action)]
def pull_request(self, event, full_name, data):
action = data["action"]
action_desc = action
if action == "closed":
if data["pull_request"]["merged"]:
2018-11-13 16:02:26 +00:00
action_desc = utils.irc.color("merged", utils.consts.GREEN)
else:
action_desc = utils.irc.color("closed without merging",
2018-11-13 16:02:26 +00:00
utils.consts.RED)
action_desc = utils.irc.bold(action_desc)
pr_title = data["pull_request"]["title"]
2018-11-12 17:16:17 +00:00
author = utils.irc.bold(data["sender"]["login"])
url = data["pull_request"]["html_url"]
return ["(%s) [pr] %s %s: %s - %s" %
(full_name, author, action_desc, pr_title, url)]
def pull_request_review(self, event, full_name, data):
if data["review"]["state"] == "commented":
return []
action = data["action"]
pr_title = data["pull_request"]["title"]
2018-11-12 17:16:17 +00:00
reviewer = utils.irc.bold(data["review"]["user"]["login"])
url = data["review"]["html_url"]
return ["(%s) [pr] %s %s a review on: %s - %s" %
(full_name, reviewer, action, pr_title, url)]
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"]
return ["(%s) [pr] %s %s on a review: %s - %s" %
(full_name, commenter, COMMENT_ACTIONS[action], pr_title, url)]
def issues(self, event, full_name, data):
action = data["action"]
issue_title = data["issue"]["title"]
2018-11-12 17:16:17 +00:00
author = utils.irc.bold(data["sender"]["login"])
url = data["issue"]["html_url"]
return ["(%s) [issue] %s %s: %s - %s" %
(full_name, author, action, issue_title, url)]
def issue_comment(self, event, full_name, data):
action = data["action"]
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"])
url = data["comment"]["html_url"]
return ["(%s) [%s] %s %s on: %s - %s" %
(full_name, type, commenter, COMMENT_ACTIONS[action], issue_title,
url)]