-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNodeJsReleases.ts
More file actions
134 lines (117 loc) · 3.45 KB
/
NodeJsReleases.ts
File metadata and controls
134 lines (117 loc) · 3.45 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
import got from 'got';
import { KeyValue } from '../database/index.js';
import { Cron, findTextChannelByName } from '../framework/index.js';
import { SnowflakeUtil } from 'discord.js';
export default new Cron({
enabled: true,
name: 'Node.js Releases',
description:
'Vérifie toutes les 30 minutes si une nouvelle release de Node.js est sortie',
schedule: '5,35 * * * *',
// schedule: '* * * * *',
async handle(context) {
// retrieve last release id from db
const lastRelease = (await KeyValue.get<number>('Last-Cron-Node.js')) ?? 0;
// fetch last releases from gh filtered by releases older or equal than the stored one
const entries = await getLastNodeReleases(lastRelease);
// if no releases return
if (entries.length === 0) return;
context.logger.info(`Found new Node.js releases`, entries);
const channel = findTextChannelByName(context.client.channels, 'news');
for (const release of entries) {
await channel.send({
content: `# Release ${release.title}\n\n<${release.link}>`,
enforceNonce: true,
nonce: SnowflakeUtil.generate().toString(),
});
const content = release.content.replaceAll('\r\n', '\n');
const lines = content.split('\n');
let message = '';
for (const line of lines) {
if (message.length + line.length > 2000) {
const m = await channel.send({
content: message,
enforceNonce: true,
nonce: SnowflakeUtil.generate().toString(),
});
await m.suppressEmbeds(true);
message = '';
}
// Remove the Commits section and after.
if (/#{1,6}\s+Commits?/.test(line)) {
break;
}
message += `\n${line}`;
}
if (message.trim()) {
const m = await channel.send({
content: message,
enforceNonce: true,
nonce: SnowflakeUtil.generate().toString(),
});
await m.suppressEmbeds(true);
}
await channel.send({
content: release.link,
enforceNonce: true,
nonce: SnowflakeUtil.generate().toString(),
});
await KeyValue.set('Last-Cron-Node.js', release.id); // update id in db
}
},
});
interface AtomEntry {
id: number;
link: string;
title: string;
content: string;
date: Date;
author: {
name: string;
image: string;
};
}
interface GithubRelease {
html_url: string;
id: number;
name: string;
draft: boolean;
prerelease: boolean;
published_at: string;
body: string;
author: {
login: string;
avatar_url: string;
};
}
export async function getLastNodeReleases(
skipAfterId: number,
): Promise<AtomEntry[]> {
const releases = await got(
'https://api.github.com/repos/nodejs/node/releases',
).json<GithubRelease[]>();
let shouldSkip = false;
return (
releases
// recent to old (title is a better attribute than date, because it's the updated field)
.sort((a, b) => b.published_at.localeCompare(a.published_at))
.filter((release) => {
if (release.id === skipAfterId) {
shouldSkip = true;
}
return !shouldSkip;
})
.map((release) => ({
id: release.id,
link: release.html_url,
title: release.name,
date: new Date(release.published_at),
content: release.body,
author: {
name: release.author.login,
image: release.author.avatar_url,
},
}))
.toReversed()
);
}