forked from oursky/skygear-lambda-check
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (55 loc) · 2.19 KB
/
Copy pathserver.js
File metadata and controls
62 lines (55 loc) · 2.19 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
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const exec = require('child_process').exec;
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
const accessToken = process.env.ACCESS_TOKEN;
// Constants
const PORT = 8080;
// App
const app = express();
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.send('GET request');
});
app.post('/', function (req, res) {
res.send('POST request');
const postRequest = req.body;
let compareUrl = postRequest.repository.compare_url.replace('{base}', postRequest.before);
const cmd = 'curl ' + compareUrl.replace('{head}', postRequest.after) + '?access_token=' + accessToken;
const filesUrls = [];
const downloadUrls = [];
let htmlUrl = '';
exec(cmd, function(error, stdout, stderr) {
// command output is in stdout
const out = JSON.parse(stdout);
htmlUrl = out.html_url;
out.files.map(file => {
const fileName = file.filename;
if (fileName.substr(fileName.length - 3) === '.py' || fileName.substr(fileName.length - 3) === '.js') {
const curlContentCmd = 'curl ' + file.contents_url;
exec(curlContentCmd, function(error, stdout, stderr) {
const contentOut = JSON.parse(stdout);
let curlDownloadCmd = '';
if (file.filename.substr(file.filename.length - 3) === '.py') {
curlDownloadCmd = 'curl '+ contentOut.download_url + '?access_token=' + accessToken + '| grep -e @skygear.op -e @op';
} else {
curlDownloadCmd = 'curl '+ contentOut.download_url + '?access_token=' + accessToken + '| fgrep .op\\(';
}
exec(curlDownloadCmd, function(error, stdout, stderr) {
if (!stdout) {
return;
}
let payload = {text : 'Filename: ' + fileName + '\n Function Name: ' + stdout + '\n URL: ' + htmlUrl};
payload = JSON.stringify(payload);
const postSlackCmd = "curl -X POST --data-urlencode 'payload=" + payload + "' " + webhookUrl;
exec(postSlackCmd, function(error, stdout, stderr) {
});
});
});
}
});
});
});
app.listen(PORT);
console.log('Running on http://localhost:' + PORT);