Skip to content

Commit 0dff510

Browse files
authored
Merge pull request #56 from greenkeeperio/feat/sync
feat: add sync hook
2 parents 929d3af + 80ae4e6 commit 0dff510

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

index.js

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ require('./lib/rollbar')
5757
env,
5858
channel
5959
}
60+
}, {
61+
register: require('./lib/sync-event'),
62+
options: {
63+
env,
64+
channel
65+
}
6066
}, {
6167
register: require('./lib/update-node-event'),
6268
options: {

lib/sync-event.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const _ = require('lodash')
2+
const Boom = require('boom')
3+
4+
const rollbar = require('./rollbar')
5+
6+
module.exports = resetEvent
7+
module.exports.attributes = {
8+
name: 'sync'
9+
}
10+
11+
function resetEvent (server, {env, channel}, next) {
12+
server.route({
13+
method: 'POST',
14+
path: '/sync',
15+
config: {
16+
pre: [{method: (request, reply) => {
17+
if (request.headers['bearer-token'] === env.BEARER_TOKEN) {
18+
return reply.continue()
19+
}
20+
return reply(Boom.unauthorized())
21+
}}]
22+
},
23+
handler
24+
})
25+
26+
async function handler (request, reply) {
27+
if (!_.get(request, 'payload.accountId')) return reply({error: 'accountId missing'}).code(400)
28+
29+
const job = {
30+
name: 'sync-repos',
31+
accountId: request.payload.accountId
32+
}
33+
34+
try {
35+
await channel.sendToQueue(env.QUEUE_NAME, Buffer.from(JSON.stringify(job)), {priority: 5})
36+
} catch (err) {
37+
rollbar.error(err, _.assign({}, request.raw.req, {
38+
socket: {
39+
encrypted: request.server.info.protocol === 'https'
40+
},
41+
connection: {
42+
remoteAddress: request.info.remoteAddress
43+
}
44+
}))
45+
return reply({error: true}).code(500)
46+
}
47+
48+
reply({ok: true}).code(202)
49+
}
50+
51+
next()
52+
}

0 commit comments

Comments
 (0)