Skip to content

Commit 6d7271b

Browse files
committed
ci: improve discord notification embed
1 parent 15777b9 commit 6d7271b

1 file changed

Lines changed: 97 additions & 27 deletions

File tree

.github/scripts/notify-discord.py

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,57 +51,90 @@ def build_push_payload() -> dict[str, object]:
5151
repository = os.getenv("GITHUB_REPOSITORY", event.get("repository", {}).get("full_name", "unknown/repo"))
5252
branch = os.getenv("GITHUB_REF_NAME", event.get("ref", "").replace("refs/heads/", ""))
5353
actor = os.getenv("GITHUB_ACTOR", event.get("pusher", {}).get("name", "unknown"))
54-
compare_url = event.get("compare") or repository_url(repository)
54+
repo_url = repository_url(repository)
55+
compare_url = event.get("compare") or repo_url
5556
commits = event.get("commits", [])
57+
commits = commits if isinstance(commits, list) else []
5658
head = event.get("head_commit") or {}
5759
head_message = first_line(str(head.get("message", "No commit message.")), 180)
5860
head_sha = str(head.get("id", os.getenv("GITHUB_SHA", "")))[:7]
61+
repo_name = repository.rsplit("/", 1)[-1]
62+
owner = repository.split("/", 1)[0] if "/" in repository else actor
63+
count = len(commits)
64+
commit_word = "commit" if count == 1 else "commits"
65+
title = f"[{repo_name}:{branch or 'unknown'}] {count} new {commit_word}"
66+
if count == 0:
67+
title = f"[{repo_name}:{branch or 'unknown'}] new push"
68+
69+
lines = [
70+
f"Repository: [{repository}]({repo_url}) | Branch: [{branch or 'unknown'}]({branch_url(repository, branch)})",
71+
"",
72+
]
73+
commit_lines = format_commit_lines(commits, repo_url, actor)
74+
if commit_lines:
75+
lines.extend(commit_lines)
76+
else:
77+
lines.append(f"[`{head_sha}`]({commit_url(repo_url, str(head.get('id', os.getenv('GITHUB_SHA', ''))))}) {head_message} - {actor}")
78+
79+
embed = {
80+
"author": {
81+
"name": owner,
82+
"url": f"https://github.com/{owner}",
83+
"icon_url": github_avatar_url(owner),
84+
},
85+
"title": title,
86+
"url": compare_url,
87+
"description": "\n".join(lines),
88+
"color": 0x2F81F7,
89+
"footer": {"text": f"pushed by {actor}"},
90+
}
91+
timestamp = str(head.get("timestamp", "")).strip()
92+
if timestamp != "":
93+
embed["timestamp"] = timestamp
5994

6095
return {
61-
"username": "ImperaZim Repository",
62-
"embeds": [
63-
{
64-
"title": f"Push: {repository}",
65-
"url": compare_url,
66-
"description": head_message,
67-
"color": 0x3BA55D,
68-
"fields": [
69-
{"name": "Branch", "value": branch or "unknown", "inline": True},
70-
{"name": "Actor", "value": actor, "inline": True},
71-
{"name": "Commits", "value": str(len(commits)), "inline": True},
72-
{"name": "Head", "value": f"`{head_sha}`", "inline": True},
73-
],
74-
}
75-
],
96+
"username": owner,
97+
"avatar_url": github_avatar_url(owner),
98+
"embeds": [embed],
7699
}
77100

78101

79102
def build_release_payload() -> dict[str, object]:
80103
repository = os.getenv("GITHUB_REPOSITORY", "unknown/repo")
104+
owner = repository.split("/", 1)[0] if "/" in repository else "ImperaZim"
81105
name = os.getenv("RELEASE_NAME", repository.rsplit("/", 1)[-1])
82106
version = os.getenv("RELEASE_VERSION", "").strip()
83107
tag = os.getenv("RELEASE_TAG", os.getenv("GITHUB_REF_NAME", "")).strip()
84108
assets = [asset.strip() for asset in os.getenv("RELEASE_ASSETS", "").split(",") if asset.strip()]
85-
release_url = f"{repository_url(repository)}/releases/tag/{tag}" if tag else repository_url(repository)
86-
title = f"Release: {name}" + (f" {version}" if version else "")
87-
88-
fields = [
89-
{"name": "Repository", "value": repository, "inline": True},
90-
{"name": "Tag", "value": tag or "unknown", "inline": True},
91-
{"name": "Trigger", "value": os.getenv("GITHUB_EVENT_NAME", "unknown"), "inline": True},
109+
repo_url = repository_url(repository)
110+
release_url = f"{repo_url}/releases/tag/{tag}" if tag else repo_url
111+
display_version = version or tag or "release"
112+
title = f"[{name}:{display_version}] release assets published"
113+
description = [
114+
f"Repository: [{repository}]({repo_url})",
115+
f"Tag: [`{tag or 'unknown'}`]({release_url})",
116+
"",
117+
"Release assets were published and verified.",
92118
]
93119
if assets:
94-
fields.append({"name": "Assets", "value": "\n".join(f"`{Path(asset).name}`" for asset in assets), "inline": False})
120+
description.extend(["", "Assets:"])
121+
description.extend(f"- `{Path(asset).name}`" for asset in assets)
95122

96123
return {
97-
"username": "ImperaZim Releases",
124+
"username": owner,
125+
"avatar_url": github_avatar_url(owner),
98126
"embeds": [
99127
{
128+
"author": {
129+
"name": owner,
130+
"url": f"https://github.com/{owner}",
131+
"icon_url": github_avatar_url(owner),
132+
},
100133
"title": title,
101134
"url": release_url,
102-
"description": "Release assets were published and verified.",
135+
"description": "\n".join(description),
103136
"color": 0x5865F2,
104-
"fields": fields,
137+
"footer": {"text": f"triggered by {os.getenv('GITHUB_ACTOR', 'GitHub Actions')}"},
105138
}
106139
],
107140
}
@@ -124,6 +157,43 @@ def repository_url(repository: str) -> str:
124157
return f"{os.getenv('GITHUB_SERVER_URL', 'https://github.com')}/{repository}"
125158

126159

160+
def branch_url(repository: str, branch: str) -> str:
161+
if branch == "":
162+
return repository_url(repository)
163+
return f"{repository_url(repository)}/tree/{branch}"
164+
165+
166+
def commit_url(repository_url_value: str, sha: str) -> str:
167+
if sha == "":
168+
return repository_url_value
169+
return f"{repository_url_value}/commit/{sha}"
170+
171+
172+
def github_avatar_url(owner: str) -> str:
173+
return f"https://github.com/{owner}.png?size=128"
174+
175+
176+
def format_commit_lines(commits: list[object], repo_url: str, fallback_author: str) -> list[str]:
177+
lines = []
178+
for commit in commits[:5]:
179+
if not isinstance(commit, dict):
180+
continue
181+
sha = str(commit.get("id", ""))[:7]
182+
full_sha = str(commit.get("id", ""))
183+
message = first_line(str(commit.get("message", "No commit message.")), 160)
184+
url = str(commit.get("url", "")) or commit_url(repo_url, full_sha)
185+
author_data = commit.get("author", {})
186+
author = fallback_author
187+
if isinstance(author_data, dict):
188+
author = str(author_data.get("username") or author_data.get("name") or fallback_author)
189+
lines.append(f"[`{sha}`]({url}) {message} - {author}")
190+
191+
remaining = len(commits) - len(lines)
192+
if remaining > 0:
193+
lines.append(f"...and {remaining} more.")
194+
return lines
195+
196+
127197
def first_line(value: str, max_length: int) -> str:
128198
line = value.splitlines()[0] if value.splitlines() else value
129199
return line if len(line) <= max_length else line[: max_length - 3] + "..."

0 commit comments

Comments
 (0)