github.py: remove git url shortening

This commit is contained in:
David Schultz 2022-01-19 21:33:45 -06:00
parent 904cb2d94c
commit 046aa1b2cf
No known key found for this signature in database
GPG key ID: F6DED672FFFD5E5E
2 changed files with 39 additions and 52 deletions

View file

@ -5,6 +5,7 @@ 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"
CREATE_URL = "https://github.com/%s/tree/%s" CREATE_URL = "https://github.com/%s/tree/%s"
PR_URL = "https://github.com/%s/pull/%s"
PR_COMMIT_RANGE_URL = "https://github.com/%s/pull/%s/files/%s..%s" PR_COMMIT_RANGE_URL = "https://github.com/%s/pull/%s/files/%s..%s"
PR_COMMIT_URL = "https://github.com/%s/pull/%s/commits/%s" PR_COMMIT_URL = "https://github.com/%s/pull/%s/commits/%s"
@ -77,19 +78,19 @@ COMMENT_ACTIONS = {
} }
COMMENT_MAX = 100 COMMENT_MAX = 100
CHECK_RUN_CONCLUSION = { CHECK_SUITE_CONCLUSION = {
"success": "passed", "success": ("passed", colors.COLOR_POSITIVE),
"failure": "failed", "failure": ("failed", colors.COLOR_NEGATIVE),
"neutral": "finished", "neutral": ("finished", colors.COLOR_NEUTRAL),
"cancelled": "was cancelled", "cancelled": ("was cancelled", colors.COLOR_NEGATIVE),
"timed_out": "timed out", "timed_out": ("timed out", colors.COLOR_NEGATIVE),
"action_required": "requires action" "action_required": ("requires action", colors.COLOR_NEUTRAL)
} }
CHECK_RUN_FAILURES = ["failure", "cancelled", "timed_out", "action_required"]
class GitHub(object): class GitHub(object):
def __init__(self, log): def __init__(self, log, exports):
self.log = log self.log = log
self.exports = exports
def is_private(self, data, headers): def is_private(self, data, headers):
if "repository" in data: if "repository" in data:
@ -125,6 +126,8 @@ class GitHub(object):
category_action = None category_action = None
if "review" in data and "state" in data["review"]: if "review" in data and "state" in data["review"]:
category = "%s+%s" % (event, data["review"]["state"]) category = "%s+%s" % (event, data["review"]["state"])
elif "check_suite" in data and "conclusion" in data["check_suite"]:
category = "%s+%s" % (event, data["check_suite"]["conclusion"])
if action: if action:
if category: if category:
@ -159,8 +162,8 @@ class GitHub(object):
out = self.delete(full_name, data) out = self.delete(full_name, data)
elif event == "release": elif event == "release":
out = self.release(full_name, data) out = self.release(full_name, data)
elif event == "check_run": elif event == "check_suite":
out = self.check_run(data) out = self.check_suite(full_name, data)
elif event == "fork": elif event == "fork":
out = self.fork(full_name, data) out = self.fork(full_name, data)
elif event == "ping": elif event == "ping":
@ -267,7 +270,7 @@ class GitHub(object):
colored_branch = utils.irc.color(branch, colors.COLOR_BRANCH) colored_branch = utils.irc.color(branch, colors.COLOR_BRANCH)
sender = utils.irc.bold(data["sender"]["login"]) sender = utils.irc.bold(data["sender"]["login"])
author = utils.irc.bold(data["sender"]["login"]) author = utils.irc.bold(data["pull_request"]["user"]["login"])
number = utils.irc.color("#%s" % data["pull_request"]["number"], number = utils.irc.color("#%s" % data["pull_request"]["number"],
colors.COLOR_ID) colors.COLOR_ID)
identifier = "%s by %s" % (number, author) identifier = "%s by %s" % (number, author)
@ -428,44 +431,32 @@ class GitHub(object):
url = self._short_url(data["release"]["html_url"]) url = self._short_url(data["release"]["html_url"])
return ["%s %s a release%s - %s" % (author, action, name, url)] return ["%s %s a release%s - %s" % (author, action, name, url)]
def check_run(self, data): def check_suite(self, full_name, data):
name = data["check_run"]["name"] suite = data["check_suite"]
commit = self._short_hash(data["check_run"]["head_sha"])
commit = self._short_hash(suite["head_sha"])
commit = utils.irc.color(commit, utils.consts.LIGHTBLUE) commit = utils.irc.color(commit, utils.consts.LIGHTBLUE)
pr = ""
url = "" url = ""
if data["check_run"]["details_url"]: if suite["pull_requests"]:
url = data["check_run"]["details_url"] pr_num = suite["pull_requests"][0]["number"]
url = " - %s" % self.exports.get("shorturl-any")(url) 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
duration = "" name = suite["app"]["name"]
if data["check_run"]["completed_at"]: conclusion = suite["conclusion"]
started_at = self._iso8601(data["check_run"]["started_at"]) conclusion, conclusion_color = CHECK_SUITE_CONCLUSION[conclusion]
completed_at = self._iso8601(data["check_run"]["completed_at"]) conclusion = utils.irc.color(conclusion, conclusion_color)
if completed_at > started_at:
seconds = (completed_at-started_at).total_seconds()
duration = " in %s" % utils.datetime.format.to_pretty_time(
seconds)
status = data["check_run"]["status"] created_at = self._iso8601(suite["created_at"])
status_str = "" updated_at = self._iso8601(suite["updated_at"])
if status == "queued": seconds = (updated_at-created_at).total_seconds()
status_str = utils.irc.bold("queued") duration = utils.datetime.format.to_pretty_time(seconds)
elif status == "in_progress":
status_str = utils.irc.bold("started")
elif status == "completed":
conclusion = data["check_run"]["conclusion"]
conclusion_color = colors.COLOR_POSITIVE
if conclusion in CHECK_RUN_FAILURES:
conclusion_color = colors.COLOR_NEGATIVE
if conclusion == "neutral":
conclusion_color = colors.COLOR_NEUTRAL
status_str = utils.irc.color( return ["[build @%s%s] %s: %s in %s%s" % (
CHECK_RUN_CONCLUSION[conclusion], conclusion_color) commit, pr, name, conclusion, duration, url)]
return ["[build @%s] %s: %s%s%s" % (
commit, name, status_str, 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"])

View file

@ -47,13 +47,9 @@ class Module(ModuleManager.BaseModule):
return org, repo, number return org, repo, number
def _short_url(self, url): def _short_url(self, url):
try: # TODO: find an alternative to git.io
page = utils.http.request("https://git.io", method="POST", # see https://github.com/jesopo/bitbot/issues/338
post_data={"url": url}) # ~ examknow 1/19/2022
return page.headers["Location"]
except utils.http.HTTPTimeoutException:
self.log.warn(
"HTTPTimeoutException while waiting for github short URL", [])
return url return url
def _change_count(self, n, symbol, color): def _change_count(self, n, symbol, color):