Created
February 1, 2017 20:49
-
-
Save babelop/42e5580b7898719887516649b3053bc7 to your computer and use it in GitHub Desktop.
CodeCommit Webhook for AWS Lambda
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
'use strict'; | |
const url = require('url'); | |
const https = require('https'); | |
exports.handler = (event, context, callback) => { | |
let webhook_url = event.Records[0].customData; | |
if (!webhook_url) { | |
let error = new Error("Web-hook URL not provided as custom data."); | |
callback(error) | |
} else { | |
console.log('POST web-hook to ' + webhook_url) | |
let options = url.parse(webhook_url); | |
options['method'] = 'POST'; | |
const req = https.request(options, (res) => { | |
let body = ''; | |
console.log('Status:', res.statusCode); | |
console.log('Headers:', JSON.stringify(res.headers)); | |
res.setEncoding('utf8'); | |
res.on('data', (chunk) => body += chunk); | |
res.on('end', () => { | |
console.log('Successfully triggered web-hook.'); | |
// If we know it's JSON, parse it. | |
if (res.headers['content-type'] === 'application/json') { | |
body = JSON.parse(body); | |
} | |
callback(null, body); | |
}); | |
}); | |
req.on('error', callback); | |
req.end(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment