Skip to content

Commit

Permalink
Inititalize the handler as an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tcbyrd committed Mar 25, 2018
1 parent f89a58e commit 50f7b3e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
78 changes: 78 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const { createProbot } = require('probot-ts');
const { resolve } = require('probot-ts/lib/resolver')
const { findPrivateKey } = require('probot-ts/lib/private-key')
const { template } = require('./views/probot')

const loadProbot = (plugin) => {
const probot = createProbot({
id: process.env.APP_ID,
secret: process.env.WEBHOOK_SECRET,
cert: findPrivateKey()
})

if (typeof plugin === 'string') {
plugin = resolve(plugin)
}

probot.load(plugin)

return probot
}


module.exports.serverless = (plugin) => {

return (event, context, callback) => {

// 🤖 A friendly homepage if there isn't a payload
if (event.httpMethod === 'GET' && event.path === '/probot') {
const res = {
statusCode: 200,
headers: {
'Content-Type': 'text/html'
},
body: template
}
return callback(null, res)
}

// Otherwise let's listen handle the payload
const probot = loadProbot(plugin)

// Ends function immediately after callback
context.callbackWaitsForEmptyEventLoop = false

// Determine incoming webhook event type
const e = event.headers['x-github-event'] || event.headers['X-GitHub-Event']
const id = event.headers['x-github-delivery'] || event.headers['X-GitHub-Delivery']

// Convert the payload to an Object if API Gateway stringifies it
event.body = (typeof event.body === 'string') ? JSON.parse(event.body) : event.body

// Do the thing
console.log(`Received event ${e}${event.body.action ? ('.' + event.body.action) : ''}`)
if (event) {
try {
probot.receive({
event: e,
payload: event.body
}).then(() => {
const res = {
statusCode: 200,
body: JSON.stringify({
message: 'Executed'
})
}
return callback(null, res)
})
} catch (err) {
console.error(err)
callback(err)
}
} else {
console.error({ event, context })
callback('unknown error')
}
}

}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "@probot/serverless-lambda",
"version": "0.0.1",
"description": "An extension for running Probot in AWS Lambda",
"main": "handler.js",
"main": "index.js",
"scripts": {
"start": "probot run ./index.js",
"test": "jest && standard"
},
"repository": {
Expand Down

0 comments on commit 50f7b3e

Please sign in to comment.