Build server prototype (integration with GitHub / NuGet / etc)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
micro-build-server/BuildServer/routes/postreceive.js

90 lines
2.2 KiB

"use strict";
var builder = require('../lib/builder'),
commenter = require('../lib/commenter');
/*
* POST from github
*/
var processPush = function (req, res, payload) {
var repository = payload.repository;
builder.build({
app: req.app,
url: repository.url,
owner: repository.owner.name,
reponame: repository.name,
rev: payload.after,
branch: payload.ref
}, function (err, result) {
console.log("Done processing request from GitHub");
console.log("Error: " + err);
//console.log("Result:");
//console.log(result);
res.send("Done processing request from GitHub\r\n" + "Error: " + err + "\r\n" + "Result: " + result);
});
};
var processPullRequest = function (req, res, payload) {
var action = payload.action,
number = payload.number,
pullRequest = payload.pull_request,
head = pullRequest.head,
headRepo = head.repo,
headRepoOptions = {
url: headRepo.url,
owner: headRepo.owner.name || headRepo.owner.login,
reponame: headRepo.name,
rev: head.sha,
branch: "refs/heads/" + head.ref
},
baseRepo = payload.repository,
baseRepoOptions = {
owner: baseRepo.owner.name || baseRepo.owner.login,
reponame: baseRepo.name
},
options = {
app: req.app,
action: action,
number: number,
headRepoOptions: headRepoOptions,
baseRepoOptions: baseRepoOptions
};
if (action !== "opened" && action !== "reopened" && action !== "synchronize") {
console.log("Got '" + action + "' event:");
//console.log(req.body);
return res.send("Only opened/reopened/synchronize actions are supported");
}
commenter.commentOnPullRequest(options, function (err, data) {
if (err) {
console.log("Unable to post comment: " + err);
}
res.send(err || data);
});
};
module.exports = function (req, res) {
if (!req.body || (!req.body.payload && !req.body.repository)) {
return res.end();
}
var eventType = req.header("x-github-event"),
payload = req.body.payload ? JSON.parse(req.body.payload || "{}") : req.body;
if (eventType === "push") {
return processPush(req, res, payload);
}
if (eventType === "pull_request") {
return processPullRequest(req, res, payload);
}
console.log("Got '" + eventType + "' event:");
//console.log(req.body);
return res.send("Only push/pull_request events are supported");
};