Skip to content

Commit

Permalink
fix(hook): fix github hook lambda response
Browse files Browse the repository at this point in the history
  • Loading branch information
wyvern8 committed Jul 8, 2018
1 parent 812906a commit f7de470
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/serverless/gtmGithubHook/gtmGithubHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ let githubUtils = require('../gtmGithubUtils.js');

import KmsUtils from './../../KmsUtils';

async function listener(event, context, callback) {
async function listener(event) {
console.log(
`hook call from ${event.requestContext.identity.sourceIp} forwarded for ${event.headers['X-Forwarded-For']}`
);
const githubEvent = event.headers['X-GitHub-Event'] || event.headers['x-github-event'];
const githubSignature = event.headers['X-Hub-Signature'] || event.headers['x-hub-signature'];
let err = await githubUtils.invalidHook(event);
if (err) {
return callback(err, {
return Promise.reject({
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: err.message
body: JSON.stringify(err)
});
}

Expand All @@ -36,10 +36,10 @@ async function listener(event, context, callback) {
// blacklisted repos result in null error and 200 as this is a valid result
err = checkRepoBlacklisted(eventBody);
if (err) {
return callback(null, {
return Promise.reject({
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: err.message
body: JSON.stringify(err)
});
}

Expand All @@ -50,25 +50,23 @@ async function listener(event, context, callback) {
console.log('Payload', json.plain(eventBody));
/* eslint-enable */

let response;
try {
await handleEvent(githubEvent, eventBody, githubSignature);
response = {
let ghEventId = await handleEvent(githubEvent, eventBody, githubSignature);
return Promise.resolve({
statusCode: 200,
headers: { 'X-ghEventId': ghEventId },
body: JSON.stringify({
input: event
})
};
});
} catch (e) {
err = e;
response = {
return Promise.reject({
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: err.message
};
});
}

return callback(err, response);
}

async function handleEvent(type, body, signature) {
Expand Down Expand Up @@ -132,6 +130,7 @@ async function handleEvent(type, body, signature) {
if (type === 'pull_request') {
setPullRequestEventStatus(ghEventId, body);
}
return ghEventId;
}

async function getTaskConfig(type, body) {
Expand Down

0 comments on commit f7de470

Please sign in to comment.