-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hook for deprecating a note version
- Loading branch information
Showing
3 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const _ = require('lodash') | ||
const Boom = require('boom') | ||
|
||
const rollbar = require('./rollbar') | ||
|
||
module.exports = deprecateNodeEvent | ||
module.exports.attributes = { | ||
name: 'deprecateNode' | ||
} | ||
|
||
function deprecateNodeEvent (server, {env, channel}, next) { | ||
server.route({ | ||
method: 'POST', | ||
path: '/deprecate-node', | ||
config: { | ||
pre: [{method: (request, reply) => { | ||
if (request.headers['bearer-token'] === env.BEARER_TOKEN) { | ||
return reply.continue() | ||
} | ||
return reply(Boom.unauthorized()) | ||
}}] | ||
}, | ||
handler | ||
}) | ||
|
||
async function handler (request, reply) { | ||
if (!_.get(request, 'payload.repositoryFullName')) return reply({error: 'repositoryFullName missing'}).code(400) | ||
if (!_.get(request, 'payload.nodeVersion')) return reply({error: 'nodeVersion missing'}).code(400) | ||
if (!_.get(request, 'payload.codeName')) return reply({error: 'codeName missing'}).code(400) | ||
if (!_.get(request, 'payload.newLowestVersion')) return reply({error: 'newLowestVersion missing'}).code(400) | ||
if (!_.get(request, 'payload.newLowestCodeName')) return reply({error: 'newLowestCodeName missing'}).code(400) | ||
// `request.payload.announcementURL` is optional | ||
|
||
const job = { | ||
name: 'deprecate-nodejs-version', | ||
repositoryFullName: request.payload.repositoryFullName, | ||
nodeVersion: request.payload.nodeVersion, | ||
codeName: request.payload.codeName, | ||
newLowestVersion: request.payload.newLowestVersion, | ||
newLowestCodeName: request.payload.newLowestCodeName, | ||
announcementURL: request.payload.announcementURL ? request.payload.announcementURL : undefined | ||
} | ||
|
||
try { | ||
await channel.sendToQueue(env.QUEUE_NAME, Buffer.from(JSON.stringify(job)), {priority: 3}) | ||
} catch (err) { | ||
rollbar.error(err, _.assign({}, request.raw.req, { | ||
socket: { | ||
encrypted: request.server.info.protocol === 'https' | ||
}, | ||
connection: { | ||
remoteAddress: request.info.remoteAddress | ||
} | ||
})) | ||
return reply({error: true}).code(500) | ||
} | ||
|
||
reply({ok: true}).code(202) | ||
} | ||
|
||
next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
const amqp = require('amqplib') | ||
const hapi = require('hapi') | ||
const tap = require('tap') | ||
|
||
const env = require('../lib/env') | ||
const register = require('../lib/deprecate-node-event') | ||
|
||
;(async () => { | ||
const conn = await amqp.connect(env.AMQP_URL) | ||
const channel = await conn.createChannel() | ||
await channel.assertQueue(env.QUEUE_NAME, { | ||
maxPriority: 5 | ||
}) | ||
|
||
let server | ||
tap.beforeEach(done => { | ||
server = new hapi.Server() | ||
server.connection() | ||
done() | ||
}) | ||
|
||
// w/o `.then` everthing blows up ¯\_(ツ)_/¯ | ||
tap.afterEach(() => channel.purgeQueue().then(() => {})) | ||
|
||
tap.tearDown(async () => { | ||
channel.reply = null // not sure why there is an outstanding reply | ||
await conn.close() | ||
}) | ||
|
||
tap.test('rejects without payload', async (t) => { | ||
server.register({ | ||
register, | ||
options: {env} | ||
}) | ||
|
||
const {statusCode} = await server.inject({ | ||
method: 'POST', | ||
url: '/deprecate-node', | ||
headers: { | ||
'Bearer-Token': env.BEARER_TOKEN, | ||
'Content-Type': 'application/json' | ||
}, | ||
payload: JSON.stringify({id: '12'}) | ||
}) | ||
t.is(statusCode, 400, 'statusCode') | ||
t.end() | ||
}) | ||
|
||
tap.test('rejects without authentification', async (t) => { | ||
const payload = JSON.stringify({ | ||
repositoryFullName: 'finnp/abc', | ||
nodeVersion: 4, | ||
codeName: 'Argon', | ||
newLowestVersion: 6, | ||
newLowestCodeName: 'Boron' | ||
}) | ||
server.register({ | ||
register, | ||
options: {env, channel} | ||
}) | ||
|
||
const {statusCode} = await server.inject({ | ||
method: 'POST', | ||
url: '/deprecate-node', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
payload | ||
}) | ||
t.is(statusCode, 401, 'statusCode') | ||
t.end() | ||
}) | ||
|
||
tap.test('stores event in queue', async (t) => { | ||
server.register({ | ||
register, | ||
options: {env, channel} | ||
}) | ||
|
||
const reqPayload = JSON.stringify({ | ||
repositoryFullName: 'finnp/abc', | ||
nodeVersion: 4, | ||
codeName: 'Argon', | ||
newLowestVersion: 6, | ||
newLowestCodeName: 'Boron' | ||
}) | ||
|
||
const {statusCode, payload} = await server.inject({ | ||
method: 'POST', | ||
url: '/deprecate-node', | ||
headers: { | ||
'Bearer-Token': env.BEARER_TOKEN, | ||
'Content-Type': 'application/json' | ||
}, | ||
payload: reqPayload | ||
}) | ||
t.is(statusCode, 202, 'statusCode') | ||
t.true(JSON.parse(payload).ok, 'payload') | ||
const job = await channel.get(env.QUEUE_NAME) | ||
t.same(JSON.parse(job.content.toString()), { | ||
name: 'deprecate-nodejs-version', | ||
repositoryFullName: 'finnp/abc', | ||
nodeVersion: 4, | ||
codeName: 'Argon', | ||
newLowestVersion: 6, | ||
newLowestCodeName: 'Boron' | ||
}) | ||
t.same(job.properties.priority, 3, 'job priority') | ||
t.end() | ||
}) | ||
|
||
tap.test('stores event with announcementURL in queue', async (t) => { | ||
server.register({ | ||
register, | ||
options: {env, channel} | ||
}) | ||
|
||
const reqPayload = JSON.stringify({ | ||
repositoryFullName: 'finnp/abc', | ||
nodeVersion: 4, | ||
codeName: 'Argon', | ||
newLowestVersion: 6, | ||
newLowestCodeName: 'Boron', | ||
announcementURL: 'http://zeppelin.club/zesty' | ||
}) | ||
|
||
const {statusCode, payload} = await server.inject({ | ||
method: 'POST', | ||
url: '/deprecate-node', | ||
headers: { | ||
'Bearer-Token': env.BEARER_TOKEN, | ||
'Content-Type': 'application/json' | ||
}, | ||
payload: reqPayload | ||
}) | ||
t.is(statusCode, 202, 'statusCode') | ||
t.true(JSON.parse(payload).ok, 'payload') | ||
const job = await channel.get(env.QUEUE_NAME) | ||
t.same(JSON.parse(job.content.toString()), { | ||
name: 'deprecate-nodejs-version', | ||
repositoryFullName: 'finnp/abc', | ||
nodeVersion: 4, | ||
codeName: 'Argon', | ||
newLowestVersion: 6, | ||
newLowestCodeName: 'Boron', | ||
announcementURL: 'http://zeppelin.club/zesty' | ||
}) | ||
t.same(job.properties.priority, 3, 'job priority') | ||
t.end() | ||
}) | ||
})() |