Webhook now accepts pull request events

And attaches comments with build status to pull requests
dependabot/npm_and_yarn/BuildServer/eslint-7.2.0
Inga 🏳‍🌈 10 years ago
parent 5e8655b157
commit e4426c1a70
  1. 99
      BuildServer/routes/postreceive.js
  2. 2
      BuildServer/routes/status.js

@ -1,6 +1,8 @@
"use strict"; "use strict";
var builder = require('../lib/builder'); var fs = require('fs'),
builder = require('../lib/builder'),
settings = require('../settings');
/* /*
* POST from github * POST from github
@ -25,6 +27,95 @@ var processPush = function (req, res, payload) {
}); });
}; };
var getStatusMessage = function (options, callback) {
var releaseDir = options.app.get('releasepath') + "/" + options.owner + "/" + options.reponame + "/" + options.branch + "/" + options.rev,
reportFile = releaseDir + "/report.json";
fs.exists(reportFile, function (exists) {
if (!exists) {
fs.exists(releaseDir, function (dirExists) {
if (!dirExists) {
return callback("Release directory not found");
}
if (options.lastAttempt) {
return callback("Report file not found");
}
//maybe it is building right now
options.lastAttempt = true;
setTimeout(function () {
getStatusMessage(options, callback);
}, 10000);
});
}
fs.readFile(reportFile, function (err, dataBuffer) {
if (err) {
return callback(err);
}
var data = dataBuffer.toString();
if (!data) {
return callback("Report file not found");
}
var report = JSON.parse(data);
if (report.result === "MBSNotFound") {
return callback("mbs.json is not found");
}
if (report.err) {
return callback("ERR: " + report.err);
}
if ((report.result.warns.$allMessages || []).length > 0) {
return callback("WARN: " + report.result.warns.$allMessages[0].message);
}
if ((report.result.infos.$allMessages || []).length > 0) {
return callback(undefined, report.result.infos.$allMessages[report.result.infos.$allMessages.length-1].message);
}
return callback(undefined, "OK");
});
});
};
var processPullRequest = function (req, res, payload) {
var action = payload.action,
number = payload.number,
pullRequest = payload.pull_request,
head = pullRequest.head,
headRepo = head.repo,
options = {
app: req.app,
url: headRepo.url,
owner: headRepo.owner.name || headRepo.owner.login,
reponame: headRepo.name,
rev: head.sha,
branch: "refs/heads/" + head.ref
},
baseRepo = payload.repository,
baseOwner = baseRepo.owner.name || baseRepo.owner.login,
baseReponame = baseRepo.name;
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");
}
getStatusMessage(options, function (err, successMessage) {
var message = err ? ("Was not built:\r\n\r\n" + err + "\r\n\r\nDO NOT MERGE!") : ("Build OK\r\n\r\n" + successMessage);
settings.createGithub(baseOwner).issues.createComment({
user: baseOwner,
repo: baseReponame,
number: number,
body: message
}, function (err) {
if (err) {
console.log("Unable to post comment: " + err);
}
res.send(err || "OK");
});
});
};
module.exports = function (req, res) { module.exports = function (req, res) {
if (!req.body || (!req.body.payload && !req.body.repository)) { if (!req.body || (!req.body.payload && !req.body.repository)) {
return res.end(); return res.end();
@ -37,7 +128,11 @@ module.exports = function (req, res) {
return processPush(req, res, payload); return processPush(req, res, payload);
} }
if (eventType === "pull_request") {
return processPullRequest(req, res, payload);
}
console.log("Got '" + eventType + "' event:"); console.log("Got '" + eventType + "' event:");
//console.log(req.body); //console.log(req.body);
return res.send("Only push events are supported"); return res.send("Only push/pull_request events are supported");
}; };

@ -64,7 +64,7 @@ var loadReport = function (app, options, callback) {
return callback(err, options); return callback(err, options);
} }
var reportFile = reportFile = releaseDir + "/report.json"; var reportFile = releaseDir + "/report.json";
options.files = files; options.files = files;
fs.exists(reportFile, function (exists) { fs.exists(reportFile, function (exists) {
if (!exists) { if (!exists) {

Loading…
Cancel
Save