forked from nisovin/MagicSpells
-
Notifications
You must be signed in to change notification settings - Fork 72
147 lines (127 loc) · 5.5 KB
/
Copy pathwiki.yml
File metadata and controls
147 lines (127 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
name: Wiki Alert
# The alert is not sent for all wiki changes:
#
# The "gollum" event only fires whenever the wiki HEAD changes, only considering the
# changes in the latest commit, and only if the pages were created or modified, not
# deleted. But pushing multiple commits is limited to GitHub collaborators (or above).
on: gollum
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Secret Check
id: secret
run: echo "empty=${{secrets.WIKI_WEBHOOK == ''}}" >> $GITHUB_OUTPUT
- name: Checkout
if: steps.secret.outputs.empty == 'false'
uses: actions/checkout@v6
with:
repository: ${{github.repository}}.wiki
- name: Send Webhook
if: steps.secret.outputs.empty == 'false'
uses: actions/github-script@v9
with:
script: |
// https://docs.discord.com/developers/resources/message#embed-object-embed-limits
const limits = {
message: 6000,
description: 4096,
embeds: 10,
fields: 25,
};
const messageConfig = {
username: "Wiki Updates",
avatar: "https://files.jasperlorelai.eu/magicspells/images/webhook_icon.png"
};
/**
* @typedef {Object} EmbedFooter
* @property {string=} text
*/
/**
* @typedef {Object} EmbedAuthor
* @property {string=} name
* @property {string=} url
* @property {string=} url_icon
*/
/**
* @typedef {Object} EmbedField
* @property {string} name
* @property {string} value
* @property {boolean=} inline
*/
/**
* @typedef {Object} Embed
* @property {string=} title
* @property {string=} url
* @property {string=} description
* @property {EmbedFooter=} footer
* @property {EmbedAuthor=} author
* @property {EmbedField[]=} fields
*/
if (!context.payload.pages?.length) {
core.info("No pages in payload.");
return;
}
// Commit sha/message will be shared across pages in a "gollum" event.
const {sha} = context.payload.pages[0];
const {stdout} = await exec.getExecOutput(`git log -1 --pretty=format:"%B" ${sha}`);
let description = "";
for (const line of stdout.trim().split("\n")) {
description += "\n> " + line;
}
if (description.length > limits.description) {
description = description.substring(0, limits.description - 3) + "...";
}
// Array of batched 10 max embeds per element.
/** @type {Array<Embed[]>} */
const batches = [];
for (const {title, html_url} of context.payload.pages) {
/** @type {EmbedField} */
const field = {
inline: true,
name: title,
value: `-# [Page](${html_url}/${sha}) • [Changes](${html_url}/_compare/${sha})`
};
let messageSize = field.name.length + field.value.length;
if (batches.length) {
for (const embed of batches.at(-1)) {
messageSize += (embed.title?.length || 0) +
(embed.description?.length || 0) +
(embed.footer?.text?.length || 0) +
(embed.author?.name?.length || 0);
if (!embed.fields?.length) continue;
for (const field of embed.fields) {
messageSize += field.name.length + field.value.length;
}
}
}
const needsNewBatch = !batches.length ||
batches.at(-1).length >= limits.embeds ||
messageSize >= limits.message;
if (needsNewBatch) batches.push([{
title: "Pages Changed",
url: `${html_url.substring(0, html_url.lastIndexOf("/"))}/_compare/${sha}`,
description,
author: {
name: "Edited by: " + context.payload.sender.login,
url: context.payload.sender.html_url,
icon_url: context.payload.sender.avatar_url,
},
fields: []
}]);
const needsNewEmbed = batches.at(-1).at(-1).fields.length >= limits.fields;
if (needsNewEmbed) batches.at(-1).push({fields: []});
batches.at(-1).at(-1).fields.push(field);
}
for (const embeds of batches) {
const message = {
username: messageConfig.username,
avatar_url: messageConfig.avatar,
embeds
};
await fetch("${{secrets.WIKI_WEBHOOK}}", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(message)
}).catch(error => core.setFailed(error.message));
}