-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
355 lines (284 loc) · 10.7 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const core = require('@actions/core');
const github = require('@actions/github');
const gitToken = core.getInput('token');
const repoToken = core.getInput('repo-token');
const octokit = github.getOctokit(gitToken);
const repoOctokit = github.getOctokit(repoToken);
const fs = require('fs');;
const shell = require('shelljs');
const semver = require('semver');
const base64 = require('js-base64');
const MOD_REPO_NAME = "QuestBeatSaberModsRepo";
const MOD_REPO_OWNER = "QuestModding";
const MAX_FORK_GET_ATTEMPTS = 12;
var modRepo;
var forkedModRepo;
var currentUser;
var notes = [];
async function Main() {
try {
const modJsonPath = core.getInput('mod-json');
if (!fs.existsSync(modJsonPath)) {
throw `File "${modJsonPath}" does not exist`;
}
const modJson = JSON.parse(fs.readFileSync(modJsonPath));
currentUser = (await octokit.rest.users.getByUsername({
username: github.context.repo.owner
})).data;
core.info("Getting Fork of Mod Repo");
try {
forkedModRepo = (await octokit.rest.repos.get({
owner: github.context.repo.owner,
repo: MOD_REPO_NAME
})).data;
} catch {
core.warning("Failed to find a fork of the mod repo, creating it now");
notes.push(`I didn't originally have a fork of the mod repo, so the workflow created one for me`);
core.info("Getting Mod Repo");
try {
modRepo = (await octokit.rest.repos.get({
owner: MOD_REPO_OWNER,
repo: MOD_REPO_NAME
})).data;
} catch {
throw "Failed to retrive the mod repo. Please contact Bobby Shmurner on discord";
}
core.info("Creating Fork");
await repoOctokit.rest.repos.createFork({
owner: MOD_REPO_OWNER,
repo: MOD_REPO_NAME
});
core.info("Getting Fork");
var failCount = 0;
while (true) {
try {
forkedModRepo = (await octokit.rest.repos.get({
owner: github.context.repo.owner,
repo: modRepo.name
})).data;
break;
} catch (error) {
core.info(`Failed: ${error.message}`);
failCount++;
if (failCount > MAX_FORK_GET_ATTEMPTS) {
throw `Failed to get fork of mod repo: Reached max get attempts\nError: ${error.message}`;
}
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
}
if (!forkedModRepo.fork) {
throw `${forkedModRepo.html_url} is not a fork of https://github.com/${MOD_REPO_OWNER}/${MOD_REPO_NAME}`;
}
modRepo = forkedModRepo.parent;
await FetchUpstream(forkedModRepo, modRepo, forkedModRepo.default_branch, modRepo.default_branch);
await CreateBranchIfRequired(modJson.id);
core.info("Cloning fork");
shell.exec(`git clone ${forkedModRepo.html_url}`);
core.info("Getting the repo's mods");
const repoMods = JSON.parse(fs.readFileSync(`${forkedModRepo.name}/mods.json`));
core.info("Adding mod entry to mod repo");
if (!repoMods.hasOwnProperty(modJson.packageVersion)) {
// if (semver.valid(modJson.packageVersion)) {
repoMods[modJson.packageVersion] = [];
const msg = `There were no mods found for the version ${modJson.packageVersion}, so a new verion entry was created`;
core.warning(msg);
notes.push(msg);
// } else {
// throw `Version ${modJson.packageVersion} is invalid!`;
// }
}
var isNewEntry = true;
for (var i = 0; i < repoMods[modJson.packageVersion].length; i++) {
if (repoMods[modJson.packageVersion][i].id == modJson.id) {
core.info("Mod entry alread exists for this version, replacing it with this new entry");
repoMods[modJson.packageVersion].splice(i, 1);
isNewEntry = false;
break;
}
}
repoMods[modJson.packageVersion].push(ConstructModEntry(modJson));
core.info("Encoding modified Mods json");
const encodedRepoMods = base64.encode(JSON.stringify(repoMods, null, 4));
core.info("Commiting moddfied Mods json");
var commit = {
owner: forkedModRepo.owner.login,
repo: forkedModRepo.name,
path: "mods.json",
message: `Added ${modJson.name} v${modJson.version} to the Mod Repo`,
content: encodedRepoMods,
branch: `refs/heads/${modJson.id}`
};
const sha = await GetFileSHA(modJson.id);
if (sha != null) {
commit.sha = sha;
}
await repoOctokit.rest.repos.createOrUpdateFileContents(commit);
core.info("Checking if Pull Request Exists for branch");
var prs = await GetPRs(`${forkedModRepo.owner.login}:${modJson.id}`);
var prTitle = "";
var prMessage = "";
if (isNewEntry && prs.length == 0) {
prTitle = `Added ${modJson.name} v${modJson.version} to the mod repo`;
prMessage = `Added ${modJson.name} v${modJson.version} to the mod repo for Beat Saber version ${modJson.packageVersion}.`;
} else {
prTitle = `Updated ${modJson.name} to v${modJson.version}`;
prMessage = `Updated ${modJson.name} to v${modJson.version} for Beat Saber version ${modJson.packageVersion}.`;
}
if (core.getInput('tag') != '') prMessage += `\n\nYou can check out the release [Here](https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/releases/tag/${core.getInput('tag')})`;
prMessage += `\nYou can download the QMod [Here](https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/releases/download/${core.getInput('tag')}/${core.getInput('qmod-name')})`;
prMessage += `\nYou can check out the build action [Here](https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId})`;
if (notes.length > 0) {
prMessage += "\n\n**Notes:**";
for (const note of notes) {
prMessage += `\n- ${note}`;
}
}
if (core.getInput('note') != '') {
prMessage += `\n- ${core.getInput('note')}`;
}
if (prs.length > 0) {
core.info("PR alread exists. Will just be adding a message to the existing PR");
await repoOctokit.rest.issues.createComment({
owner: modRepo.owner.login,
repo: modRepo.name,
issue_number: prs[0].number,
body: prMessage
})
} else {
core.info("No PR found, creating one now");
await repoOctokit.rest.pulls.create({
owner: modRepo.owner.login,
repo: modRepo.name,
title: prTitle,
head: `${forkedModRepo.owner.login}:${modJson.id}`,
base: modRepo.default_branch,
body: prMessage,
maintainer_can_modify: true
})
prs = await GetPRs(`${forkedModRepo.owner.login}:${modJson.id}`);
}
core.info(`\nSuccess! PR can be found here: ${prs[0].html_url}`);
} catch (error) {
core.setFailed(error);
}
}
async function CreateBranchIfRequired(branchName) {
core.info(`Checking if "${branchName}" branch exists`);
try {
await octokit.rest.git.getRef({
owner: forkedModRepo.owner.login,
repo: forkedModRepo.name,
ref: `heads/${branchName}`
});
core.info("Branch already exists");
} catch {
core.info("Branch does not exists, creating it now");
const sha = (await octokit.rest.git.getRef({
owner: forkedModRepo.owner.login,
repo: forkedModRepo.name,
ref: `heads/${forkedModRepo.default_branch}`
})).data.object.sha;
await repoOctokit.rest.git.createRef({
owner: forkedModRepo.owner.login,
repo: forkedModRepo.name,
ref: `refs/heads/${branchName}`,
sha: sha
})
return;
}
// This will only run if the branch already existed, as there's a return in the catch statement
await FetchUpstream(forkedModRepo, forkedModRepo, branchName, forkedModRepo.default_branch);
}
async function FetchUpstream(repo, upstreamRepo, branch, upstreamBranch) {
core.info(`Checking if ${repo.owner.login}:${branch} is behind ${upstreamRepo.owner.login}:${upstreamBranch}`);
const compareResults = (await octokit.rest.repos.compareCommits({
owner: upstreamRepo.owner.login,
repo: upstreamRepo.name,
base: upstreamBranch,
head: `${repo.owner.login}:${branch}`
})).data;
if (compareResults.behind_by > 0) {
core.info(`${repo.owner.login}:${branch} is behind by ${compareResults.behind_by} commits. Fetching Upstream...`);
const upstreamBranchReference = (await octokit.rest.git.getRef({
owner: upstreamRepo.owner.login,
repo: upstreamRepo.name,
ref: `heads/${upstreamBranch}`
})).data;
try {
await repoOctokit.rest.git.updateRef({
owner: repo.owner.login,
repo: repo.name,
ref: `heads/${branch}`,
sha: upstreamBranchReference.object.sha,
force: true
})
} catch (error) {
throw `Failed to fetch upstream. This can be fixed by performing a manual merge\nError: ${error.message}`;
}
} else {
core.info(`${repo.owner.login}:${branch} is up-to-date`);
}
}
function ConstructModEntry(modJson) {
var cover = core.getInput('cover');
var authors = core.getInput('authors');
var authorIcon = core.getInput('author-icon');
var downloadLink = core.getInput('qmod-url');
if (cover == '') {
if (!fs.existsSync('cover.png')) {
const msg = '"cover" was not specified, and "cover.png" could not be found in the root of the repo, so no cover was set';
core.warning(msg);
notes.push(msg);
} else {
cover = `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/raw/${github.context.ref}/cover.png`
}
}
if (authors == '') {
authors = modJson.author;
}
if (authorIcon == '') {
authorIcon = currentUser.avatar_url;
}
if (downloadLink == '') {
if (core.getInput('tag') == '') throw '"tag" and "qmod-url" were not specified. Please specify at least one';
downloadLink = `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/releases/download/${core.getInput('tag')}/${core.getInput('qmod-name')}`;
}
core.info(authors.split(", "));
const modEntry = {
name: modJson.name,
description: modJson.description,
id: modJson.id,
version: modJson.version,
downloadLink: downloadLink,
source: `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/`,
cover: cover,
authorIcon: authorIcon,
authors: authors.split(", ")
}
return modEntry;
}
async function GetFileSHA(branchName) {
try {
const result = await octokit.rest.repos.getContent({
owner: forkedModRepo.owner.login,
repo: forkedModRepo.name,
path: "mods.json",
ref: `refs/heads/${branchName}`
});
return result.data.sha;
} catch (error) {
core.warning(`Failed to get the SHA of the repo's "mods.json"\nError: ${error.message}`);
notes.push(`Failed to get the SHA of the fork's "mods.json", which means a new file was created. This *shouldn't* cause any issues, but it should still be noted`);
return null;
}
}
async function GetPRs(head) {
return (await octokit.rest.pulls.list({
owner: modRepo.owner.login,
repo: modRepo.name,
state: "open",
head: head
})).data;
}
Main();