-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-task.js
More file actions
80 lines (63 loc) · 2.26 KB
/
github-task.js
File metadata and controls
80 lines (63 loc) · 2.26 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
const github = require('./github');
const { URL } = require('url');
const separator = '<span id="turbo-content-start"></span>';
const line = '\n****\n';
function getURLs(str, pull) {
const re = /https:\/\/yandex\.ru\/turbo\?text=[^\s|)]+/g;
const match = str.match(re);
if (!Array.isArray(match)) {
return [];
}
const examples = [
'https://yandex.ru/turbo?text=https://rozhdestvenskiy.ru/',
'https://yandex.ru/turbo?text=[URL]'
];
return match.filter(url => !examples.includes(url)).map(url => {
try {
const before = new URL(url);
before.hostname = 'master.turboext.net';
const after = new URL(url);
after.hostname = `pull-${pull}.turboext.net`;
const text = before.searchParams.get('text');
return {
text,
before: before.toString(),
beforeFrame: before.toString().replace('/turbo', '/frame'),
after: after.toString(),
afterFrame: after.toString().replace('/turbo', '/frame')
};
} catch(e) {
return false;
}
}).filter(Boolean);
}
module.exports = async function githubTask(payload) {
try {
const number = payload.pull_request.number;
const search = {
owner: 'turboext',
repo: 'css',
number
};
const { data } = await github.pullRequests.get(search);
const original = data.body ? data.body.split(separator)[0] : '';
const beta = `🚀 [master](https://master.turboext.net)\n🚀 [pull request](https://pull-${number}.turboext.net)`;
const beautify = url => {
return [
url.text,
`🚀 [master](${url.before}) [master iframe](${url.beforeFrame})`,
`🚀 [PR](${url.after}) [PR iframe](${url.afterFrame})\n`
].join('\n');
};
const urls = getURLs(original, number);
const body = [
original.trim(),
separator,
line,
urls.length ? urls.map(beautify).join('\n') : beta
].join('\n');
return await github.pullRequests.update({...search, body});
} catch (e) {
console.error(e);
}
};