Skip to content

Commit 557a35d

Browse files
committed
improving the notification messages
1 parent 603b3d1 commit 557a35d

1 file changed

Lines changed: 113 additions & 15 deletions

File tree

web/github.php

Lines changed: 113 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@
7676
$ChatMsg = "🐛 [{$User}](https://github.com/{$User}) **{$Action}** issue [#{$Issue['number']} {$IssueTitle}]({$IssueUrl}) "
7777
. "in [{$RepositoryName}](https://github.com/{$RepositoryName}).";
7878

79+
if (!empty($Issue['labels'])) {
80+
$Labels = array_map(fn($l) => "`{$l['name']}`", $Issue['labels']);
81+
$ChatMsg .= " " . implode(' ', $Labels);
82+
}
83+
84+
if ($Action === 'closed' && !empty($Issue['state_reason'])) {
85+
$ChatMsg .= " _(reason: {$Issue['state_reason']})_";
86+
}
87+
88+
if ($Action === 'edited' && !empty($Message['changes'])) {
89+
$Changed = array_keys($Message['changes']);
90+
$ChatMsg .= " _(changed: " . implode(', ', $Changed) . ")_";
91+
}
92+
7993
if (!empty($Issue['body'])) {
8094
$ChatMsg .= "\n\n> " . substr($Issue['body'], 0, 500) . (strlen($Issue['body']) > 500 ? "" : "");
8195
}
@@ -89,10 +103,41 @@
89103
$Action = $Message['action'];
90104
$PRUrl = $PR['html_url'];
91105
$PRTitle = $PR['title'];
106+
$HeadBranch = $PR['head']['ref'] ?? '';
107+
$BaseBranch = $PR['base']['ref'] ?? '';
92108

93109
$ChatMsg = "🔀 [{$User}](https://github.com/{$User}) **{$Action}** pull request "
94110
. "[#{$PR['number']} {$PRTitle}]({$PRUrl}) "
95-
. "in [{$RepositoryName}](https://github.com/{$RepositoryName}).";
111+
. "in [{$RepositoryName}](https://github.com/{$RepositoryName})";
112+
113+
if ($HeadBranch && $BaseBranch) {
114+
$ChatMsg .= " (`{$HeadBranch}` → `{$BaseBranch}`)";
115+
}
116+
$ChatMsg .= ".";
117+
118+
if (!empty($PR['draft'])) {
119+
$ChatMsg .= " _(draft)_";
120+
}
121+
122+
if ($Action === 'closed' && !empty($PR['merged'])) {
123+
$ChatMsg .= " ✅ Merged.";
124+
}
125+
126+
if (in_array($Action, ['opened', 'reopened', 'closed']) && isset($PR['commits'])) {
127+
$Stats = [];
128+
if (!empty($PR['commits'])) {
129+
$Stats[] = "{$PR['commits']} commit" . ($PR['commits'] !== 1 ? "s" : "");
130+
}
131+
if (isset($PR['additions'], $PR['deletions'])) {
132+
$Stats[] = "+{$PR['additions']}/-{$PR['deletions']}";
133+
}
134+
if (!empty($PR['changed_files'])) {
135+
$Stats[] = "{$PR['changed_files']} file" . ($PR['changed_files'] !== 1 ? "s" : "");
136+
}
137+
if (!empty($Stats)) {
138+
$ChatMsg .= " _(" . implode(', ', $Stats) . ")_";
139+
}
140+
}
96141

97142
if (!empty($PR['body'])) {
98143
$ChatMsg .= "\n\n> " . substr($PR['body'], 0, 500) . (strlen($PR['body']) > 500 ? "" : "");
@@ -109,8 +154,14 @@
109154

110155
if (!empty($Message['commits'])) {
111156
foreach ($Message['commits'] as $Commit) {
112-
$Commits[] = "• [".substr($Commit['id'], 0, 7)."]({$Commit['url']}) _{$Commit['message']}_ "
113-
. "by {$Commit['author']['name']}";
157+
$CommitMsg = strtok($Commit['message'], "\n");
158+
$FileCounts = [];
159+
if (!empty($Commit['added'])) $FileCounts[] = '+' . count($Commit['added']);
160+
if (!empty($Commit['modified'])) $FileCounts[] = '~' . count($Commit['modified']);
161+
if (!empty($Commit['removed'])) $FileCounts[] = '-' . count($Commit['removed']);
162+
$FileInfo = !empty($FileCounts) ? ' _(' . implode(' ', $FileCounts) . ' files)_' : '';
163+
$Commits[] = "• [" . substr($Commit['id'], 0, 7) . "]({$Commit['url']}) _{$CommitMsg}_ "
164+
. "by {$Commit['author']['name']}{$FileInfo}";
114165
}
115166
}
116167

@@ -119,6 +170,10 @@
119170
. " to [{$RepositoryName}](https://github.com/{$RepositoryName}) "
120171
. "[`{$Branch}`](https://github.com/{$RepositoryName}/tree/{$Branch})";
121172

173+
if (!empty($Message['compare'])) {
174+
$ChatMsg .= " ([compare]({$Message['compare']}))";
175+
}
176+
122177
if (!empty($Commits)) {
123178
$ChatMsg .= "\n" . implode("\n", $Commits);
124179
}
@@ -132,34 +187,77 @@
132187
$Status = $Message['check_suite']['conclusion'] ?? $Message['check_run']['conclusion'] ?? 'in_progress';
133188
$Name = $Message['check_suite']['app']['name'] ?? $Message['check_run']['name'];
134189
$Url = $Message['check_suite']['url'] ?? $Message['check_run']['html_url'];
190+
$Branch = $Message['check_suite']['head_branch'] ?? $Message['check_run']['check_suite']['head_branch'] ?? '';
191+
$CommitMsg = $Message['check_suite']['head_commit']['message'] ?? $Message['check_run']['check_suite']['head_commit']['message'] ?? '';
135192

136193
$Emoji = $Status === 'success' ? "" : ($Status === 'failure' ? "" : "");
137194
$ChatMsg = "{$Emoji} Check **{$Name}** {$Status} "
138-
. "for [{$RepositoryName}](https://github.com/{$RepositoryName}) "
139-
. "([details]({$Url}))";
195+
. "for [{$RepositoryName}](https://github.com/{$RepositoryName})";
196+
if ($Branch) {
197+
$ChatMsg .= " on `{$Branch}`";
198+
}
199+
$ChatMsg .= " ([details]({$Url}))";
200+
if ($CommitMsg) {
201+
$CommitMsg = strtok($CommitMsg, "\n");
202+
$ChatMsg .= "\n> _{$CommitMsg}_";
203+
}
140204

141205
$Msg['text'] = $ChatMsg;
142206
//SendToChat('notifications', $Msg, $useRC, $useTeams);
143207
break;
144208

145209
case 'workflow_run':
146210
case 'workflow_job':
147-
$Workflow = $Message['workflow']['name'] ?? $Message['workflow_job']['name'] ?? "Workflow";
148-
$Status = $Message['workflow_run']['conclusion'] ?? $Message['workflow_job']['conclusion'] ?? "in_progress";
149-
$Url = $Message['workflow_run']['html_url'] ?? "#";
150-
$Emoji = $Status === 'success' ? "" : ($Status === 'failure' ? "" : "");
211+
$WorkflowName = $Message['workflow_run']['name'] ?? $Message['workflow']['name'] ?? $Message['workflow_job']['workflow_name'] ?? $Message['workflow_job']['name'] ?? "Workflow";
212+
$Status = $Message['workflow_run']['conclusion'] ?? $Message['workflow_job']['conclusion'] ?? $Message['workflow_run']['status'] ?? $Message['workflow_job']['status'] ?? "in_progress";
213+
$Url = $Message['workflow_run']['html_url'] ?? $Message['workflow_job']['html_url'] ?? "#";
214+
$Branch = $Message['workflow_run']['head_branch'] ?? $Message['workflow_job']['head_branch'] ?? '';
215+
$Emoji = $Status === 'success' ? "" : ($Status === 'failure' ? "" : ($Status === 'in_progress' ? "" : "🔄"));
151216

152-
$ChatMsg = "{$Emoji} Workflow **{$Workflow}** {$Status} "
153-
. "for [{$RepositoryName}](https://github.com/{$RepositoryName}) "
154-
. "([view run]({$Url}))";
217+
$ChatMsg = "{$Emoji} Workflow **{$WorkflowName}** {$Status} "
218+
. "for [{$RepositoryName}](https://github.com/{$RepositoryName})";
219+
if ($Branch) {
220+
$ChatMsg .= " on `{$Branch}`";
221+
}
222+
$ChatMsg .= " ([view run]({$Url}))";
223+
224+
if ($EventType === 'workflow_run' && !empty($Message['workflow_run']['head_commit']['message'])) {
225+
$HeadCommitMsg = strtok($Message['workflow_run']['head_commit']['message'], "\n");
226+
$ChatMsg .= "\n> _{$HeadCommitMsg}_";
227+
}
228+
229+
if ($EventType === 'workflow_job' && !empty($Message['workflow_job']['steps'])) {
230+
foreach ($Message['workflow_job']['steps'] as $Step) {
231+
if ($Step['status'] === 'in_progress') {
232+
$ChatMsg .= "\n> Step: _{$Step['name']}_";
233+
break;
234+
}
235+
}
236+
}
155237

156238
$Msg['text'] = $ChatMsg;
157239
//SendToChat('notifications', $Msg, $useRC, $useTeams);
158240
break;
159241
case 'gollum':
160-
$ChatMsg = "ℹ️ {$Msg['alias']} triggered a **{$EventType}** event "
161-
. (isset($Message['data']['pages'][0]['action']) ? "({$Message['data']['pages'][0]['action']} [{$Message['data']['pages'][0]['title']}]({$Message['data']['pages'][0]['html_url']})) " : "")
162-
. "on [{$RepositoryName}](https://github.com/{$RepositoryName}).";
242+
$Pages = $Message['pages'] ?? [];
243+
if (!empty($Pages)) {
244+
$PageLines = [];
245+
foreach ($Pages as $Page) {
246+
$PageAction = $Page['action'] ?? 'updated';
247+
$PageTitle = $Page['title'] ?? 'Unknown';
248+
$PageUrl = $Page['html_url'] ?? '';
249+
$Summary = !empty($Page['summary']) ? "{$Page['summary']}" : '';
250+
$PageLines[] = "• **{$PageAction}** [{$PageTitle}]({$PageUrl}){$Summary}";
251+
}
252+
$PageCount = count($Pages);
253+
$ChatMsg = "📝 [{$User}](https://github.com/{$User}) updated {$PageCount} wiki "
254+
. ($PageCount === 1 ? "page" : "pages")
255+
. " on [{$RepositoryName}](https://github.com/{$RepositoryName}):\n"
256+
. implode("\n", $PageLines);
257+
} else {
258+
$ChatMsg = "📝 [{$User}](https://github.com/{$User}) updated the wiki "
259+
. "on [{$RepositoryName}](https://github.com/{$RepositoryName}).";
260+
}
163261
$Msg['text'] = $ChatMsg;
164262
SendToChat('notifications', $Msg, $useRC, $useTeams);
165263
break;

0 commit comments

Comments
 (0)