forked from wdw21/songbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
540 lines (477 loc) · 18.4 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import {oauthAuthorizationUrl} from "@octokit/oauth-authorization-url";
import {http} from '@google-cloud/functions-framework';
import {RequestError} from "@octokit/request-error";
import express from 'express';
import util from 'util';
import crypto from 'crypto';
import cookieParser from "cookie-parser";
import cors from 'cors';
import * as fs from 'fs';
import * as path from "path";
import libxmljs2 from "libxmljs2";
import {cleanupChanges, listChanges} from './listChanges.js';
import {
BASE_URL,
EDITOR_DOMAIN,
CHANGES_BASE_URL,
CONFIG_BASE_URL,
OAUTH_CLIENT_ID,
newUserOctokit,
getFileFromBranch,
htmlSuffix,
htmlPrefix,
MAIN_BRANCH_NAME,
prepareBranch, editorLink,
prepareMainBranch,
HandleError, EDITOR_BASE_URL, PARENT_DOMAIN, clearCookiesAndAuthRedirect, REDIRECT_BASE_URL,
} from './common.js';
const app = express();
app.use(cookieParser());
app.use(cors({origin: EDITOR_DOMAIN, credentials: true}));
//app.use(logRequest)
//app.use(errorHandleWrapper)
//app.use(errorHandle)
// app.use((err, req, res, next) => {
//
// next(res);
// })
app.get('/', async (req, res) => {
if (req.cookies.new === 'false') {
res.redirect(CHANGES_BASE_URL);
} else {
res.redirect("/intro");
}
});
app.get('/changes', async (req, res) => {
const {octokit, user} =
await newUserOctokit(req, res);
if (!octokit) {
return;
}
res.redirect(`/users/${user}/changes`)
});
app.get('/songs', async (req, res) => {
const {octokit, user} = await newUserOctokit(req, res);
if (!octokit) return;
res.redirect(`/users/${user}/songs`)
});
app.use(express.static('static'));
app.get('/users/:user/changes', listChanges);
app.post('/users/:user/changes[:]cleanup', cleanupChanges);
app.get('/users/:user/changes/:branch[:]edit', async (req, res) => {
const branchName = req.params.branch;
const {octokit, user} = await newUserOctokit(req, res);
if (!octokit) return;
let file = await getFileFromBranch(octokit, user, branchName);
res.redirect(editorLink(user, branchName, file, false, false));
});
app.delete('/users/:user/changes/:branch', async (req, res) => {
const branchName = req.params.branch;
const {octokit, user} = await newUserOctokit(req, res);
if (!octokit) return;
await octokit.rest.git.deleteRef({owner: user, repo: 'songbook', "ref": "heads/" + branchName});
res.send("Deleted");
});
async function getPullRequest(octokit, user, branchName) {
const res = await octokit.request('GET /repos/{owner}/{repo}/pulls{?state,head,base,sort,direction,per_page,page}', {
owner: 'wdw21',
repo: 'songbook',
head: `${user}:${branchName}`,
})
console.log("getPullRequest: " + JSON.stringify(res))
return res.status=200 && res.data.length>0 ? res.data[0] : null
}
async function getCommitsDifferenceMsg(octokit, user, branchName) {
const commits = await octokit.request('GET /repos/{owner}/{repo}/compare/{basehead}{?page,per_page}', {
owner: user,
repo: 'songbook',
basehead: `songeditor-main...${branchName}`
})
let msg="";
for (let commit of commits.data.commits) {
msg+=commit.commit.message + "\n"
}
return msg;
}
async function prDescription(octokit, user, branchName) {
let file = await getFileFromBranch(octokit, user, branchName);
let link = editorLink(user, branchName, file, false);
let msg = await getCommitsDifferenceMsg(octokit, user, branchName);
let body = `
[Link do edytora](${link})\n\n${msg}\n\nZa kilka minut pojawi się tu wyrenderowana piosenka (PDF).
Ktoś też zrecenzuje/zaakceptuje Twoje zmiany. Może też mieć tutaj dodatkowe komentarze/pytania.\n
Możesz zawsze wrócić do [edytora](${link}) by nanieść dodatkowe poprawki.`;
return { body, file }
}
// We prepare a form, such that user can do final commit.
async function publishByGet(req, res) {
const {octokit, user} = await newUserOctokit(req, res);
const branchName = req.params.branch;
const {body, file} = prDescription(octokit, user, branchName)
let url = `https://github.com/wdw21/songbook/compare/main...${user}:songbook:${branchName}?title=Piosenka: ${encodeURIComponent(file)}&expand=1&body=${encodeURIComponent(body)}`
res.redirect(url);
}
async function publishByPost(req, res) {
const {octokit, user} = await newUserOctokit(req, res);
const branchName = req.params.branch;
const existingPr = await getPullRequest(octokit, user, branchName);
if (existingPr != null) {
res.redirect(existingPr.html_url);
return
}
const {body, file} = await prDescription(octokit, user, branchName)
const response = await octokit.request('POST /repos/{owner}/{repo}/pulls', {
owner: 'wdw21',
repo: 'songbook',
title: `Piosenka: ${file}`,
body: body,
head: `${user}:${branchName}`,
base: 'main'
})
console.log("Creation of PULL request returned:" + JSON.stringify(response))
res.redirect(response.data.html_url);
}
app.get('/users/:user/changes/:branch[:]publish', async (req, res) => {
await publishByPost(req, res);
});
async function getSongs(octokit, user) {
let songs = await octokit.rest.repos.getContent(
{owner: user, repo: 'songbook', path: 'songs', ref: MAIN_BRANCH_NAME})
//console.log(util.inspect(songs, false, null, false));
let os = [];
for (let s of songs.data) {
os.push({
title: s.name.replaceAll(/.xml$/g, ""),
filename: s.name,
path: s.path,
});
}
return os
}
app.get('/users/:user/changes[:]new', async (req, res) => {
try {
console.log("Starting request /users/:user/changes[:]new");
const {octokit, user} = await newUserOctokit(req, res);
if (!octokit) {
return;
}
console.log("Request query: ", req.query)
console.log("Request query file: ", req.query.file)
if (req.query.file) {
let file = req.query.file;
console.log("Got file: ", file)
if (!file.toLowerCase().endsWith(".xml")) {
file = file + ".xml";
}
return await newChange(file, octokit, user, res)
}
htmlPrefix(res);
res.write(`
<h1>Nowa edycja</h1>\n
Wybierz (lub utwórz) plik, który będziesz edytował:
`);
let songs = await getSongs(octokit, user)
res.write(`<datalist id="files">\n`);
for (const song of songs) {
res.write(` <option>${song.title}</option>\n`);
}
res.write(`</datalist>\n`);
res.write(`
<form action="/users/${user}/changes:new" method="post">
<input name="file" id='file' type="text" list="files" required pattern="[a-zA-Z0-9\\.\\-_()]+"
oninvalid="this.setCustomValidity('Pole musi być wypełnione i zawierać wyłącznie podstawowe litery, cyfry, myślnik i podkreślnik.')"
oninput="this.setCustomValidity('')"/>
<div>
<input type="submit" value="Rozpocznij edycję"/>
</div>
</form>`);
} catch (e) {
HandleError(e, res);
} finally {
htmlSuffix(res);
console.log("!!! /changes:NEW processing finished !!!");
}
});
function sanitizeBranchName(br) {
return br.replaceAll(/[^a-zA-Z0-9.\-_]/g, "_");
}
async function newChange(file, octokit, user, res) {
try {
console.log("newChange started")
let today = new Date().toISOString().slice(0, 10);
let rand = Math.floor(Math.random() * 10000);
let branchName = sanitizeBranchName("se-" + today + "-" + rand + "-" + file);
await prepareBranch(octokit, user, branchName)
res.redirect(editorLink(user, branchName, "songs/" + file, true, true));
} catch (e) {
console.log("newChange EXCEPTION", e)
// Don't know why generic handler is not executed.
myErrorHandle(e, res);
res.end();
throw e;
}
}
app.post('/users/:user/changes[:]new', express.urlencoded({extended: true}), async (req, res) => {
// console.log("BODY", JSON.stringify(req.body));
let file = req.body.file.trim();
if (!file.toLowerCase().endsWith(".xml")) {
file = file + ".xml";
}
console.log("[newChange] getting octokit...")
const {octokit, user} = await newUserOctokit(req, res, `${BASE_URL}users/me/changes:new?file=${encodeURIComponent(file)}`);
if (!octokit) {
console.log("Couldn't get octokit...")
return
}
console.log("[newChange] got octokit. Preparing branch ...")
let nc = await newChange(file, octokit, user, res)
console.log("users/:user/changes[:]new exited");
return nc;
});
app.post('/users/:user/changes/:file[:]new', async (req, res) => {
let file = req.params.trim();
console.log("[newChange] getting octokit...")
const {octokit, user} = await newUserOctokit(req, res, `${BASE_URL}users/me/changes:new?file=${encodeURIComponent(file)}`);
if (!octokit) {
console.log("Couldn't get octokit...")
return
}
console.log("[newChange] got octokit. Preparing branch ...")
return newChange(file, octokit, user, res)
});
app.get('/users/:user/songs', async (req, res) => {
try {
console.log("Starting request /songs");
const {octokit, user} = await newUserOctokit(req, res);
if (!octokit) {
return;
}
htmlPrefix(res);
res.write(`
<h1>Lista piosenek</h1>\n
Wybierz plik, który będziesz edytował:
`);
let songs = await getSongs(octokit, user)
res.write(`<ul>\n`);
for (const song of songs) {
res.write(` <li>${song.title} <form action="/users/${user}/changes:new" method="post"><input type="hidden" name="file" value="${song.filename}"/><input type="submit" value="[ Edytuj ]"/></form></li>\n`);
}
res.write(`</ul>\n`);
res.write(`
<form action="/users/${user}/changes:new" method="post">
<input name="file" id='file' type="text" list="files"/>
<div>
<input type="submit" value="Dodaj nową"/>
</div>
</form>`);
} catch (e) {
HandleError(e, res);
} finally {
htmlSuffix(res);
console.log("!!! /songs processing finished !!!");
}
});
app.get('/config', async (req, res) => {
const {octokit, authuser} = await newUserOctokit(req, res);
try {
await octokit.rest.repos.get({owner: authuser, repo: "songbook"});
} catch (e) {
await octokit.rest.repos.createFork({owner: "wdw21", repo: "songbook"});
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
await delay(3000);
}
await prepareMainBranch(octokit, authuser);
res.redirect(`/users/${authuser}/changes`);
});
function myErrorHandle(e, res) {
if (e instanceof RequestError && e.status===404) {
console.log("Returning 404")
res.status(404);
res.send("Document not found");
} if (e instanceof RequestError && e.status===401) {
console.log("Got 401 -> Redirecting to /auth")
res.redirect("/auth")
} else {
HandleError(e, res);
}
}
async function validateSongXML(payload) {
const xsd = fs.readFileSync(new URL('./song.xsd', import.meta.url)).toString()
const xsdDoc = libxmljs2.parseXml(xsd);
const song = libxmljs2.parseXml(payload);
if (!song.validate(xsdDoc)) {
console.warn("XML not VALID", payload)
return JSON.stringify(song.validationErrors);
} else {
console.log("XML VALID :)")
return null
}
}
async function commit(branchName, file, msg, payload, req, res) {
const {octokit, mygraphql, user} = await newUserOctokit(req, res);
if (!octokit) {
return;
}
console.log('Branch:', branchName);
const branch = await prepareBranch(octokit, user, branchName)
console.log(util.inspect(branch, false, null, false));
const validError = await validateSongXML(payload);
if (validError) {
return {errors: validError};
}
return mygraphql(`
mutation ($input: CreateCommitOnBranchInput!) {
createCommitOnBranch(input: $input) {
commit {
url
}
}
}
`, {
"input": {
"branch": {
"repositoryNameWithOwner": user + "/songbook",
"branchName": branchName
},
"message": {
"headline": msg.trim()
},
"fileChanges": {
"additions": [
{
"path": file,
"contents": Buffer.from(payload).toString("base64")
}
]
},
"expectedHeadOid": branch.data.commit.sha
}
});
}
app.post('/users/:user/changes/:branchName[:]commit', async (req, res) => {
const branchName = req.params.branchName;
const msg = req.query.msg ? req.query.msg : "Kolejne zmiany";
const file = req.query.file
const payload = req.body;
const commitResult = await commit(branchName, file, msg, payload, req, res);
if (commitResult && commitResult.createCommitOnBranch) {
res.send(
{
"status": "committed",
"commit": commitResult.createCommitOnBranch.commit
}
);
console.log(":commit over")
} else {
res.send(
{
"status": "failure",
"errors": commitResult.errors
}
);
}
});
app.post('/users/:user/changes/:branchName/:file([^$]+)', async (req, res) => {
const branchName = req.params.branchName;
const msg = req.query.msg ? req.query.msg.trim() : "Kolejne zmiany";
const file = req.params.file.trim()
const payload = req.body;
const commitResult = await commit(branchName, file, msg, payload, req, res);
res.send(
{
"status": "committed",
"commit": commitResult.createCommitOnBranch.commit
}
);
console.log(":commit over")
});
app.post('/users/:user/changes/:branchName/:file([^$]+)[:]commitAndPublish', async (req, res) => {
const branchName = req.params.branchName;
const msg = req.query.msg ? req.query.msg : "Kolejne zmiany";
const file = req.params.file.trim()
const payload = req.body;
await commit(branchName, file, msg, payload, req, res);
return publish(res, req);
});
app.get('/users/:user/changes/:branchName/:file([^$]+)', async (req, res) => {
try {
const {octokit, user} = await newUserOctokit(req, res);
if (!octokit) return;
const branchName = req.params.branchName;
console.log(branchName);
let cont = await octokit.rest.repos.getContent(
{owner: user, repo: 'songbook', path: req.params.file, ref: branchName});
// res.setHeader('Content-disposition', 'attachment; filename='+req.params.file);
res.setHeader('Content-type', 'text/xml')
res.send(new Buffer.from(cont.data.content, "base64").toString('utf-8'));
} catch (e) {
if (e instanceof RequestError && e.status===404) {
res.status(404);
res.send("Document not found");
} else {
HandleError(e, res);
}
} finally {
res.end();
}
});
app.get('/auth', async (req, res) => {
clearCookiesAndAuthRedirect(res, CHANGES_BASE_URL);
});
app.get('/redirect', async (req, res) => {
let red = req.cookies["redirectUrl"];
console.log(`/redirect with REDIRECT URL: '${red}'`)
const {octokit} = await newUserOctokit(req, res);
if (!octokit) {
return
}
res.clearCookie("redirectUrl");
if (red && red.startsWith(REDIRECT_BASE_URL)) {
res.redirect(CHANGES_BASE_URL);
} else if (red && red.startsWith(BASE_URL)) {
res.redirect(red);
} else {
res.status(400);
}
})
app.get('/intro', async (req, res) => {
htmlPrefix(res);
res.write(`
<h1>O edytowaniu piosenek w śpiewniku</h1>
<p>Zachęcamy wszystkich do poprawiania piosenek w naszym śpiewniku i dopisywania nowych.
Staramy się, by było to jak najwygodniejsze, a rezultaty mogły być drukowane (PDF A4 i A5), czytane jako ebook
(EPUB) lub <a href="${EDITOR_DOMAIN}">przeglądane na stronie</a>.
Gdy twoje propozycje zmian zostaną zatwiedzone (dbamy o jakość), nowa wersja plików (wygenerowana w kilka minut)
będzie zawierała te poprawki.
</p>
<p>Dlatego przygotowaliśmy narzędzie umożliwiające wygodne edytowanie piosenek i dodawanie ich do śpiewnika.
Jedyny wymaganiem jest założenie bezpłatnego konta w serwisie <a href="https://github.com/">github</a>,
bo nasza baza piosenek jest przechowywana <a href="https://github.com/wdw21/songbook/tree/main/songs">właśnie tam</a>.
</p>
<p>Jeśli chcesz spróbować - <b>przejdź do <a href="/auth">edycji</a></b> – zostaniesz poproszony o zalogowanie lub przyznanie uprawnień naszej aplikacji.</p>
<details>
<summary>O bezpieczeństwie</summary>
Możesz odczuwać uzasadniony dyskomfort (w szczególności jeśli masz swoje własne repozytoria githubowe),
by upoważniać jakąś aplikację, by miała do nich dostęp. Mogę Cię tylko zapewnić (ale musisz mi zaufać),
że ta aplikacja tylko:
<ul>
<li>Tworzy roboczą kopię (fork) repozytorium <a href="https://github.com/wdw21/songbook">wdw21/songbook</a>, jeśli go jeszcze nie posiadasz.</li>
<li>Dotyka wyłącznie Twojej roboczej kopi repozytorium 'songbook'</li>
<li>Tworzy i kasuje gałęzie (branches) w repozytorium 'songbook' o nazwie zaczynającej się od 'se-'</li>
<li>Tworzy zapisy (commits) w tych gałęziach, gdy zapisujesz zmiany piosenek.</li>
</ul>
</details>
<details>
<summary>Tryb samodzielny</summary>
<p>Jeżeli chcesz użyć lub zapoznać się z samym edytorem piosenek, to przejdź do: <a href="${EDITOR_BASE_URL}">samodzielnego edytora</a>.
Będziesz mógł/mogła tworzyć nowe piosenki lub otwierać pliki z Twojego dysku lub je zapisywać do plików. Możesz wtedy je przesłać mailem
lub samodzielnie użyć narzędzia 'git'.
</p>
</details>
<p>Problemy/uwagi możesz zgłaszać na: <a href="https://github.com/wdw21/songbook/issues">https://github.com/wdw21/songbook/issues</a></p>
`);
htmlSuffix(res);
});
console.info("Registering songbook", "baseUrl:" + BASE_URL);
http('songbook', app);
console.info("Registered songbook", "baseUrl:" + BASE_URL);