forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuesday-bot.ts
78 lines (68 loc) · 2.45 KB
/
tuesday-bot.ts
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
import { setOutput } from '@actions/core';
import { join } from 'path';
import languages from '../src/i18n/languages';
import { TranslationStatusBuilder } from './lib/translation-status/builder';
const PAGE_SOURCE_DIRECTORY = './src/content/docs';
await setDiscordMessage();
async function setDiscordMessage() {
const builder = new TranslationStatusBuilder({
pageSourceDir: './src/content/docs',
htmlOutputFilePath: './dist/translation-status/index.html',
sourceLanguage: 'en',
targetLanguages: Object.keys(languages)
.filter((lang) => lang !== 'en')
.sort(),
languageLabels: languages,
githubRepo: process.env.GITHUB_REPOSITORY || 'withastro/docs',
});
const pages = await builder.createPageIndex();
const statusByPage = builder.getTranslationStatusByPage(pages);
const toTranslate = statusByPage.filter(
(s) => new Date(s.sourcePage.lastMajorChange) > new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)
);
const list = toTranslate
.filter(
(s) =>
Object.keys(s.translations).filter(
(l) => s.translations[l]?.isMissing || s.translations[l]?.isOutdated
).length > 0
)
.map((s) => {
const langs =
Object.keys(s.translations).filter(
(l) => s.translations[l]?.isMissing || s.translations[l]?.isOutdated
).length ===
Object.keys(languages).length - 1
? ['all']
: Object.keys(s.translations);
return `- [\`${s.subpath}\`](<https://github.com/withastro/docs/tree/main/${join(
PAGE_SOURCE_DIRECTORY,
'en',
s.subpath
)}>) (${
langs[0] === 'all'
? 'all'
: langs
.filter((lang) => {
if (lang === 'en') return false;
const { isMissing, isOutdated } = s.translations[lang];
return isMissing || isOutdated;
})
.join(', ')
})`;
})
.join('\n');
let message = `**Translation Tuesday!** <@&951985780828545095>\n\nWe have ${
Object.keys(toTranslate).length
} pages with major changes since last week. Please help us translate these pages to your language!\n\n${list}`;
const suffix =
'\n\nSee our [Translation Status page](<https://i18n.docs.astro.build>) for more, including open PRs.';
// Keep the entire message including the suffix within Discord's limits
const maxLengthWithoutSuffix = 2000 - suffix.length;
while (message.length > maxLengthWithoutSuffix) {
const lastNewline = message.lastIndexOf('\n', maxLengthWithoutSuffix);
message = message.slice(0, lastNewline);
}
message += suffix;
setOutput('DISCORD_MESSAGE', message);
}