-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbot.ts
More file actions
199 lines (168 loc) · 4.98 KB
/
Copy pathbot.ts
File metadata and controls
199 lines (168 loc) · 4.98 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { Mwn } from "mwn";
import { MwnMissingPageError } from "mwn/build/error";
import * as dotenv from "dotenv";
import * as fs from "fs";
import * as crypto from "crypto";
dotenv.config();
const commitMessage = process.env.COMMIT_MESSAGE ?? "Update templates";
const commitAuthorName = process.env.COMMIT_AUTHOR_NAME;
const isDryRun = (process.env.BOT_DRY_RUN ?? "").toLowerCase() === "true";
type Wiki = {
apiUrl: string;
accessToken: string;
allianceLogoUrl?: string;
};
type Template = {
filePath: string;
pageName: string;
};
type File = {
filePath: string;
wikiFileName: string;
fileHash?: string;
};
import wikisData from "./data/wikis.json";
import templatesData from "./data/templates.json";
import filesData from "./data/files.json";
const wikis: Wiki[] = wikisData;
const templates: Template[] = templatesData;
const files: File[] = filesData;
let success = true;
for (const file of files) {
file.fileHash = computeLocalSHA1(file.filePath);
}
async function updateTemplateOnWiki(wiki: Wiki) {
const accessToken = process.env[wiki.accessToken];
if (!accessToken) {
throw new Error(`❌ Failed to find access token for wiki ${wiki.apiUrl}!`);
}
const bot = await Mwn.init({
apiUrl: wiki.apiUrl,
OAuth2AccessToken: accessToken,
userAgent:
"RobloxWikiAllianceBot/1.0 (https://github.com/Roblox-Indie-Wikis/Templates)",
defaultParams: {
assert: "user",
},
});
for (const template of templates) {
let content = fs.readFileSync(template.filePath, "utf-8");
// only take the first line of the commit
let editSummary = commitMessage.split("\n")[0];
if (commitAuthorName) {
editSummary += ` (${commitAuthorName})`;
}
if (wiki.allianceLogoUrl) {
// replace url on 3rd party wikis
content = content.replace(
"https://static.wikitide.net/commonswiki/e/e0/IRWA_Logo_Black.svg",
wiki.allianceLogoUrl
);
}
try {
await bot.edit(template.pageName, async (rev) => {
if (rev.content.trim() === content.trim()) {
console.log(
`✅ ${template.pageName} on wiki ${wiki.apiUrl} is up to date!`
);
return;
}
console.log(`ℹ️ Updating ${template.pageName} on wiki ${wiki.apiUrl} ...`);
if (isDryRun) {
console.log('🪲 (Skipped) New content:', content);
return;
}
return {
text: content,
summary: editSummary,
bot: true,
nocreate: false,
};
});
} catch (error) {
if (error instanceof MwnMissingPageError) {
console.log(`ℹ️ Creating ${template.pageName} on wiki ${wiki.apiUrl} ...`);
if (isDryRun) {
console.log('🪲 (Skipped) New content:', content);
continue;
}
try {
await bot.create(template.pageName, content, editSummary);
} catch (error) {
success = false;
console.error(
`❌ Error creating ${template.pageName} on wiki ${wiki.apiUrl}:`,
error
);
}
continue;
}
success = false;
console.error(
`❌ Error updating ${template.pageName} on wiki ${wiki.apiUrl}:`,
error
);
}
}
for (const file of files) {
const response = await bot.request({
action: "query",
prop: "imageinfo",
titles: `File:${file.wikiFileName}`,
iiprop: "sha1",
format: "json",
});
/* eslint-disable @typescript-eslint/no-explicit-any */
const result = Object.values(response.query.pages)[0] as any;
if (
!result.missing &&
result.imageinfo &&
result.imageinfo[0] &&
result.imageinfo[0].sha1 === file.fileHash
) {
console.log(
`✅ File ${file.wikiFileName} on wiki ${wiki.apiUrl} is up to date!`
);
continue;
}
console.log(`ℹ️ Updating file ${file.wikiFileName} on wiki ${wiki.apiUrl} ...`);
if (isDryRun) {
console.log('🪲 (Skipped)');
continue;
}
try {
await bot.upload(file.filePath, file.wikiFileName, "Update file");
} catch (error) {
success = false;
console.error(
`❌ Error updating file ${file.wikiFileName} on wiki ${wiki.apiUrl}:`,
error
);
}
}
}
function computeLocalSHA1(filepath: string): string {
const fileBuffer = fs.readFileSync(filepath);
return crypto.createHash("sha1").update(fileBuffer).digest("hex");
}
function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
(async () => {
for (const wiki of wikis) {
try {
await updateTemplateOnWiki(wiki);
} catch (error) {
success = false;
console.error(`⚠️ Error updating ${wiki.apiUrl}:`, error);
}
// wait 1s between each wiki
await delay(1000);
}
if (success) {
console.log("✅ All wikis have been updated successfully!");
} else {
console.error("⚠️ Some wikis have not been updated successfully!");
process.exit(1);
}
})();