Last active
July 27, 2017 08:24
-
-
Save kalinchernev/d1e79a7f883a4d37f50519d9b05df0c5 to your computer and use it in GitHub Desktop.
Example node.js lambda function to update pull request title and tags after greenkeeper bot. Written and formatted badly - mix of ES5 and ES6, etc. it was struggling finding issues reported by the platform ...
This file contains hidden or 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"; | |
var crypto = require("crypto"); | |
var GitHubApi = require("github"); | |
var github = new GitHubApi(); | |
var request = require("request"); | |
function signRequestBody(key, body) { | |
return ( | |
"sha1=" + crypto.createHmac("sha1", key).update(body, "utf-8").digest("hex") | |
); | |
} | |
module.exports.githubWebhookListener = function(event, context, callback) { | |
var errMsg; // eslint-disable-line | |
var token = process.env.GITHUB_WEBHOOK_SECRET; | |
var headers = event.headers; | |
var sig = headers["X-Hub-Signature"]; | |
var githubEvent = headers["X-GitHub-Event"]; | |
var id = headers["X-GitHub-Delivery"]; | |
var payload = event.body; | |
var calculatedSig = signRequestBody(token, JSON.stringify(payload)); | |
if (typeof token !== "string") { | |
errMsg = "[401] must provide a 'GITHUB_WEBHOOK_SECRET' env variable"; | |
return callback(new Error(errMsg)); | |
} | |
if (!sig) { | |
errMsg = "[401] No X-Hub-Signature found on request"; | |
return callback(new Error(errMsg)); | |
} | |
if (!githubEvent) { | |
errMsg = "[422] No X-Github-Event found on request"; | |
return callback(new Error(errMsg)); | |
} | |
if (!id) { | |
errMsg = "[401] No X-Github-Delivery found on request"; | |
return callback(new Error(errMsg)); | |
} | |
if (sig !== calculatedSig) { | |
errMsg = | |
"[401] X-Hub-Signature incorrect. Github webhook token doesn't match"; | |
return callback(new Error(errMsg)); | |
} | |
/* eslint-disable */ | |
console.log("---------------------------------"); | |
console.log( | |
'Github-Event: "' + githubEvent + '" with action: "' + payload.action + '"' | |
); | |
console.log("---------------------------------"); | |
console.log("Payload", payload); | |
/* eslint-enable */ | |
if ( | |
(payload.action === "opened" || payload.action === "reopened") && | |
payload.pull_request.user.login === "greenkeeper[bot]" | |
) { | |
// We split username/repo information and make assumptions later. | |
var repo = payload.pull_request.head.repo.full_name.split("/"); | |
var repoOwner = repo[0]; | |
var repoName = repo[1]; | |
var options = { | |
// Github token | |
token: process.env.GITHUB_TOKEN, | |
// Repo owner | |
owner: repoOwner, | |
// Repo name | |
repo: repoName, | |
// Labels to add | |
labels: ["tag: internal", "automated"] | |
}; | |
github.authenticate({ | |
type: "token", | |
token: options.token | |
}); | |
// Add label | |
github.issues.addLabels({ | |
owner: options.owner, | |
repo: options.repo, | |
number: payload.pull_request.number, | |
labels: options.labels | |
}); | |
request( | |
{ | |
url: payload.pull_request.commits_url, | |
headers: { | |
"User-Agent": "request" | |
} | |
}, | |
function(error, response, body) { | |
const d = JSON.parse(body); | |
github.pullRequests.update({ | |
owner: options.owner, | |
repo: options.repo, | |
number: payload.pull_request.number, | |
// 'chore(package): update chai to version 4.1.0' | |
title: d[0].commit.message + " - noissue" | |
}); | |
} | |
); | |
} | |
var response = { | |
statusCode: 200, | |
body: JSON.stringify({ | |
input: event | |
}) | |
}; | |
return callback(null, response); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment