-
Notifications
You must be signed in to change notification settings - Fork 0
/
hapiPlugin.js
182 lines (168 loc) · 4.75 KB
/
hapiPlugin.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
172
173
174
175
176
177
178
179
180
181
182
'use strict'
var Path = require('path')
function registerHapiPlugin (server, options, next) {
server.register([require('inert'), require('vision')], function (err) {
if (err) throw err
var getFullTeams = require('./ghTeams').getFullTeams
var exposePublicFields = require('./exposePublicFields')
var hasWebhook = false
var nextAutoUpdate = null
var organization = options.githubOrganization
var token = options.githubApiToken
var github
var lastWebhook = null
var expiresIn = 1000 * 60 * 60 * 24 * 20 // Cache the data for 20 days (note: hapi max: 2147483647)
server.method('githubFullTeam', function (organization, next) {
if (!github) {
github = require('./githubApiForToken')(token)
}
getFullTeams(github, organization, function (err, data) {
nextAutoUpdate = new Date(Date.now() + expiresIn)
next(err, data ? data.map(exposePublicFields) : undefined)
})
}, {
cache: {
generateTimeout: 1000 * 60 * 10, // Wait 10 minutes for github to return all data
expiresIn: expiresIn
}
})
server.views({
engines: {
hbs: require('handlebars')
},
isCached: false,
relativeTo: Path.join(__dirname, '..', 'web', 'views'),
helpersPath: 'helpers'
})
server.route({
method: 'GET',
path: '/static/{param*}',
handler: {
directory: {
path: Path.join(__dirname, '..', 'web', 'static'),
redirectToSlash: true,
index: true
}
}
})
if (!organization) {
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.view('index')
}
})
next()
return
}
if (!token) {
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.view('index', {
org: organization,
noAuthToken: true,
totalMembers: '?'
})
}
})
next()
return
}
if (options.webhookSecret) {
hasWebhook = true
var crypto = require('crypto')
var hashes = crypto.getHashes()
server.route({
method: 'POST',
path: '/webhook',
config: {
payload: {
parse: false
}
},
handler: function (request, reply) {
var sigHeader = request.headers['x-hub-signature']
if (!sigHeader) {
return reply({error: 'missing-signature'})
}
var sigParts = /^([^=]+)\=(.*)$/.exec(sigHeader)
if (!sigParts) {
return reply({error: 'mal-formatted-signature'})
}
var type = sigParts[1]
if (hashes.indexOf(type) === -1) {
return reply({error: 'unknown-signature-hash'})
}
var signature = sigParts[2]
var hmac = crypto.createHmac(type, options.webhookSecret)
hmac.update(request.payload)
var check = hmac.digest('hex')
if (signature !== check) {
return reply({error: 'wrong-signature'})
}
lastWebhook = new Date()
server.methods.githubFullTeam.cache.drop(organization, function (err) {
if (err) {
console.log(err)
return reply({error: 'internal-error'})
}
reply({success: true})
})
}
})
}
server.route({
method: 'GET',
path: '/teams.json',
config: {
cors: {
origin: ['*']
}
},
handler: function (req, reply) {
server.methods.githubFullTeam(organization, function (err, data) {
if (err) {
return reply({error: 'internal-error'})
}
reply(data)
})
}
})
server.route({
method: 'GET',
path: '/',
handler: function (req, reply) {
server.methods.githubFullTeam(organization, function (err, teams) {
var totalMembers = '?'
if (teams) {
totalMembers = Object.keys(teams.reduce(function (all, team) {
if (team.members) {
team.members.forEach(function (member) {
all[member.login] = member
})
}
return all
}, {})).length
}
reply.view('index', {
org: organization,
error: err,
totalMembers: totalMembers,
teams: teams,
hasWebhook: hasWebhook,
nextAutoUpdate: nextAutoUpdate,
lastWebhook: lastWebhook
})
})
}
})
next()
})
}
registerHapiPlugin.attributes = {
pkg: require('../package.json')
}
exports.register = registerHapiPlugin