-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (147 loc) · 4.95 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
const http = require('http')
const fs = require('fs')
const url = require('url')
const async = require('async')
const concat = require('concat-stream')
const checkPR = require('./prcheck.js')
const checkCollab = require('./collabcheck.js')
const checkEmail = require('./email.js')
const mergePR = require('./merge.js')
// q to slow it down enough for the GitHub API
const q = async.queue(function que (pullreq, callback) {
console.log(new Date(), 'QUEUE', pullreq.number)
mergePR(pullreq, function donePR (err, message) {
if (err) return callback(err, message)
callback()
})
}, 1)
console.log('QUEUE LENGTH', q.length())
q.drain = function drain () { console.log('Queue drain') }
module.exports = function () {
const server = http.createServer(router)
// router routes the requests to RR
// to the appropriate places
function router (req, res) {
console.log('(ノ・∀・)ノ')
console.log(new Date(), req.method, req.url)
let queryURL
let username
// End point to latest data
if (req.url === ('/data')) { return sendData(res) }
// When RR gets a push from email when added as collab
// Email from GitHub -> cloudmail.in -> here
if (req.url === '/push') {
return handleEmail(req, res)
}
// When Git-it verifies user added RR as collab
// Comes from verify step in Git-it challenge #8
if (req.url.match('/collab')) {
queryURL = url.parse(req.url, true)
username = queryURL.query.username
return checkCollab(username, function (err, collab) {
collabStatus(res, err, collab)
})
}
// When a PR is made to patchwork repo
// Comes from a GitHub webhook Patchwork repo
if (req.url.match('/orderin')) {
return handlePR(req, res)
}
// When Git-it verifies user made a PR
// Comes from verify step in Git-it challenge #10
if (req.url.match('/pr')) {
queryURL = url.parse(req.url, true)
username = queryURL.query.username
return checkPR(username, function (err, pr) {
// TODO prStatus and collabStatus could be a shared method
prStatus(res, err, pr)
})
}
// When any other request goes to reporobot.jlord.us
res.statusCode = 404
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({
error: 404,
message: 'not_found'
}, true, 2))
}
function handleEmail (req, res) {
req.pipe(concat(function (buff) {
try {
const emailObj = JSON.parse(buff)
checkEmail(emailObj, function checkedEmail (err, message) {
if (err) console.log(new Date(), message, err)
})
} catch (e) {
return console.log(new Date(), 'Error parsing email JSON', req.headers, buff.length, [buff.toString()])
}
}))
// TODO Why is this needed, and otherwise
// cutting off getting the whole request
setTimeout(function () {
res.statusCode = 200
res.end('Thank you.')
}, 1000)
}
function handlePR (req, res) {
req.pipe(concat(function (buff) {
try {
const pullreq = JSON.parse(buff)
// TODO do this check elsewhere, this should just be routing
// Check if it's a closed PR
if (pullreq.action && pullreq.action === 'closed') {
console.log(new Date(), 'SKIPPING: Closed pull request')
} else {
// Send open PR to the queue
q.push(pullreq, function (err, message) {
if (err) return console.log(new Date(), message, err)
console.log(new Date(), pullreq.number, 'Finished PR')
})
}
} catch (e) {
return console.log(new Date(), 'Error parsing PR JSON', req.headers, buff.length, [buff.toString()])
}
res.statusCode = 200
res.setHeader('content-type', 'application/json')
res.end('Thank you.')
}))
}
// Response to Git-it on the existence of user's PR
function prStatus (res, err, pr) {
if (err) {
console.log(err)
res.statusCode = 500
res.end(JSON.stringify({ error: err }))
return
}
console.log(new Date(), 'PR check response:', pr )
res.statusCode = 200
res.end(JSON.stringify({
pr: pr
}, true, 2))
}
// Response to Git-it on RR being added as collab
function collabStatus (res, err, collab) {
if (err) {
console.log(new Date, 'Error getting collab status:', err)
res.statusCode = 500
res.end(JSON.stringify({ error: err }))
return
}
console.log(new Date(), 'Collab check response:', collab )
res.statusCode = 200
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({
collab: collab
}, true, 2))
}
function sendData (res) {
fs.readFile(process.env['CONTRIBUTORS'], function readContrData (err, data) {
if (err) return console.log(new Date(), err)
console.log(new Date(), 'Responding with contributor data')
res.statusCode = 200
res.end(JSON.stringify(JSON.parse(data.toString())))
})
}
return server
}