Fix typos, missed imports, wrong namespaces, etc
This commit is contained in:
parent
14f2fd6a03
commit
5e51086cdd
2 changed files with 66 additions and 138 deletions
|
@ -1,5 +1,8 @@
|
||||||
|
import itertools, json
|
||||||
from src import ModuleManager, utils
|
from src import ModuleManager, utils
|
||||||
from . import github
|
from . import colors, github
|
||||||
|
|
||||||
|
FORM_ENCODED = "application/x-www-form-urlencoded"
|
||||||
|
|
||||||
@utils.export("channelset", {"setting": "git-prevent-highlight",
|
@utils.export("channelset", {"setting": "git-prevent-highlight",
|
||||||
"help": "Enable/disable preventing highlights",
|
"help": "Enable/disable preventing highlights",
|
||||||
|
@ -15,22 +18,22 @@ class Module(ModuleManager.BaseModule):
|
||||||
self._github = github.GitHub()
|
self._github = github.GitHub()
|
||||||
|
|
||||||
@utils.hook("api.post.github")
|
@utils.hook("api.post.github")
|
||||||
def github_webhook(self, event):
|
def _api_github_webhook(self, event):
|
||||||
return self._webhook("github", "GitHub", self._github,
|
return self._webhook("github", "GitHub", self._github,
|
||||||
event["data"], event["headers"])
|
event["data"], event["headers"])
|
||||||
|
|
||||||
def _webhook(self, webhook_type, webhook_name, handler, payload_str,
|
def _webhook(self, webhook_type, webhook_name, handler, payload_str,
|
||||||
headers):
|
headers):
|
||||||
payload = event["data"].decode("utf8")
|
payload = payload_str.decode("utf8")
|
||||||
if event["headers"]["Content-Type"] == FORM_ENCODED:
|
if headers["Content-Type"] == FORM_ENCODED:
|
||||||
payload = urllib.parse.unquote(urllib.parse.parse_qs(payload)[
|
payload = urllib.parse.unquote(urllib.parse.parse_qs(payload)[
|
||||||
"payload"][0])
|
"payload"][0])
|
||||||
data = json.loads(payload)
|
data = json.loads(payload)
|
||||||
|
|
||||||
full_name, repo_username, report_name, organisation = handler.names(
|
full_name, repo_username, repo_name, organisation = handler.names(
|
||||||
data, headers)
|
data, headers)
|
||||||
branch = handler.branch(data, headers)
|
branch = handler.branch(data, headers)
|
||||||
event, event_action = handler.event(data, headers)
|
current_event, event_action = handler.event(data, headers)
|
||||||
|
|
||||||
hooks = self.bot.database.channel_settings.find_by_setting(
|
hooks = self.bot.database.channel_settings.find_by_setting(
|
||||||
"%s-hooks" % webhook_type)
|
"%s-hooks" % webhook_type)
|
||||||
|
@ -46,6 +49,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
found_hook = hooked_repos[repo_username]
|
found_hook = hooked_repos[repo_username]
|
||||||
elif organisation and organisation in hooked_repos:
|
elif organisation and organisation in hooked_repos:
|
||||||
found_hook = hooked_repos[organisation]
|
found_hook = hooked_repos[organisation]
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
repo_hooked = True
|
repo_hooked = True
|
||||||
server = self.bot.get_server_by_id(server_id)
|
server = self.bot.get_server_by_id(server_id)
|
||||||
|
@ -57,8 +62,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
|
|
||||||
events = []
|
events = []
|
||||||
for hooked_event in found_hook["events"]:
|
for hooked_event in found_hook["events"]:
|
||||||
events.append(EVENT_CATEGORIES.get(hooked_event,
|
events.append(handler.event_categories(hooked_event))
|
||||||
[hooked_event]))
|
|
||||||
events = list(itertools.chain(*events))
|
events = list(itertools.chain(*events))
|
||||||
|
|
||||||
channel = server.channels.get(channel_name)
|
channel = server.channels.get(channel_name)
|
||||||
|
@ -72,7 +76,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
else:
|
else:
|
||||||
return {"state": "success", "deliveries": 0}
|
return {"state": "success", "deliveries": 0}
|
||||||
|
|
||||||
outputs = handler.webhook(data, headers)
|
outputs = handler.webhook(full_name, current_event, data, headers)
|
||||||
|
|
||||||
if outputs:
|
if outputs:
|
||||||
for server, channel in targets:
|
for server, channel in targets:
|
||||||
|
@ -83,7 +87,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
|
|
||||||
for output in outputs:
|
for output in outputs:
|
||||||
output = "(%s) %s" % (
|
output = "(%s) %s" % (
|
||||||
utils.irc.color(source, COLOR_REPO), output)
|
utils.irc.color(source, colors.COLOR_REPO), output)
|
||||||
|
|
||||||
if channel.get_setting("git-prevent-highlight", False):
|
if channel.get_setting("git-prevent-highlight", False):
|
||||||
output = self._prevent_highlight(server, channel,
|
output = self._prevent_highlight(server, channel,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#--depends-on-github
|
#--depends-on-github
|
||||||
from src import ModuleManager, utils
|
from src import ModuleManager, utils
|
||||||
|
from . import colors
|
||||||
FORM_ENCODED = "application/x-www-form-urlencoded"
|
|
||||||
|
|
||||||
COMMIT_URL = "https://github.com/%s/commit/%s"
|
COMMIT_URL = "https://github.com/%s/commit/%s"
|
||||||
COMMIT_RANGE_URL = "https://github.com/%s/compare/%s...%s"
|
COMMIT_RANGE_URL = "https://github.com/%s/compare/%s...%s"
|
||||||
|
@ -93,7 +92,7 @@ class GitHub(object):
|
||||||
organisation = None
|
organisation = None
|
||||||
if "organization" in data:
|
if "organization" in data:
|
||||||
organisation = data["organization"]["login"]
|
organisation = data["organization"]["login"]
|
||||||
return full_name, repo_username, repo_name, oraganisation
|
return full_name, repo_username, repo_name, organisation
|
||||||
|
|
||||||
def branch(self, data, headers):
|
def branch(self, data, headers):
|
||||||
if "ref" in data:
|
if "ref" in data:
|
||||||
|
@ -107,115 +106,40 @@ class GitHub(object):
|
||||||
event_action = "%s/%s" % (event, data["action"])
|
event_action = "%s/%s" % (event, data["action"])
|
||||||
return event, event_action
|
return event, event_action
|
||||||
|
|
||||||
def webhook(self, data, headers):
|
def event_categories(self, event):
|
||||||
github_event = headers["X-GitHub-Event"]
|
return EVENT_CATEGORIES.get(event, [event])
|
||||||
|
|
||||||
full_name = None
|
|
||||||
repo_username = None
|
|
||||||
repo_name = None
|
|
||||||
if "repository" in data:
|
|
||||||
full_name = data["repository"]["full_name"]
|
|
||||||
repo_username, repo_name = full_name.split("/", 1)
|
|
||||||
|
|
||||||
organisation = None
|
|
||||||
if "organization" in data:
|
|
||||||
organisation = data["organization"]["login"]
|
|
||||||
|
|
||||||
event_action = None
|
|
||||||
if "action" in data:
|
|
||||||
event_action = "%s/%s" % (github_event, data["action"])
|
|
||||||
|
|
||||||
branch = None
|
|
||||||
if "ref" in data:
|
|
||||||
_, _, branch = data["ref"].rpartition("/")
|
|
||||||
|
|
||||||
hooks = self.bot.database.channel_settings.find_by_setting(
|
|
||||||
"github-hooks")
|
|
||||||
targets = []
|
|
||||||
|
|
||||||
repo_hooked = False
|
|
||||||
for server_id, channel_name, hooked_repos in hooks:
|
|
||||||
found_hook = None
|
|
||||||
if full_name and full_name in hooked_repos:
|
|
||||||
found_hook = hooked_repos[full_name]
|
|
||||||
elif repo_username and repo_username in hooked_repos:
|
|
||||||
found_hook = hooked_repos[repo_username]
|
|
||||||
elif organisation and organisation in hooked_repos:
|
|
||||||
found_hook = hooked_repos[organisation]
|
|
||||||
|
|
||||||
if found_hook:
|
|
||||||
repo_hooked = True
|
|
||||||
server = self.bot.get_server_by_id(server_id)
|
|
||||||
if server and channel_name in server.channels:
|
|
||||||
if (branch and
|
|
||||||
found_hook["branches"] and
|
|
||||||
not branch in found_hook["branches"]):
|
|
||||||
continue
|
|
||||||
|
|
||||||
github_events = []
|
|
||||||
for hooked_event in found_hook["events"]:
|
|
||||||
github_events.append(EVENT_CATEGORIES.get(
|
|
||||||
hooked_event, [hooked_event]))
|
|
||||||
github_events = list(itertools.chain(*github_events))
|
|
||||||
|
|
||||||
channel = server.channels.get(channel_name)
|
|
||||||
if (github_event in github_events or
|
|
||||||
(event_action and event_action in github_events)):
|
|
||||||
targets.append([server, channel])
|
|
||||||
|
|
||||||
if not targets:
|
|
||||||
if not repo_hooked:
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
return {"state": "success", "deliveries": 0}
|
|
||||||
|
|
||||||
outputs = None
|
|
||||||
if github_event == "push":
|
|
||||||
outputs = self.push(full_name, data)
|
|
||||||
elif github_event == "commit_comment":
|
|
||||||
outputs = self.commit_comment(full_name, data)
|
|
||||||
elif github_event == "pull_request":
|
|
||||||
outputs = self.pull_request(full_name, data)
|
|
||||||
elif github_event == "pull_request_review":
|
|
||||||
outputs = self.pull_request_review(full_name, data)
|
|
||||||
elif github_event == "pull_request_review_comment":
|
|
||||||
outputs = self.pull_request_review_comment(full_name, data)
|
|
||||||
elif github_event == "issue_comment":
|
|
||||||
outputs = self.issue_comment(full_name, data)
|
|
||||||
elif github_event == "issues":
|
|
||||||
outputs = self.issues(full_name, data)
|
|
||||||
elif github_event == "create":
|
|
||||||
outputs = self.create(full_name, data)
|
|
||||||
elif github_event == "delete":
|
|
||||||
outputs = self.delete(full_name, data)
|
|
||||||
elif github_event == "release":
|
|
||||||
outputs = self.release(full_name, data)
|
|
||||||
elif github_event == "check_run":
|
|
||||||
outputs = self.check_run(data)
|
|
||||||
elif github_event == "fork":
|
|
||||||
outputs = self.fork(full_name, data)
|
|
||||||
elif github_event == "ping":
|
|
||||||
outputs = self.ping(data)
|
|
||||||
elif github_event == "membership":
|
|
||||||
outputs = self.membership(organisation, data)
|
|
||||||
elif github_event == "watch":
|
|
||||||
outputs = self.watch(data)
|
|
||||||
return outputs
|
|
||||||
|
|
||||||
def _prevent_highlight(self, server, channel, s):
|
|
||||||
for user in channel.users:
|
|
||||||
if len(user.nickname) == 1:
|
|
||||||
# if we don't ignore 1-letter nicknames, the below while loop
|
|
||||||
# will fire indefininitely.
|
|
||||||
continue
|
|
||||||
|
|
||||||
regex = re.compile(r"(.)\b(%s)(%s)" % (
|
|
||||||
re.escape(user.nickname[0]), re.escape(user.nickname[1:])),
|
|
||||||
re.I)
|
|
||||||
s = regex.sub("\\1\\2\u200c\\3", s)
|
|
||||||
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
def webhook(self, full_name, event, data, headers):
|
||||||
|
if event == "push":
|
||||||
|
return self.push(full_name, data)
|
||||||
|
elif event == "commit_comment":
|
||||||
|
return self.commit_comment(full_name, data)
|
||||||
|
elif event == "pull_request":
|
||||||
|
return self.pull_request(full_name, data)
|
||||||
|
elif event == "pull_request_review":
|
||||||
|
return self.pull_request_review(full_name, data)
|
||||||
|
elif event == "pull_request_review_comment":
|
||||||
|
return self.pull_request_review_comment(full_name, data)
|
||||||
|
elif event == "issue_comment":
|
||||||
|
return self.issue_comment(full_name, data)
|
||||||
|
elif event == "issues":
|
||||||
|
return self.issues(full_name, data)
|
||||||
|
elif event == "create":
|
||||||
|
return self.create(full_name, data)
|
||||||
|
elif event == "delete":
|
||||||
|
return self.delete(full_name, data)
|
||||||
|
elif event == "release":
|
||||||
|
return self.release(full_name, data)
|
||||||
|
elif event == "check_run":
|
||||||
|
return self.check_run(data)
|
||||||
|
elif event == "fork":
|
||||||
|
return self.fork(full_name, data)
|
||||||
|
elif event == "ping":
|
||||||
|
return self.ping(data)
|
||||||
|
elif event == "membership":
|
||||||
|
return self.membership(organisation, data)
|
||||||
|
elif event == "watch":
|
||||||
|
return self.watch(data)
|
||||||
def _short_url(self, url):
|
def _short_url(self, url):
|
||||||
try:
|
try:
|
||||||
page = utils.http.request("https://git.io", method="POST",
|
page = utils.http.request("https://git.io", method="POST",
|
||||||
|
@ -235,9 +159,9 @@ class GitHub(object):
|
||||||
def _change_count(self, n, symbol, color):
|
def _change_count(self, n, symbol, color):
|
||||||
return utils.irc.color("%s%d" % (symbol, n), color)+utils.irc.bold("")
|
return utils.irc.color("%s%d" % (symbol, n), color)+utils.irc.bold("")
|
||||||
def _added(self, n):
|
def _added(self, n):
|
||||||
return self._change_count(n, "+", COLOR_POSITIVE)
|
return self._change_count(n, "+", colors.COLOR_POSITIVE)
|
||||||
def _removed(self, n):
|
def _removed(self, n):
|
||||||
return self._change_count(n, "-", COLOR_NEGATIVE)
|
return self._change_count(n, "-", colors.COLOR_NEGATIVE)
|
||||||
def _modified(self, n):
|
def _modified(self, n):
|
||||||
return self._change_count(n, "~", utils.consts.PURPLE)
|
return self._change_count(n, "~", utils.consts.PURPLE)
|
||||||
|
|
||||||
|
@ -250,7 +174,7 @@ class GitHub(object):
|
||||||
def push(self, full_name, data):
|
def push(self, full_name, data):
|
||||||
outputs = []
|
outputs = []
|
||||||
branch = data["ref"].split("/", 2)[2]
|
branch = data["ref"].split("/", 2)[2]
|
||||||
branch = utils.irc.color(branch, COLOR_BRANCH)
|
branch = utils.irc.color(branch, colors.COLOR_BRANCH)
|
||||||
author = utils.irc.bold(data["pusher"]["name"])
|
author = utils.irc.bold(data["pusher"]["name"])
|
||||||
|
|
||||||
forced = ""
|
forced = ""
|
||||||
|
@ -263,7 +187,7 @@ class GitHub(object):
|
||||||
elif len(data["commits"]) <= 3:
|
elif len(data["commits"]) <= 3:
|
||||||
for commit in data["commits"]:
|
for commit in data["commits"]:
|
||||||
hash = commit["id"]
|
hash = commit["id"]
|
||||||
hash_colored = utils.irc.color(self._short_hash(hash), COLOR_ID)
|
hash_colored = utils.irc.color(self._short_hash(hash), colors.COLOR_ID)
|
||||||
message = commit["message"].split("\n")[0].strip()
|
message = commit["message"].split("\n")[0].strip()
|
||||||
url = self._short_url(COMMIT_URL % (full_name, hash))
|
url = self._short_url(COMMIT_URL % (full_name, hash))
|
||||||
|
|
||||||
|
@ -291,11 +215,11 @@ class GitHub(object):
|
||||||
|
|
||||||
def pull_request(self, full_name, data):
|
def pull_request(self, full_name, data):
|
||||||
number = utils.irc.color("#%s" % data["pull_request"]["number"],
|
number = utils.irc.color("#%s" % data["pull_request"]["number"],
|
||||||
COLOR_ID)
|
colors.COLOR_ID)
|
||||||
action = data["action"]
|
action = data["action"]
|
||||||
action_desc = "%s %s" % (action, number)
|
action_desc = "%s %s" % (action, number)
|
||||||
branch = data["pull_request"]["base"]["ref"]
|
branch = data["pull_request"]["base"]["ref"]
|
||||||
colored_branch = utils.irc.color(branch, COLOR_BRANCH)
|
colored_branch = utils.irc.color(branch, colors.COLOR_BRANCH)
|
||||||
|
|
||||||
if action == "opened":
|
if action == "opened":
|
||||||
action_desc = "requested %s merge into %s" % (number,
|
action_desc = "requested %s merge into %s" % (number,
|
||||||
|
@ -303,11 +227,11 @@ class GitHub(object):
|
||||||
elif action == "closed":
|
elif action == "closed":
|
||||||
if data["pull_request"]["merged"]:
|
if data["pull_request"]["merged"]:
|
||||||
action_desc = "%s %s into %s" % (
|
action_desc = "%s %s into %s" % (
|
||||||
utils.irc.color("merged", COLOR_POSITIVE), number,
|
utils.irc.color("merged", colors.COLOR_POSITIVE), number,
|
||||||
colored_branch)
|
colored_branch)
|
||||||
else:
|
else:
|
||||||
action_desc = "%s %s" % (
|
action_desc = "%s %s" % (
|
||||||
utils.irc.color("closed", COLOR_NEGATIVE), number)
|
utils.irc.color("closed", colors.COLOR_NEGATIVE), number)
|
||||||
elif action == "ready_for_review":
|
elif action == "ready_for_review":
|
||||||
action_desc = "marked %s ready for review" % number
|
action_desc = "marked %s ready for review" % number
|
||||||
elif action == "synchronize":
|
elif action == "synchronize":
|
||||||
|
@ -331,7 +255,7 @@ class GitHub(object):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
number = utils.irc.color("#%s" % data["pull_request"]["number"],
|
number = utils.irc.color("#%s" % data["pull_request"]["number"],
|
||||||
COLOR_ID)
|
colors.COLOR_ID)
|
||||||
action = data["action"]
|
action = data["action"]
|
||||||
pr_title = data["pull_request"]["title"]
|
pr_title = data["pull_request"]["title"]
|
||||||
reviewer = utils.irc.bold(data["sender"]["login"])
|
reviewer = utils.irc.bold(data["sender"]["login"])
|
||||||
|
@ -350,7 +274,7 @@ class GitHub(object):
|
||||||
|
|
||||||
def pull_request_review_comment(self, full_name, data):
|
def pull_request_review_comment(self, full_name, data):
|
||||||
number = utils.irc.color("#%s" % data["pull_request"]["number"],
|
number = utils.irc.color("#%s" % data["pull_request"]["number"],
|
||||||
COLOR_ID)
|
colors.COLOR_ID)
|
||||||
action = data["action"]
|
action = data["action"]
|
||||||
pr_title = data["pull_request"]["title"]
|
pr_title = data["pull_request"]["title"]
|
||||||
sender = utils.irc.bold(data["sender"]["login"])
|
sender = utils.irc.bold(data["sender"]["login"])
|
||||||
|
@ -359,7 +283,7 @@ class GitHub(object):
|
||||||
(sender, COMMENT_ACTIONS[action], number, pr_title, url)]
|
(sender, COMMENT_ACTIONS[action], number, pr_title, url)]
|
||||||
|
|
||||||
def issues(self, full_name, data):
|
def issues(self, full_name, data):
|
||||||
number = utils.irc.color("#%s" % data["issue"]["number"], COLOR_ID)
|
number = utils.irc.color("#%s" % data["issue"]["number"], colors.COLOR_ID)
|
||||||
action = data["action"]
|
action = data["action"]
|
||||||
issue_title = data["issue"]["title"]
|
issue_title = data["issue"]["title"]
|
||||||
author = utils.irc.bold(data["sender"]["login"])
|
author = utils.irc.bold(data["sender"]["login"])
|
||||||
|
@ -372,7 +296,7 @@ class GitHub(object):
|
||||||
if data["changes"]["body"]["from"] == data["comment"]["body"]:
|
if data["changes"]["body"]["from"] == data["comment"]["body"]:
|
||||||
return
|
return
|
||||||
|
|
||||||
number = utils.irc.color("#%s" % data["issue"]["number"], COLOR_ID)
|
number = utils.irc.color("#%s" % data["issue"]["number"], colors.COLOR_ID)
|
||||||
action = data["action"]
|
action = data["action"]
|
||||||
issue_title = data["issue"]["title"]
|
issue_title = data["issue"]["title"]
|
||||||
type = "PR" if "pull_request" in data["issue"] else "issue"
|
type = "PR" if "pull_request" in data["issue"] else "issue"
|
||||||
|
@ -384,7 +308,7 @@ class GitHub(object):
|
||||||
|
|
||||||
def create(self, full_name, data):
|
def create(self, full_name, data):
|
||||||
ref = data["ref"]
|
ref = data["ref"]
|
||||||
ref_color = utils.irc.color(ref, COLOR_BRANCH)
|
ref_color = utils.irc.color(ref, colors.COLOR_BRANCH)
|
||||||
type = data["ref_type"]
|
type = data["ref_type"]
|
||||||
sender = utils.irc.bold(data["sender"]["login"])
|
sender = utils.irc.bold(data["sender"]["login"])
|
||||||
url = self._short_url(CREATE_URL % (full_name, ref))
|
url = self._short_url(CREATE_URL % (full_name, ref))
|
||||||
|
@ -392,7 +316,7 @@ class GitHub(object):
|
||||||
|
|
||||||
def delete(self, full_name, data):
|
def delete(self, full_name, data):
|
||||||
ref = data["ref"]
|
ref = data["ref"]
|
||||||
ref_color = utils.irc.color(ref, COLOR_BRANCH)
|
ref_color = utils.irc.color(ref, colors.COLOR_BRANCH)
|
||||||
type = data["ref_type"]
|
type = data["ref_type"]
|
||||||
sender = utils.irc.bold(data["sender"]["login"])
|
sender = utils.irc.bold(data["sender"]["login"])
|
||||||
return ["%s deleted a %s: %s" % (sender, type, ref_color)]
|
return ["%s deleted a %s: %s" % (sender, type, ref_color)]
|
||||||
|
@ -433,11 +357,11 @@ class GitHub(object):
|
||||||
status_str = utils.irc.bold("started")
|
status_str = utils.irc.bold("started")
|
||||||
elif status == "completed":
|
elif status == "completed":
|
||||||
conclusion = data["check_run"]["conclusion"]
|
conclusion = data["check_run"]["conclusion"]
|
||||||
conclusion_color = COLOR_POSITIVE
|
conclusion_color = colors.COLOR_POSITIVE
|
||||||
if conclusion in CHECK_RUN_FAILURES:
|
if conclusion in CHECK_RUN_FAILURES:
|
||||||
conclusion_color = COLOR_NEGATIVE
|
conclusion_color = colors.COLOR_NEGATIVE
|
||||||
if conclusion == "neutral":
|
if conclusion == "neutral":
|
||||||
conclusion_color = COLOR_NEUTRAL
|
conclusion_color = colors.COLOR_NEUTRAL
|
||||||
|
|
||||||
status_str = utils.irc.color(
|
status_str = utils.irc.color(
|
||||||
CHECK_RUN_CONCLUSION[conclusion], conclusion_color)
|
CHECK_RUN_CONCLUSION[conclusion], conclusion_color)
|
||||||
|
|
Loading…
Reference in a new issue