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)
elif event == "watch":
out = self.watch(data)
return list(zip(out, [None]*len(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
return out
def _iso8601(self, s):
return utils.datetime.parse.iso8601(s)
def ping(self, data):
return ["Received new webhook"]
return [("Received new webhook", None)]
def _change_count(self, n, symbol, color):
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:
outputs.append(
"%s %spushed to %s" % (author, forced_str, branch))
"%s %spushed to %s" % (author, forced_str, branch), None)
elif len(commits) <= 3:
for commit in commits:
hash = commit["id"]
hash_colored = utils.irc.color(self._short_hash(hash), colors.COLOR_ID)
message = commit["message"].split("\n")[0].strip()
url = self._short_url(single_url % hash)
url = single_url % hash
outputs.append(
"%s %spushed %s to %s: %s - %s"
% (author, forced_str, hash_colored, branch, message, url))
outputs.append((
"%s %spushed %s to %s: %s"
% (author, forced_str, hash_colored, branch, message), url))
else:
outputs.append("%s %spushed %d commits to %s - %s"
% (author, forced_str, len(commits), branch,
self._short_url(range_url)))
outputs.append(("%s %spushed %d commits to %s"
% (author, forced_str, len(commits), branch), url))
return outputs
@ -260,9 +253,9 @@ class GitHub(object):
action = data["action"]
commit = self._short_hash(data["comment"]["commit_id"])
commenter = utils.irc.bold(data["comment"]["user"]["login"])
url = self._short_url(data["comment"]["html_url"])
return ["[commit/%s] %s %s a comment - %s" % (commit, commenter,
action, url)]
url = data["comment"]["html_url"]
return [("[commit/%s] %s %s a comment" % (commit, commenter,
action), url)]
def pull_request(self, full_name, data):
raw_number = data["pull_request"]["number"]
@ -326,9 +319,9 @@ class GitHub(object):
action_desc = "renamed %s" % identifier
pr_title = data["pull_request"]["title"]
url = self._short_url(data["pull_request"]["html_url"])
return ["[PR] %s %s: %s - %s" % (
sender, action_desc, pr_title, url)]
url = data["pull_request"]["html_url"]
return [("[PR] %s %s: %s" % (
sender, action_desc, pr_title), url)]
def pull_request_review(self, full_name, data):
if not data["action"] == "submitted":
@ -346,7 +339,7 @@ class GitHub(object):
action = data["action"]
pr_title = data["pull_request"]["title"]
reviewer = utils.irc.bold(data["sender"]["login"])
url = self._short_url(data["review"]["html_url"])
url = data["review"]["html_url"]
state_desc = state
if state == "approved":
@ -356,8 +349,8 @@ class GitHub(object):
elif state == "dismissed":
state_desc = "dismissed a review"
return ["[PR] %s %s on %s: %s - %s" %
(reviewer, state_desc, number, pr_title, url)]
return [("[PR] %s %s on %s: %s" %
(reviewer, state_desc, number, pr_title), url)]
def pull_request_review_comment(self, full_name, data):
number = utils.irc.color("#%s" % data["pull_request"]["number"],
@ -365,9 +358,9 @@ class GitHub(object):
action = data["action"]
pr_title = data["pull_request"]["title"]
sender = utils.irc.bold(data["sender"]["login"])
url = self._short_url(data["comment"]["html_url"])
return ["[PR] %s %s on a review on %s: %s - %s" %
(sender, COMMENT_ACTIONS[action], number, pr_title, url)]
url = data["comment"]["html_url"]
return [("[PR] %s %s on a review on %s: %s" %
(sender, COMMENT_ACTIONS[action], number, pr_title), url)]
def issues(self, full_name, data):
number = utils.irc.color("#%s" % data["issue"]["number"],
@ -381,9 +374,9 @@ class GitHub(object):
issue_title = data["issue"]["title"]
author = utils.irc.bold(data["sender"]["login"])
url = self._short_url(data["issue"]["html_url"])
return ["[issue] %s %s: %s - %s" %
(author, action_str, issue_title, url)]
url = data["issue"]["html_url"]
return [("[issue] %s %s: %s" %
(author, action_str, issue_title), url)]
def issue_comment(self, full_name, data):
if "changes" in data:
# 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"
title = data["issue"]["title"]
commenter = utils.irc.bold(data["sender"]["login"])
url = self._short_url(data["comment"]["html_url"])
url = data["comment"]["html_url"]
body = ""
if not action == "deleted":
body = ": %s" % self._comment(data["comment"]["body"])
return ["[%s] %s %s on %s (%s)%s - %s" %
(type, commenter, COMMENT_ACTIONS[action], number, title, body,
url)]
return [("[%s] %s %s on %s (%s)%s" %
(type, commenter, COMMENT_ACTIONS[action], number, title, body), 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"])
url = self._short_url(CREATE_URL % (full_name, ref))
return ["%s created a %s: %s - %s" % (sender, type, ref_color, url)]
url = CREATE_URL % (full_name, ref)
return [("%s created a %s: %s" % (sender, type, ref_color), url)]
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 release(self, full_name, data):
action = data["action"]
@ -428,8 +420,8 @@ class GitHub(object):
if name:
name = ": %s" % name
author = utils.irc.bold(data["release"]["author"]["login"])
url = self._short_url(data["release"]["html_url"])
return ["%s %s a release%s - %s" % (author, action, name, url)]
url = data["release"]["html_url"]
return [("%s %s a release%s" % (author, action, name), url)]
def check_suite(self, full_name, data):
suite = data["check_suite"]
@ -442,8 +434,7 @@ class GitHub(object):
if suite["pull_requests"]:
pr_num = suite["pull_requests"][0]["number"]
pr = "/PR%s" % utils.irc.color("#%s" % pr_num, colors.COLOR_ID)
url = self._short_url(PR_URL % (full_name, pr_num))
url = " - %s" % url
url = PR_URL % (full_name, pr_num)
name = suite["app"]["name"]
conclusion = suite["conclusion"]
@ -455,21 +446,21 @@ class GitHub(object):
seconds = (updated_at-created_at).total_seconds()
duration = utils.datetime.format.to_pretty_time(seconds)
return ["[build @%s%s] %s: %s in %s%s" % (
commit, pr, name, conclusion, duration, url)]
return [("[build @%s%s] %s: %s in %s" % (
commit, pr, name, conclusion, duration), url)]
def fork(self, full_name, data):
forker = utils.irc.bold(data["sender"]["login"])
fork_full_name = utils.irc.color(data["forkee"]["full_name"],
utils.consts.LIGHTBLUE)
url = self._short_url(data["forkee"]["html_url"])
return ["%s forked into %s - %s" %
(forker, fork_full_name, url)]
url = data["forkee"]["html_url"]
return [("%s forked into %s" %
(forker, fork_full_name), url)]
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["team"]["name"])]
data["team"]["name"]), None)]
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
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
return self.exports.get("shorturl")(self.bot, url) or url
def _change_count(self, n, symbol, color):
return utils.irc.color("%s%d" % (symbol, n), color)+utils.irc.bold("")