Merge pull request #341 from bitbot-irc/launchd/git-universal-shorting

Honor url shortener preferences for git modules
This commit is contained in:
David Schultz 2022-09-08 17:38:03 -05:00 committed by GitHub
commit 1fd0e30957
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 54 deletions

91
modules/git_webhooks/github.py Normal file → Executable file
View file

@ -172,19 +172,13 @@ class GitHub(object):
out = self.membership(organisation, data) out = self.membership(organisation, data)
elif event == "watch": elif event == "watch":
out = self.watch(data) out = self.watch(data)
return list(zip(out, [None]*len(out))) return out
def _short_url(self, url):
# TODO: find an alternative to git.io
# see https://github.com/jesopo/bitbot/issues/338
# ~ examknow 1/19/2022
return url
def _iso8601(self, s): def _iso8601(self, s):
return utils.datetime.parse.iso8601(s) return utils.datetime.parse.iso8601(s)
def ping(self, data): def ping(self, data):
return ["Received new webhook"] return [("Received new webhook", None)]
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("")
@ -228,21 +222,20 @@ class GitHub(object):
if len(commits) == 0 and forced: if len(commits) == 0 and forced:
outputs.append( outputs.append(
"%s %spushed to %s" % (author, forced_str, branch)) "%s %spushed to %s" % (author, forced_str, branch), None)
elif len(commits) <= 3: elif len(commits) <= 3:
for commit in commits: for commit in commits:
hash = commit["id"] hash = commit["id"]
hash_colored = utils.irc.color(self._short_hash(hash), colors.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(single_url % hash) url = single_url % hash
outputs.append( outputs.append((
"%s %spushed %s to %s: %s - %s" "%s %spushed %s to %s: %s"
% (author, forced_str, hash_colored, branch, message, url)) % (author, forced_str, hash_colored, branch, message), url))
else: else:
outputs.append("%s %spushed %d commits to %s - %s" outputs.append(("%s %spushed %d commits to %s"
% (author, forced_str, len(commits), branch, % (author, forced_str, len(commits), branch), url))
self._short_url(range_url)))
return outputs return outputs
@ -260,9 +253,9 @@ class GitHub(object):
action = data["action"] action = data["action"]
commit = self._short_hash(data["comment"]["commit_id"]) commit = self._short_hash(data["comment"]["commit_id"])
commenter = utils.irc.bold(data["comment"]["user"]["login"]) commenter = utils.irc.bold(data["comment"]["user"]["login"])
url = self._short_url(data["comment"]["html_url"]) url = data["comment"]["html_url"]
return ["[commit/%s] %s %s a comment - %s" % (commit, commenter, return [("[commit/%s] %s %s a comment" % (commit, commenter,
action, url)] action), url)]
def pull_request(self, full_name, data): def pull_request(self, full_name, data):
raw_number = data["pull_request"]["number"] raw_number = data["pull_request"]["number"]
@ -326,9 +319,9 @@ class GitHub(object):
action_desc = "renamed %s" % identifier action_desc = "renamed %s" % identifier
pr_title = data["pull_request"]["title"] pr_title = data["pull_request"]["title"]
url = self._short_url(data["pull_request"]["html_url"]) url = data["pull_request"]["html_url"]
return ["[PR] %s %s: %s - %s" % ( return [("[PR] %s %s: %s" % (
sender, action_desc, pr_title, url)] sender, action_desc, pr_title), url)]
def pull_request_review(self, full_name, data): def pull_request_review(self, full_name, data):
if not data["action"] == "submitted": if not data["action"] == "submitted":
@ -346,7 +339,7 @@ class GitHub(object):
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"])
url = self._short_url(data["review"]["html_url"]) url = data["review"]["html_url"]
state_desc = state state_desc = state
if state == "approved": if state == "approved":
@ -356,8 +349,8 @@ class GitHub(object):
elif state == "dismissed": elif state == "dismissed":
state_desc = "dismissed a review" state_desc = "dismissed a review"
return ["[PR] %s %s on %s: %s - %s" % return [("[PR] %s %s on %s: %s" %
(reviewer, state_desc, number, pr_title, url)] (reviewer, state_desc, number, pr_title), url)]
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"],
@ -365,9 +358,9 @@ class GitHub(object):
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"])
url = self._short_url(data["comment"]["html_url"]) url = data["comment"]["html_url"]
return ["[PR] %s %s on a review on %s: %s - %s" % return [("[PR] %s %s on a review on %s: %s" %
(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"], number = utils.irc.color("#%s" % data["issue"]["number"],
@ -381,9 +374,9 @@ class GitHub(object):
issue_title = data["issue"]["title"] issue_title = data["issue"]["title"]
author = utils.irc.bold(data["sender"]["login"]) author = utils.irc.bold(data["sender"]["login"])
url = self._short_url(data["issue"]["html_url"]) url = data["issue"]["html_url"]
return ["[issue] %s %s: %s - %s" % return [("[issue] %s %s: %s" %
(author, action_str, issue_title, url)] (author, action_str, 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
@ -396,30 +389,29 @@ class GitHub(object):
type = "PR" if "pull_request" in data["issue"] else "issue" type = "PR" if "pull_request" in data["issue"] else "issue"
title = data["issue"]["title"] title = data["issue"]["title"]
commenter = utils.irc.bold(data["sender"]["login"]) commenter = utils.irc.bold(data["sender"]["login"])
url = self._short_url(data["comment"]["html_url"]) url = data["comment"]["html_url"]
body = "" body = ""
if not action == "deleted": if not action == "deleted":
body = ": %s" % self._comment(data["comment"]["body"]) body = ": %s" % self._comment(data["comment"]["body"])
return ["[%s] %s %s on %s (%s)%s - %s" % return [("[%s] %s %s on %s (%s)%s" %
(type, commenter, COMMENT_ACTIONS[action], number, title, body, (type, commenter, COMMENT_ACTIONS[action], number, title, body), 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"])
url = self._short_url(CREATE_URL % (full_name, ref)) url = CREATE_URL % (full_name, ref)
return ["%s created a %s: %s - %s" % (sender, type, ref_color, url)] return [("%s created a %s: %s" % (sender, type, ref_color), url)]
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 release(self, full_name, data): def release(self, full_name, data):
action = data["action"] action = data["action"]
@ -428,8 +420,8 @@ class GitHub(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"])
url = self._short_url(data["release"]["html_url"]) url = data["release"]["html_url"]
return ["%s %s a release%s - %s" % (author, action, name, url)] return [("%s %s a release%s" % (author, action, name), url)]
def check_suite(self, full_name, data): def check_suite(self, full_name, data):
suite = data["check_suite"] suite = data["check_suite"]
@ -442,8 +434,7 @@ class GitHub(object):
if suite["pull_requests"]: if suite["pull_requests"]:
pr_num = suite["pull_requests"][0]["number"] pr_num = suite["pull_requests"][0]["number"]
pr = "/PR%s" % utils.irc.color("#%s" % pr_num, colors.COLOR_ID) pr = "/PR%s" % utils.irc.color("#%s" % pr_num, colors.COLOR_ID)
url = self._short_url(PR_URL % (full_name, pr_num)) url = PR_URL % (full_name, pr_num)
url = " - %s" % url
name = suite["app"]["name"] name = suite["app"]["name"]
conclusion = suite["conclusion"] conclusion = suite["conclusion"]
@ -455,21 +446,21 @@ class GitHub(object):
seconds = (updated_at-created_at).total_seconds() seconds = (updated_at-created_at).total_seconds()
duration = utils.datetime.format.to_pretty_time(seconds) duration = utils.datetime.format.to_pretty_time(seconds)
return ["[build @%s%s] %s: %s in %s%s" % ( return [("[build @%s%s] %s: %s in %s" % (
commit, pr, name, conclusion, duration, url)] commit, pr, name, conclusion, duration), url)]
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["forkee"]["full_name"], fork_full_name = utils.irc.color(data["forkee"]["full_name"],
utils.consts.LIGHTBLUE) utils.consts.LIGHTBLUE)
url = self._short_url(data["forkee"]["html_url"]) url = data["forkee"]["html_url"]
return ["%s forked into %s - %s" % return [("%s forked into %s" %
(forker, fork_full_name, url)] (forker, fork_full_name), url)]
def membership(self, organisation, data): def membership(self, organisation, data):
return ["%s %s %s to team %s" % return [("%s %s %s to team %s" %
(data["sender"]["login"], data["action"], data["member"]["login"], (data["sender"]["login"], data["action"], data["member"]["login"],
data["team"]["name"])] data["team"]["name"]), None)]
def watch(self, data): def watch(self, data):
return ["%s starred the repository" % data["sender"]["login"]] return [("%s starred the repository" % data["sender"]["login"], None)]

View file

@ -47,10 +47,7 @@ class Module(ModuleManager.BaseModule):
return org, repo, number return org, repo, number
def _short_url(self, url): def _short_url(self, url):
# TODO: find an alternative to git.io return self.exports.get("shorturl")(self.bot, url) or url
# see https://github.com/jesopo/bitbot/issues/338
# ~ examknow 1/19/2022
return url
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("")