-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inititalize the handler as an extension
- Loading branch information
Showing
2 changed files
with
80 additions
and
1 deletion.
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
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') | ||
} | ||
} | ||
|
||
} |
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