pass git_webhook urls up to __init__.py to be shortened

This commit is contained in:
jesopo 2019-10-18 14:03:20 +01:00
parent 2b4f5dc75b
commit 86147ded77
4 changed files with 52 additions and 47 deletions

View file

@ -118,10 +118,15 @@ class Module(ModuleManager.BaseModule):
if repo_name and hide_org: if repo_name and hide_org:
source = repo_name source = repo_name
for output in outputs: for output, url in outputs:
output = "(%s) %s" % ( output = "(%s) %s" % (
utils.irc.color(source, colors.COLOR_REPO), output) utils.irc.color(source, colors.COLOR_REPO), output)
if url:
shorturl = self.exports.get_one("shorturl")(server, url,
context=channel) or url
output = "%s - %s" % (output, shorturl)
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,
output) output)

View file

@ -102,7 +102,7 @@ class Gitea(object):
return self.ping(data) return self.ping(data)
def ping(self, data): def ping(self, data):
return ["Received new webhook"] return [["Received new webhook", None]]
def _short_hash(self, hash): def _short_hash(self, hash):
return hash[:8] return hash[:8]
@ -120,16 +120,15 @@ class Gitea(object):
message = commit["message"].split("\n")[0].strip() message = commit["message"].split("\n")[0].strip()
url = commit["url"] url = commit["url"]
outputs.append( outputs.append(["%s pushed %s to %s: %s"
"%s pushed %s to %s: %s - %s" % (author, hash_colored, branch, message), url])
% (author, hash_colored, branch, message, url))
else: else:
first_id = data["before"] first_id = data["before"]
last_id = data["commits"][-1]["id"] last_id = data["commits"][-1]["id"]
url = data["compare_url"] url = data["compare_url"]
outputs.append("%s pushed %d commits to %s - %s" outputs.append(["%s pushed %d commits to %s"
% (author, len(data["commits"]), branch, url)) % (author, len(data["commits"]), branch), url])
return outputs return outputs
@ -160,8 +159,8 @@ class Gitea(object):
pr_title = data["pull_request"]["title"] pr_title = data["pull_request"]["title"]
author = utils.irc.bold(data["sender"]["login"]) author = utils.irc.bold(data["sender"]["login"])
url = data["pull_request"]["html_url"] url = data["pull_request"]["html_url"]
return ["[PR] %s %s: %s - %s" % ( return [["[PR] %s %s: %s" %
author, action_desc, pr_title, url)] (author, action_desc, pr_title), url]]
def issues(self, full_name, data): def issues(self, full_name, data):
@ -173,37 +172,38 @@ class Gitea(object):
url = "%s/issues/%d" % (data["repository"]["html_url"], url = "%s/issues/%d" % (data["repository"]["html_url"],
data["issue"]["number"]) data["issue"]["number"])
return ["[issue] %s %s %s: %s - %s" % return [["[issue] %s %s %s: %s" %
(author, action, number, issue_title, url)] (author, action, number, issue_title), url]]
def issue_comment(self, full_name, data): def issue_comment(self, full_name, data):
if "changes" in data: if "changes" in data:
# don't show this event when nothing has actually changed # don't show this event when nothing has actually changed
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"], colors.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 data["issue"]["pull_request"] else "issue" type = "PR" if data["issue"]["pull_request"] else "issue"
commenter = utils.irc.bold(data["sender"]["login"]) commenter = utils.irc.bold(data["sender"]["login"])
url = data["comment"]["html_url"] url = data["comment"]["html_url"]
return ["[%s] %s %s on %s: %s - %s" % return [["[%s] %s %s on %s: %s" %
(type, commenter, COMMENT_ACTIONS[action], number, issue_title, (type, commenter, COMMENT_ACTIONS[action], number, issue_title),
url)] url]]
def create(self, full_name, data): def create(self, full_name, data):
ref = data["ref"] ref = data["ref"]
ref_color = utils.irc.color(ref, colors.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 created a %s: %s" % (sender, type, ref_color)] return [["%s created a %s: %s" % (sender, type, ref_color), None]]
def delete(self, full_name, data): def delete(self, full_name, data):
ref = data["ref"] ref = data["ref"]
ref_color = utils.irc.color(ref, colors.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)], None]
def repository(self, full_name, data): def repository(self, full_name, data):
return [] return []
@ -215,12 +215,12 @@ class Gitea(object):
if name: if name:
name = ": %s" % name name = ": %s" % name
author = utils.irc.bold(data["release"]["author"]["login"]) author = utils.irc.bold(data["release"]["author"]["login"])
return ["%s %s a release%s" % (author, action, name)] return [["%s %s a release%s" % (author, action, name)], None]
def fork(self, full_name, data): def fork(self, full_name, data):
forker = utils.irc.bold(data["sender"]["login"]) forker = utils.irc.bold(data["sender"]["login"])
fork_full_name = utils.irc.color(data["repository"]["full_name"], fork_full_name = utils.irc.color(data["repository"]["full_name"],
utils.consts.LIGHTBLUE) utils.consts.LIGHTBLUE)
url = data["repository"]["html_url"] url = data["repository"]["html_url"]
return ["%s forked into %s - %s" % return [["%s forked into %s" %
(forker, fork_full_name, url)] (forker, fork_full_name), url]]

View file

@ -120,36 +120,38 @@ class GitHub(object):
return EVENT_CATEGORIES.get(event, [event]) return EVENT_CATEGORIES.get(event, [event])
def webhook(self, full_name, event, data, headers): def webhook(self, full_name, event, data, headers):
out = []
if event == "push": if event == "push":
return self.push(full_name, data) out = self.push(full_name, data)
elif event == "commit_comment": elif event == "commit_comment":
return self.commit_comment(full_name, data) out = self.commit_comment(full_name, data)
elif event == "pull_request": elif event == "pull_request":
return self.pull_request(full_name, data) out = self.pull_request(full_name, data)
elif event == "pull_request_review": elif event == "pull_request_review":
return self.pull_request_review(full_name, data) out = self.pull_request_review(full_name, data)
elif event == "pull_request_review_comment": elif event == "pull_request_review_comment":
return self.pull_request_review_comment(full_name, data) out = self.pull_request_review_comment(full_name, data)
elif event == "issue_comment": elif event == "issue_comment":
return self.issue_comment(full_name, data) out = self.issue_comment(full_name, data)
elif event == "issues": elif event == "issues":
return self.issues(full_name, data) out = self.issues(full_name, data)
elif event == "create": elif event == "create":
return self.create(full_name, data) out = self.create(full_name, data)
elif event == "delete": elif event == "delete":
return self.delete(full_name, data) out = self.delete(full_name, data)
elif event == "release": elif event == "release":
return self.release(full_name, data) out = self.release(full_name, data)
elif event == "check_run": elif event == "check_run":
return self.check_run(data) out = self.check_run(data)
elif event == "fork": elif event == "fork":
return self.fork(full_name, data) out = self.fork(full_name, data)
elif event == "ping": elif event == "ping":
return self.ping(data) out = self.ping(data)
elif event == "membership": elif event == "membership":
return self.membership(organisation, data) out = self.membership(organisation, data)
elif event == "watch": elif event == "watch":
return self.watch(data) out = self.watch(data)
return list(zip(out, [None]*len(out)))
def _short_url(self, url): def _short_url(self, url):
self.log.debug("git.io shortening: %s" % url) self.log.debug("git.io shortening: %s" % url)

View file

@ -108,16 +108,15 @@ class GitLab(object):
message = commit["message"].split("\n")[0].strip() message = commit["message"].split("\n")[0].strip()
url = commit["url"] url = commit["url"]
outputs.append( outputs.append(["%s pushed %s to %s: %s - %s"
"%s pushed %s to %s: %s - %s" % (author, hash_colored, branch, message), url])
% (author, hash_colored, branch, message, url))
else: else:
first_id = data["before"] first_id = data["before"]
last_id = data["after"] last_id = data["after"]
url = data["compare_url"] url = data["compare_url"]
outputs.append("%s pushed %d commits to %s" outputs.append(["%s pushed %d commits to %s"
% (author, len(data["commits"]), branch)) % (author, len(data["commits"]), branch), None])
return outputs return outputs
@ -143,8 +142,7 @@ class GitLab(object):
pr_title = data["object_attributes"]["title"] pr_title = data["object_attributes"]["title"]
author = utils.irc.bold(data["user"]["username"]) author = utils.irc.bold(data["user"]["username"])
url = data["object_attributes"]["url"] url = data["object_attributes"]["url"]
return ["[MR] %s %s: %s - %s" % ( return [["[MR] %s %s: %s" % (author, action_desc, pr_title), url]]
author, action_desc, pr_title, url)]
def issues(self, full_name, data): def issues(self, full_name, data):
number = utils.irc.color("#%s" % data["object_attributes"]["iid"], number = utils.irc.color("#%s" % data["object_attributes"]["iid"],
@ -154,8 +152,8 @@ class GitLab(object):
author = utils.irc.bold(data["user"]["username"]) author = utils.irc.bold(data["user"]["username"])
url = data["object_attributes"]["url"] url = data["object_attributes"]["url"]
return ["[issue] %s %s %s: %s - %s" % return [["[issue] %s %s %s: %s - %s" %
(author, action, number, issue_title, url)] (author, action, number, issue_title), url]]
def note(self, full_name, data): def note(self, full_name, data):
type = data["object_attributes"]["noteable_type"] type = data["object_attributes"]["noteable_type"]
@ -171,5 +169,5 @@ class GitLab(object):
issue_title = data["issue"]["title"] issue_title = data["issue"]["title"]
commenter = utils.irc.bold(data["user"]["username"]) commenter = utils.irc.bold(data["user"]["username"])
url = data["object_attributes"]["url"] url = data["object_attributes"]["url"]
return ["[%s] %s commented on %s: %s - %s" % return [["[%s] %s commented on %s: %s" %
(type, commenter, number, issue_title, url)] (type, commenter, number, issue_title), url]]