From acce6e7578fa7d092b2b768de213a95ba57da222 Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Sat, 14 Jul 2018 15:32:58 -0500 Subject: [PATCH 1/3] Better way of detecting pull_request --- aws/lambda/index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/aws/lambda/index.js b/aws/lambda/index.js index 52a573f..9c30893 100644 --- a/aws/lambda/index.js +++ b/aws/lambda/index.js @@ -30,14 +30,22 @@ exports.handler = async function(event, context) { return; } - var pull_request; - if (json["action"] === "requested" || json["action"] === "rerequested") { - pull_request = json["check_suite"]["pull_requests"][0]; + let pull_requests; + if ("pull_requests" in json) { + pull_requests = json["check_suite"]["pull_requests"]; + } + else if ("pull_request" in json) { + pull_requests = [json["pull_request"]]; } else { - pull_request = json["pull_request"]; + response.statusCode = 500; + response.body = "No pull requests present"; + context.succeed(response); + return; } + const pull_request = pull_requests[0]; + var title = "New PR #" + pull_request["number"] + "\nwith base " + pull_request["base"]["ref"] + "\nand head " + pull_request["head"]["sha"]; From 7a4e62aeaf51ee28fa1d8574490f25717314f201 Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Sat, 14 Jul 2018 15:54:53 -0500 Subject: [PATCH 2/3] Refactor enqueuing into separate function --- aws/lambda/index.js | 135 ++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 62 deletions(-) diff --git a/aws/lambda/index.js b/aws/lambda/index.js index 9c30893..62af4cd 100644 --- a/aws/lambda/index.js +++ b/aws/lambda/index.js @@ -8,6 +8,75 @@ AWS.config.apiVersions = { var ec2 = new AWS.EC2(); var sqs = new AWS.SQS(); +const PARAMS = { + InstanceIds: [ + process.env["INSTANCE_ID"], + ] +}; +const QUEUE_URL = process.env["QUEUE_URL"]; + +const enqueuePullRequest = function(pr, installation) { + const title = "New PR #" + pr["number"] + + "\nwith base " + pr["base"]["ref"] + + "\nand head " + pr["head"]["sha"]; + + const cloneUrl = pr["base"]["repo"]["url"].replace("https://api.github.com/repos", "https://github.com"); + + const message = { + MessageBody: title, + QueueUrl: QUEUE_URL, + MessageGroupId: "0", + MessageAttributes: { + "pr": { + DataType: "String", + StringValue: String(pr["number"]) + }, + "base": { + DataType: "String", + StringValue: pr["base"]["ref"] + }, + "base-sha": { + DataType: "String", + StringValue: pr["base"]["sha"] + }, + "head": { + DataType: "String", + StringValue: pr["head"]["sha"] + }, + "head-branch": { + DataType: "String", + StringValue: pr["head"]["ref"] + }, + "repo": { + DataType: "String", + StringValue: cloneUrl + }, + "installation": { + DataType: "String", + StringValue: installation + }, + "url": { + DataType: "String", + StringValue: pr["base"]["repo"]["url"] + }, + } + }; + + await new Promise((resolve, reject) => { + sqs.sendMessage(message, function(err, data) { + if (err) { + console.log("Error", err); + } + else { + console.log("SQS Success"); + } + resolve(); + }); + }); + + return message; +} + exports.handler = async function(event, context) { context.callbackWaitsForEmptyEventLoop = false; @@ -30,6 +99,8 @@ exports.handler = async function(event, context) { return; } + const INSTALLATION = String(json["installation"]["id"]); + let pull_requests; if ("pull_requests" in json) { pull_requests = json["check_suite"]["pull_requests"]; @@ -46,71 +117,11 @@ exports.handler = async function(event, context) { const pull_request = pull_requests[0]; - var title = "New PR #" + pull_request["number"] + - "\nwith base " + pull_request["base"]["ref"] + - "\nand head " + pull_request["head"]["sha"]; - - var params = { - InstanceIds: [ - process.env["INSTANCE_ID"], - ] - }; - - var message = { - MessageBody: title, - QueueUrl: process.env["QUEUE_URL"], - MessageGroupId: "0", - MessageAttributes: { - "pr": { - DataType: "String", - StringValue: String(pull_request["number"]) - }, - "base": { - DataType: "String", - StringValue: pull_request["base"]["ref"] - }, - "base-sha": { - DataType: "String", - StringValue: pull_request["base"]["sha"] - }, - "head": { - DataType: "String", - StringValue: pull_request["head"]["sha"] - }, - "head-branch": { - DataType: "String", - StringValue: pull_request["head"]["ref"] - }, - "repo": { - DataType: "String", - StringValue: json["repository"]["clone_url"] - }, - "installation": { - DataType: "String", - StringValue: String(json["installation"]["id"]) - }, - "url": { - DataType: "String", - StringValue: pull_request["base"]["repo"]["url"] - }, - } - }; + const message = enqueuePullRequest(pull_request, INSTALLATION); response.body = JSON.stringify(message, null, 2); await new Promise((resolve, reject) => { - sqs.sendMessage(message, function(err, data) { - if (err) { - console.log("Error", err); - } - else { - console.log("SQS Success"); - } - resolve(); - }); - }); - - await new Promise((resolve, reject) => { - ec2.startInstances(params, function(err, data) { + ec2.startInstances(PARAMS, function(err, data) { if (err) { console.log("Error", err); } From d1d4f8751acb1cd81f14f233ac8dbf76120ef727 Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Sat, 14 Jul 2018 15:57:29 -0500 Subject: [PATCH 3/3] Handle multiple PRs --- aws/lambda/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/lambda/index.js b/aws/lambda/index.js index 62af4cd..23a8f0f 100644 --- a/aws/lambda/index.js +++ b/aws/lambda/index.js @@ -115,10 +115,10 @@ exports.handler = async function(event, context) { return; } - const pull_request = pull_requests[0]; - - const message = enqueuePullRequest(pull_request, INSTALLATION); - response.body = JSON.stringify(message, null, 2); + for (pull_request in pull_requests) { + const message = enqueuePullRequest(pull_request, INSTALLATION); + response.body += JSON.stringify(message, null, 2) + "\n\n"; + } await new Promise((resolve, reject) => { ec2.startInstances(PARAMS, function(err, data) {