forked from Sniddl/discord-commits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (152 loc) · 5.76 KB
/
index.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const core = require("@actions/core");
const github = require("@actions/github");
const _ = require("lodash");
const axios = require("axios");
const ST = require("stjs");
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
async function run() {
try {
const message = core.getInput("message");
const webhook = core.getInput("webhook");
const webhook_all = core.getInput("webhook_all");
const url = core.getInput("url");
const iconUrl = core.getInput("icon_url") ?? "https://i.imgur.com/eJ08X1Z.png";
const author = core.getInput("author") ?? "Tedeapolis";
const embed =
core.getInput("embed") ||
'{ "title": "{{ commit.title }}", "description": "{{ commit.description }}", "url": "{{ commit.url }}", "author": { "name": "{{ commit.author.name }} ({{ commit.author.username }})", "icon_url": "https://avatars.io/gravatar/{{ commit.author.email }}"} }';
const data = {
env: { ...process.env },
github: { ...github },
};
github.context.payload.commits = github.context.payload.commits || [
{
author: {
email: "[email protected]",
name: "Zeb",
username: "ZebTheWizard",
},
message: "this is a title\n\nthis is a description",
},
{
author: {
email: "[email protected]",
name: "Zeb",
username: "ZebTheWizard",
},
message:
"testing\n" +
"\n" +
"**What is new?**\n" +
"I'm testing out some capability.\n" +
"~what do you think?~\n" +
"```js\n" +
"console.log('asdf');\n" +
"```",
},
];
/**
* @type any[]
*/
const embeds = github.context.payload.commits.filter((commit) => {
const message = commit.message.toLowerCase();
if (message.includes("redacted")) {
return false;
}
if (message.includes("merge")) {
return false;
}
return true;
}).map((commit) => {
const $data = {
...data,
commit: {
title: commit.message.split("\n\n").slice(0, 1).join("\n\n"),
description: commit.message.split("\n\n").slice(1).join("\n\n"),
...commit,
},
};
const parsed = ST.select($data).transformWith(JSON.parse(embed)).root();
return parsed;
});
data.length = embeds.length;
let payload = {}
if (webhook_all) {
const embeds_all = github.context.payload.commits.filter((commit) => {
const message = commit.message.toLowerCase();
if (message.includes("merge")) {
return false;
}
return true;
}).map((commit) => {
const $data = {
...data,
commit: {
title: commit.message.split("\n\n").slice(0, 1).join("\n\n"),
description: commit.message.split("\n\n").slice(1).join("\n\n"),
...commit,
},
};
const parsed = ST.select($data).transformWith(JSON.parse(embed)).root();
return parsed;
});
while (embeds_all.length > 0) {
payload["embeds"] = embeds_all.splice(0, 10);
try {
await axios.post(`${webhook_all}?wait=true`, JSON.stringify(payload), {
headers: {
"Content-Type": "application/json",
"X-GitHub-Event": github.context.eventName || "push",
},
});
} catch (err) {
console.log(JSON.stringify(payload));
console.error("Error :", err);
core.setFailed(
"Message :",
err.response ? err.response.data : err.message
);
}
payload = {}
}
}
if (embeds.length == 0) {
console.log("All messages filtered, cancelling run.");
process.exit(0);
return;
}
embeds.splice(0, 0, {
"description": _.template(message)(data),
"color": 2752256,
"author": {
"name": _.template(author)(data),
"url": _.template(url)(data),
"icon_url": _.template(iconUrl)(data),
}
})
while (embeds.length > 0) {
payload["embeds"] = embeds.splice(0, 10);
try {
await axios.post(`${webhook}?wait=true`, JSON.stringify(payload), {
headers: {
"Content-Type": "application/json",
"X-GitHub-Event": github.context.eventName || "push",
},
});
} catch (err) {
console.log(JSON.stringify(payload));
console.error("Error :", err);
core.setFailed(
"Message :",
err.response ? err.response.data : err.message
);
}
payload = {}
}
console.log("Message sent ! Shutting down ...");
process.exit(0);
} catch (error) {
core.setFailed(error.message);
}
}
run();