mirror of
https://github.com/TehPeGaSuS/GitBot.git
synced 2026-07-05 14:05:45 +02:00
Some minor improvements and additions
This commit is contained in:
@@ -88,6 +88,10 @@ class Bot:
|
||||
self._load_static_webhooks()
|
||||
self._load_static_rss()
|
||||
|
||||
@property
|
||||
def _commit_limit(self) -> int:
|
||||
return self._cfg.get("commit_limit", 3)
|
||||
|
||||
# ── Static config loading ─────────────────────────────────────────────────
|
||||
|
||||
def _load_static_webhooks(self):
|
||||
@@ -99,6 +103,7 @@ class Bot:
|
||||
db.webhook_add(
|
||||
self._database, net, ch,
|
||||
hook["repo"],
|
||||
hook.get("forge"),
|
||||
hook.get("events", list(DEFAULT_EVENTS)),
|
||||
hook.get("branches", []),
|
||||
)
|
||||
@@ -245,16 +250,21 @@ class Bot:
|
||||
primary = events[0] if events else ""
|
||||
|
||||
targets = db.webhook_targets(
|
||||
self._database, full_name, repo_user, organisation)
|
||||
self._database, forge, full_name, repo_user, organisation)
|
||||
|
||||
if not targets:
|
||||
log.debug("[%s] No targets for %s", forge, full_name)
|
||||
return
|
||||
|
||||
outputs = parser.parse(full_name, primary, data, headers)
|
||||
outputs = parser.parse(full_name, primary, data, headers,
|
||||
commit_limit=self._commit_limit)
|
||||
if not outputs:
|
||||
return
|
||||
|
||||
forge_tag = fmt.bold(f"[{forge.capitalize()}]")
|
||||
source = fmt.color(full_name or organisation or repo_name or forge,
|
||||
fmt.COLOR_REPO)
|
||||
|
||||
for target in targets:
|
||||
if branch and target["branches"] and branch not in target["branches"]:
|
||||
continue
|
||||
@@ -265,12 +275,8 @@ class Bot:
|
||||
if not set(events) & allowed:
|
||||
continue
|
||||
|
||||
source = fmt.color(
|
||||
full_name or organisation or repo_name or forge,
|
||||
fmt.COLOR_REPO)
|
||||
|
||||
for message, url in outputs:
|
||||
line = f"({source}) {message}"
|
||||
line = f"{forge_tag} ({source}) {message}"
|
||||
if url:
|
||||
line = f"{line} - {url}"
|
||||
await self._deliver_irc(target["network"], target["channel"], line)
|
||||
|
||||
+55
-27
File diff suppressed because it is too large
Load Diff
+27
-14
@@ -77,9 +77,9 @@ def event_categories(ev):
|
||||
return EVENT_CATEGORIES.get(ev, [ev])
|
||||
|
||||
|
||||
def parse(full_name, ev, data, headers):
|
||||
def parse(full_name, ev, data, headers, commit_limit=3):
|
||||
dispatch = {
|
||||
"push": _push,
|
||||
"push": lambda fn, d: _push(fn, d, commit_limit),
|
||||
"pull_request": _pull_request,
|
||||
"issues": _issues,
|
||||
"issue_comment": _issue_comment,
|
||||
@@ -96,19 +96,32 @@ def parse(full_name, ev, data, headers):
|
||||
return []
|
||||
|
||||
|
||||
def _push(full_name, data):
|
||||
def _push(full_name, data, commit_limit=3):
|
||||
branch_str = color(data["ref"].rpartition("/")[2], COLOR_BRANCH)
|
||||
author = bold(data["pusher"]["login"])
|
||||
commits = data.get("commits", [])
|
||||
outputs = []
|
||||
if len(commits) <= 3:
|
||||
for c in commits:
|
||||
h = color(_short(c["id"]), COLOR_ID)
|
||||
msg = c["message"].split("\n")[0].strip()
|
||||
outputs.append((f"{author} pushed {h} to {branch_str}: {msg}", c["url"]))
|
||||
else:
|
||||
url = data.get("compare_url")
|
||||
outputs.append((f"{author} pushed {len(commits)} commits to {branch_str}", url))
|
||||
author = bold(data["pusher"]["login"])
|
||||
commits = data.get("commits", [])
|
||||
range_url = data.get("compare_url")
|
||||
n = len(commits)
|
||||
|
||||
if not commits:
|
||||
return [(f"{author} pushed to {branch_str}", None)]
|
||||
|
||||
# Single commit: one clean line
|
||||
if n == 1:
|
||||
c = commits[0]
|
||||
h = color(_short(c["id"]), COLOR_ID)
|
||||
msg = c["message"].split("\n")[0].strip()
|
||||
return [(f"{author} pushed {h} to {branch_str}: {msg}", c.get("url"))]
|
||||
|
||||
# Multiple commits
|
||||
outputs = [(f"{author} pushed {n} commits to {branch_str}", range_url)]
|
||||
shown = commits[:commit_limit]
|
||||
for c in shown:
|
||||
msg = c["message"].split("\n")[0].strip()
|
||||
outputs.append((f"{author} {_short(c['id'])} - {msg}", None))
|
||||
hidden = n - len(shown)
|
||||
if hidden > 0:
|
||||
outputs.append((f"(+{hidden} hidden commit{'s' if hidden != 1 else ''})", None))
|
||||
return outputs
|
||||
|
||||
|
||||
|
||||
+29
-22
@@ -104,10 +104,10 @@ def event_categories(ev):
|
||||
return EVENT_CATEGORIES.get(ev, [ev])
|
||||
|
||||
|
||||
def parse(full_name, ev, data, headers):
|
||||
def parse(full_name, ev, data, headers, commit_limit=3):
|
||||
"""Return list of (message, url) tuples."""
|
||||
dispatch = {
|
||||
"push": _push,
|
||||
"push": lambda fn, d: _push(fn, d, commit_limit),
|
||||
"commit_comment": _commit_comment,
|
||||
"pull_request": _pull_request,
|
||||
"pull_request_review": _pr_review,
|
||||
@@ -128,33 +128,40 @@ def parse(full_name, ev, data, headers):
|
||||
return []
|
||||
|
||||
|
||||
def _format_push(branch_str, author, commits, forced, single_url, range_url):
|
||||
outputs = []
|
||||
def _push(full_name, data, commit_limit=3):
|
||||
branch_str = color(data["ref"].split("/", 2)[2], COLOR_BRANCH)
|
||||
author = bold(data["pusher"]["name"])
|
||||
forced = data.get("forced", False)
|
||||
commits = data.get("commits", [])
|
||||
forced_str = f"{color('force', RED)} " if forced else ""
|
||||
|
||||
if not commits and forced:
|
||||
return [(f"{author} {forced_str}pushed to {branch_str}", None)]
|
||||
if len(commits) <= 3:
|
||||
for c in commits:
|
||||
h = color(_short(c["id"]), COLOR_ID)
|
||||
msg = c["message"].split("\n")[0].strip()
|
||||
url = single_url % c["id"]
|
||||
outputs.append((f"{author} {forced_str}pushed {h} to {branch_str}: {msg}", url))
|
||||
else:
|
||||
url = range_url
|
||||
outputs.append((f"{author} {forced_str}pushed {len(commits)} commits to {branch_str}", url))
|
||||
return outputs
|
||||
|
||||
|
||||
def _push(full_name, data):
|
||||
branch_str = color(data["ref"].split("/", 2)[2], COLOR_BRANCH)
|
||||
author = bold(data["pusher"]["name"])
|
||||
forced = data.get("forced", False)
|
||||
commits = data.get("commits", [])
|
||||
range_url = None
|
||||
if commits:
|
||||
range_url = COMMIT_RANGE_URL % (full_name, data["before"], commits[-1]["id"])
|
||||
single_url = COMMIT_URL % (full_name, "%s")
|
||||
return _format_push(branch_str, author, commits, forced, single_url, range_url)
|
||||
|
||||
n = len(commits)
|
||||
|
||||
# Single commit: one clean line with hash, branch and message
|
||||
if n == 1:
|
||||
c = commits[0]
|
||||
h = color(_short(c["id"]), COLOR_ID)
|
||||
msg = c["message"].split("\n")[0].strip()
|
||||
url = COMMIT_URL % (full_name, c["id"])
|
||||
return [(f"{author} {forced_str}pushed {h} to {branch_str}: {msg}", url)]
|
||||
|
||||
# Multiple commits: summary line + individual lines + optional hidden count
|
||||
outputs = [(f"{author} {forced_str}pushed {n} commits to {branch_str}", range_url)]
|
||||
shown = commits[:commit_limit]
|
||||
for c in shown:
|
||||
msg = c["message"].split("\n")[0].strip()
|
||||
outputs.append((f"{author} {_short(c['id'])} - {msg}", None))
|
||||
hidden = n - len(shown)
|
||||
if hidden > 0:
|
||||
outputs.append((f"(+{hidden} hidden commit{'s' if hidden != 1 else ''})", None))
|
||||
return outputs
|
||||
|
||||
|
||||
def _commit_comment(full_name, data):
|
||||
|
||||
+26
-13
@@ -95,9 +95,9 @@ def event_categories(ev):
|
||||
return EVENT_CATEGORIES.get(ev, [ev])
|
||||
|
||||
|
||||
def parse(full_name, ev, data, headers):
|
||||
def parse(full_name, ev, data, headers, commit_limit=3):
|
||||
dispatch = {
|
||||
"push": _push,
|
||||
"push": lambda fn, d: _push(fn, d, commit_limit),
|
||||
"tag_push": _tag_push,
|
||||
"merge_request": _merge_request,
|
||||
"issue": _issues,
|
||||
@@ -112,18 +112,31 @@ def parse(full_name, ev, data, headers):
|
||||
return []
|
||||
|
||||
|
||||
def _push(full_name, data):
|
||||
def _push(full_name, data, commit_limit=3):
|
||||
branch_str = color(data["ref"].rpartition("/")[2], COLOR_BRANCH)
|
||||
author = bold(data["user_username"])
|
||||
commits = data.get("commits", [])
|
||||
outputs = []
|
||||
if len(commits) <= 3:
|
||||
for c in commits:
|
||||
h = color(_short(c["id"]), COLOR_ID)
|
||||
msg = c["message"].split("\n")[0].strip()
|
||||
outputs.append((f"{author} pushed {h} to {branch_str}: {msg}", c["url"]))
|
||||
else:
|
||||
outputs.append((f"{author} pushed {len(commits)} commits to {branch_str}", None))
|
||||
author = bold(data["user_username"])
|
||||
commits = data.get("commits", [])
|
||||
n = len(commits)
|
||||
|
||||
if not commits:
|
||||
return [(f"{author} pushed to {branch_str}", None)]
|
||||
|
||||
# Single commit: one clean line
|
||||
if n == 1:
|
||||
c = commits[0]
|
||||
h = color(_short(c["id"]), COLOR_ID)
|
||||
msg = c["message"].split("\n")[0].strip()
|
||||
return [(f"{author} pushed {h} to {branch_str}: {msg}", c.get("url"))]
|
||||
|
||||
# Multiple commits (GitLab has no compare URL in push payloads)
|
||||
outputs = [(f"{author} pushed {n} commits to {branch_str}", None)]
|
||||
shown = commits[:commit_limit]
|
||||
for c in shown:
|
||||
msg = c["message"].split("\n")[0].strip()
|
||||
outputs.append((f"{author} {_short(c['id'])} - {msg}", c.get("url")))
|
||||
hidden = n - len(shown)
|
||||
if hidden > 0:
|
||||
outputs.append((f"(+{hidden} hidden commit{'s' if hidden != 1 else ''})", None))
|
||||
return outputs
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user