From 86147ded77531c8a9b47a2033b697096c6c861be Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 18 Oct 2019 14:03:20 +0100 Subject: [PATCH] pass git_webhook urls up to __init__.py to be shortened --- modules/git_webhooks/__init__.py | 7 +++++- modules/git_webhooks/gitea.py | 40 ++++++++++++++++---------------- modules/git_webhooks/github.py | 32 +++++++++++++------------ modules/git_webhooks/gitlab.py | 20 +++++++--------- 4 files changed, 52 insertions(+), 47 deletions(-) diff --git a/modules/git_webhooks/__init__.py b/modules/git_webhooks/__init__.py index 721b2a26..d43c310a 100644 --- a/modules/git_webhooks/__init__.py +++ b/modules/git_webhooks/__init__.py @@ -118,10 +118,15 @@ class Module(ModuleManager.BaseModule): if repo_name and hide_org: source = repo_name - for output in outputs: + for output, url in outputs: output = "(%s) %s" % ( 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): output = self._prevent_highlight(server, channel, output) diff --git a/modules/git_webhooks/gitea.py b/modules/git_webhooks/gitea.py index 730159bb..19abd8bf 100644 --- a/modules/git_webhooks/gitea.py +++ b/modules/git_webhooks/gitea.py @@ -102,7 +102,7 @@ class Gitea(object): return self.ping(data) def ping(self, data): - return ["Received new webhook"] + return [["Received new webhook", None]] def _short_hash(self, hash): return hash[:8] @@ -120,16 +120,15 @@ class Gitea(object): message = commit["message"].split("\n")[0].strip() url = commit["url"] - outputs.append( - "%s pushed %s to %s: %s - %s" - % (author, hash_colored, branch, message, url)) + outputs.append(["%s pushed %s to %s: %s" + % (author, hash_colored, branch, message), url]) else: first_id = data["before"] last_id = data["commits"][-1]["id"] url = data["compare_url"] - outputs.append("%s pushed %d commits to %s - %s" - % (author, len(data["commits"]), branch, url)) + outputs.append(["%s pushed %d commits to %s" + % (author, len(data["commits"]), branch), url]) return outputs @@ -160,8 +159,8 @@ class Gitea(object): pr_title = data["pull_request"]["title"] author = utils.irc.bold(data["sender"]["login"]) url = data["pull_request"]["html_url"] - return ["[PR] %s %s: %s - %s" % ( - author, action_desc, pr_title, url)] + return [["[PR] %s %s: %s" % + (author, action_desc, pr_title), url]] def issues(self, full_name, data): @@ -173,37 +172,38 @@ class Gitea(object): url = "%s/issues/%d" % (data["repository"]["html_url"], data["issue"]["number"]) - return ["[issue] %s %s %s: %s - %s" % - (author, action, number, issue_title, url)] + return [["[issue] %s %s %s: %s" % + (author, action, number, issue_title), url]] def issue_comment(self, full_name, data): if "changes" in data: # don't show this event when nothing has actually changed 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"] issue_title = data["issue"]["title"] type = "PR" if data["issue"]["pull_request"] else "issue" commenter = utils.irc.bold(data["sender"]["login"]) url = data["comment"]["html_url"] - return ["[%s] %s %s on %s: %s - %s" % - (type, commenter, COMMENT_ACTIONS[action], number, issue_title, - url)] + return [["[%s] %s %s on %s: %s" % + (type, commenter, COMMENT_ACTIONS[action], number, issue_title), + url]] def create(self, full_name, data): ref = data["ref"] ref_color = utils.irc.color(ref, colors.COLOR_BRANCH) type = data["ref_type"] 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): ref = data["ref"] ref_color = utils.irc.color(ref, colors.COLOR_BRANCH) type = data["ref_type"] 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): return [] @@ -215,12 +215,12 @@ class Gitea(object): if name: name = ": %s" % name 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): forker = utils.irc.bold(data["sender"]["login"]) fork_full_name = utils.irc.color(data["repository"]["full_name"], utils.consts.LIGHTBLUE) url = data["repository"]["html_url"] - return ["%s forked into %s - %s" % - (forker, fork_full_name, url)] + return [["%s forked into %s" % + (forker, fork_full_name), url]] diff --git a/modules/git_webhooks/github.py b/modules/git_webhooks/github.py index 9dacab7c..7195f354 100644 --- a/modules/git_webhooks/github.py +++ b/modules/git_webhooks/github.py @@ -120,36 +120,38 @@ class GitHub(object): return EVENT_CATEGORIES.get(event, [event]) def webhook(self, full_name, event, data, headers): + out = [] if event == "push": - return self.push(full_name, data) + out = self.push(full_name, data) elif event == "commit_comment": - return self.commit_comment(full_name, data) + out = self.commit_comment(full_name, data) elif event == "pull_request": - return self.pull_request(full_name, data) + out = self.pull_request(full_name, data) 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": - return self.pull_request_review_comment(full_name, data) + out = self.pull_request_review_comment(full_name, data) elif event == "issue_comment": - return self.issue_comment(full_name, data) + out = self.issue_comment(full_name, data) elif event == "issues": - return self.issues(full_name, data) + out = self.issues(full_name, data) elif event == "create": - return self.create(full_name, data) + out = self.create(full_name, data) elif event == "delete": - return self.delete(full_name, data) + out = self.delete(full_name, data) elif event == "release": - return self.release(full_name, data) + out = self.release(full_name, data) elif event == "check_run": - return self.check_run(data) + out = self.check_run(data) elif event == "fork": - return self.fork(full_name, data) + out = self.fork(full_name, data) elif event == "ping": - return self.ping(data) + out = self.ping(data) elif event == "membership": - return self.membership(organisation, data) + out = self.membership(organisation, data) elif event == "watch": - return self.watch(data) + out = self.watch(data) + return list(zip(out, [None]*len(out))) def _short_url(self, url): self.log.debug("git.io shortening: %s" % url) diff --git a/modules/git_webhooks/gitlab.py b/modules/git_webhooks/gitlab.py index 20bd7592..e7e155cb 100644 --- a/modules/git_webhooks/gitlab.py +++ b/modules/git_webhooks/gitlab.py @@ -108,16 +108,15 @@ class GitLab(object): message = commit["message"].split("\n")[0].strip() url = commit["url"] - outputs.append( - "%s pushed %s to %s: %s - %s" - % (author, hash_colored, branch, message, url)) + outputs.append(["%s pushed %s to %s: %s - %s" + % (author, hash_colored, branch, message), url]) else: first_id = data["before"] last_id = data["after"] url = data["compare_url"] - outputs.append("%s pushed %d commits to %s" - % (author, len(data["commits"]), branch)) + outputs.append(["%s pushed %d commits to %s" + % (author, len(data["commits"]), branch), None]) return outputs @@ -143,8 +142,7 @@ class GitLab(object): pr_title = data["object_attributes"]["title"] author = utils.irc.bold(data["user"]["username"]) url = data["object_attributes"]["url"] - return ["[MR] %s %s: %s - %s" % ( - author, action_desc, pr_title, url)] + return [["[MR] %s %s: %s" % (author, action_desc, pr_title), url]] def issues(self, full_name, data): number = utils.irc.color("#%s" % data["object_attributes"]["iid"], @@ -154,8 +152,8 @@ class GitLab(object): author = utils.irc.bold(data["user"]["username"]) url = data["object_attributes"]["url"] - return ["[issue] %s %s %s: %s - %s" % - (author, action, number, issue_title, url)] + return [["[issue] %s %s %s: %s - %s" % + (author, action, number, issue_title), url]] def note(self, full_name, data): type = data["object_attributes"]["noteable_type"] @@ -171,5 +169,5 @@ class GitLab(object): issue_title = data["issue"]["title"] commenter = utils.irc.bold(data["user"]["username"]) url = data["object_attributes"]["url"] - return ["[%s] %s commented on %s: %s - %s" % - (type, commenter, number, issue_title, url)] + return [["[%s] %s commented on %s: %s" % + (type, commenter, number, issue_title), url]]