-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwebhook.js
112 lines (94 loc) · 3.1 KB
/
webhook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
var AWS = require('aws-sdk');
var codebuild = new AWS.CodeBuild();
var GitHubApi = require("github");
var github = new GitHubApi();
// setup github client
github.authenticate({
type: "basic",
username: process.env.GITHUB_USERNAME,
password: process.env.GITHUB_ACCESS_TOKEN
});
// get the region where this lambda is running
var region = process.env.AWS_DEFAULT_REGION;
var repo = process.env.GITHUB_REPOSITORY.split('/');
module.exports.resource = (event, context, callback) => {
console.log("REQUEST RECEIVED:\n" + JSON.stringify(event));
// For Delete requests, immediately send a SUCCESS response.
if (event.RequestType == "Delete") {
// TODO remove webhook from repo
sendResponse(event, context, "SUCCESS");
return;
} else {
var data = {
owner: repo[3],
repo: repo[4],
name: 'web',
events: ['pull_request'],
active: true,
config: {
url: event.ResourceProperties.Endpoint,
content_type:"json"
}
};
if(event.RequestType == "Create") {
github.repos.createHook(data).then(function(data){
sendResponse(event, context, "SUCCESS", {});
}).catch(function(err){
console.log(err);
sendResponse(event, context, "FAILED", {});
});
} else {
github.repos.editHook(data).then(function(data){
sendResponse(event, context, "SUCCESS", {});
}).catch(function(err){
console.log(err);
sendResponse(event, context, "FAILED", {});
});;
}
}
// var responseStatus = "FAILED";
// var responseData = {};
// sendResponse(event, context, responseStatus, responseData);
}
// Send response to the pre-signed S3 URL
function sendResponse(event, context, responseStatus, responseData) {
var responseBody = JSON.stringify({
Status: responseStatus,
Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
PhysicalResourceId: context.logStreamName,
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
Data: responseData
});
console.log("RESPONSE BODY:\n", responseBody);
var https = require("https");
var url = require("url");
var parsedUrl = url.parse(event.ResponseURL);
var options = {
hostname: parsedUrl.hostname,
port: 443,
path: parsedUrl.path,
method: "PUT",
headers: {
"content-type": "",
"content-length": responseBody.length
}
};
console.log("SENDING RESPONSE...\n");
var request = https.request(options, function(response) {
console.log("STATUS: " + response.statusCode);
console.log("HEADERS: " + JSON.stringify(response.headers));
// Tell AWS Lambda that the function execution is done
context.done();
});
request.on("error", function(error) {
console.log("sendResponse Error:" + error);
// Tell AWS Lambda that the function execution is done
context.done();
});
// write data to request body
request.write(responseBody);
request.end();
}