forked from amykapernick/github_fax_pr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·171 lines (132 loc) · 4.59 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
require('dotenv').config()
const express = require('express'),
app = express(),
bodyParser = require('body-parser'),
fs = require('file-system'),
fetch = require('node-fetch'),
{Octokit} = require('@octokit/rest'),
accountSid = process.env.TWILIO_ACCOUNT_SID,
authToken = process.env.TWILIO_AUTH_TOKEN,
client = require('twilio')(accountSid, authToken),
fileName = 'fax',
ComputerVision = require('@azure/cognitiveservices-computervision'),
ComputerVisionClient = ComputerVision.ComputerVisionClient,
ComputerVisionModels = ComputerVision.ComputerVisionModels,
CognitiveServicesCredentials = require('@azure/ms-rest-azure-js').CognitiveServicesCredentials
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
app.use('/files', express.static('files'))
app.get('/', (req, res) => {
res.send('<h1>Send and review Github PRs via Twilio Fax API</h1>')
})
app.post('/fax', (req, res) => {
client.fax.faxes.create({
from: process.env.SEND_NUMBER,
to: process.env.RECEIVE_NUMBER,
mediaUrl: `https://github-fax.ngrok.io/${req.body.fileName}`
})
.then(fax => console.log(fax.sid))
})
app.post('/sent', (req, res) => {
const twiml = `<Response><Receive action="/receive" /></Response>`
res.type('text/xml')
res.send(twiml)
})
app.post('/receive', (req, res) => {
fetch('https://github-fax.ngrok.io/read', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({url: req.body.MediaUrl})
})
res.status(200)
res.send()
})
app.post('/pr/open', (req, res) => {
console.log(`A PR has been opened, check it out at ${req.body.pull_request.url}`)
let data = {
url: req.body.pull_request.url
}
if(!['opened', 'assigned'].includes(req.body.action)) {
res.sendStatus(200)
return
}
if(req.body.requested_reviewers) {
req.body.requested_reviewers.some(user => {
if(user.login == process.env.GITHUB_USERNAME) {
data.reviewId = user.id
return true
}
return false
})
}
fetch('https://github-fax.ngrok.io/create-pdf', {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' }
})
res.sendStatus(200)
})
app.post('/create-pdf', (req, res) => {
const url = req.body.url,
matches = url.match(/https:\/\/api\.github\.com\/repos\/((\w|\d|-)+)\/((\w|\d|-|_)+)\/pulls\/((\d)+)/i),
username = matches[1],
repo = matches[3],
pr = matches[5]
const PDFDocument = require('pdfkit'),
doc = new PDFDocument
doc.pipe(fs.createWriteStream(`./files/pr_${repo}_${pr}.pdf`))
doc.fontSize(40)
doc.text('A new PR has been opened!')
doc.fontSize(20)
doc.text(`Check out the PR at ${url}`)
doc.end()
fetch('https://github-fax.ngrok.io/fax', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
fileName: `files/pr_${repo}_${pr}.pdf`
})})
res.end()
})
app.post('/pr/comment', (req, res) => {
const {url, username, repo, pr, comment} = req.body
fetch(`https://api.github.com/repos/${username}/${repo}/issues/${pr}/comments`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `token ${process.env.GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify({
body: comment
})
})
res.sendStatus(200)
})
app.post('/read', async (req, res) => {
const cognitiveServiceCredentials = new CognitiveServicesCredentials(process.env.COMPUTER_VISION_KEY),
client = new ComputerVisionClient(cognitiveServiceCredentials, process.env.COMPUTER_VISION_ENDPOINT)
client.batchReadFile(req.body.url).then(result => {
const operationId = result.operationLocation.replace(`${process.env.COMPUTER_VISION_ENDPOINT}/vision/v2.1/read/operations/`, '')
client.getReadOperationResult(operationId).then(data => {
let string = ''
data.recognitionResults[0].lines.forEach(line => {
string = `${string}${line.text}`
})
const matches = string.match(/https:\/\/api\.github\.com\/repos\/((\w|\d|-)+)\/((\w|\d|-|_)+)\/pulls\/((\d)+)/i),
url = matches[0],
username = matches[1],
repo = matches[3],
pr = matches[5],
comment = string.split(url)[1]
fetch('https://github-fax.ngrok.io/pr/comment', {
method: 'POST',
body: JSON.stringify({url, username, repo, pr, comment}),
headers: { 'Content-Type': 'application/json' }
})
}).catch(err => console.log(err))
}).catch(err => console.log(err))
res.sendStatus(200)
})
app.listen(process.env.PORT || 3000, () => {
console.log(`Example app listening on port ${process.env.PORT || 3000}!`)
})