-
Notifications
You must be signed in to change notification settings - Fork 53
/
email.js
77 lines (66 loc) · 2.49 KB
/
email.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
var asciify = require('asciify')
var btoa = require('btoa')
var request = require('request')
var acceptInvites = require('./accept-invites.js')
module.exports = function (object, callback) {
// if it's not an email, return
if (!object.headers) return
getDetails(object)
function getDetails (object) {
var baseURL = 'https://api.github.com/repos/'
var subject = object.headers.Subject
console.log(new Date(), 'Received email:', subject)
if (!subject.match('invited you to')) {
console.log(new Date(), 'non relevant email')
return
}
var detailsArray = subject.split(' invited you to ')
var details = { 'username': detailsArray[0],
'repo': detailsArray[1] }
details.fileURI = baseURL + details.repo + '/contents/contributors/' +
'add-' + details.username + '.txt'
details.forSHA = '?ref=add-' + details.username
console.log(new Date(), details.username, 'invited Reporobot as a collaborator.')
// When any new invite email comes,
// go and accept all invites
acceptInvites(function makeArt (err) {
if (err) return callback(err, 'Invite error')
asciiArt(details)
})
}
function asciiArt (details) {
asciify(details.username, { font: 'isometric2' }, function (err, res) {
if (err) return callback(err, 'Ascii art error')
writeRepo(res, details)
})
}
function writeRepo (artwork, details) {
var reqHeaders = {
'User-Agent': 'request',
'Authorization': 'token ' + process.env['REPOROBOT_TOKEN']
}
var options = {
headers: reqHeaders,
url: details.fileURI + details.forSHA,
json: true,
body: {
'branch': 'add-' + details.username,
'committer': {
'name': 'reporobot',
'email': '[email protected]' },
'sha': '',
'content': btoa(artwork),
'message': 'drew a picture :art:' }}
request.get(options, function getSHA (err, res, body) {
if (err) return callback(err, 'Error fetching SHA')
if (res.statusCode !== 200) return callback('Did not get SHA', body.message)
options.body.sha = body.sha
options.url = details.fileURI
request.put(options, function commitToRepo (err, res, body) {
if (err) return callback(err, 'Error collabing on forked repo.')
if (res.statusCode !== 200) return callback('Didn not collab', body.message)
console.log(new Date(), 'Commited to', details.username, 'repo')
})
})
}
}