-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
60 lines (56 loc) · 2.45 KB
/
app.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
const debug = require('debug')('app');
const yaml = require('js-yaml');
const config = require('./utils/config');
const urlScanReport = require('./utils/urlscan');
const app = require('./utils/github');
const server = require('./utils/server');
const webhook = require('./utils/webhook');
webhook.on('pull_request', async event => {
if (event.payload.action === 'opened') {
debug("New PR opened! (" + event.payload.repository.owner.login + "/" + event.payload.repository.name + "; #" + event.payload.pull_request.number + ")");
const github = await app.asInstallation(event.payload.installation.id);
debug("Getting original branch...");
const originalBranch = await github.repos.getContent({
owner: event.payload.repository.owner.login,
repo: event.payload.repository.name,
ref: event.payload.pull_request.base.ref,
path: '_data/scams.yaml'
});
debug("Getting PR branch...");
const pullRequestBranch = await github.repos.getContent({
owner: event.payload.repository.owner.login,
repo: event.payload.repository.name,
ref: event.payload.pull_request.head.ref,
path: '_data/scams.yaml'
});
const originalContent = yaml.safeLoad(Buffer.from(originalBranch.data.content,'base64').toString());
const pullRequestContent = yaml.safeLoad(Buffer.from(pullRequestBranch.data.content,'base64').toString());
const oldEntries = originalContent.map(entry => entry.url);
const newEntries = await Promise.all(pullRequestContent.map(entry => entry.url).filter(entry => !oldEntries.includes(entry)).map(url => pullRequestContent.find(entry => entry.url === url)).map(async entry => {
entry.URLScan = (await urlScanReport(entry.url)) || '(Error)';
return entry;
}));
debug("Found " + newEntries.length + " new entries");
debug("Creating comment...");
if(newEntries.length > 0) {
await github.issues.createComment({
owner: event.payload.repository.owner.login,
repo: event.payload.repository.name,
number: event.payload.pull_request.number,
body: '**New entries added**: \n\n' + newEntries.map(entry => Object.keys(entry).map(key => '**' + key + '**: ' + entry[key]).join('\n')).join("\n<hr>\n")
});
} else {
await github.issues.createComment({
owner: event.payload.repository.owner.login,
repo: event.payload.repository.name,
number: event.payload.pull_request.number,
body: '**No new entries added**'
});
}
debug("Done!");
}
});
process.on('unhandledRejection', reason => {
debug(reason);
});
debug("Started!");