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.
113 lines
3.3 KiB
113 lines
3.3 KiB
10 years ago
|
"use strict";
|
||
|
|
||
8 years ago
|
import builder = require("../lib/builder");
|
||
|
import commenter = require("../lib/commenter");
|
||
8 years ago
|
|
||
|
const getBranchDescription = (options) => `${options.owner}/${options.reponame}:${options.branchname || options.branch}`;
|
||
8 years ago
|
|
||
8 years ago
|
const processPush = (req, res, payload) => {
|
||
8 years ago
|
const repository = payload.repository;
|
||
|
const options = {
|
||
8 years ago
|
"app": req.app,
|
||
|
"branch": payload.ref,
|
||
|
"owner": repository.owner.name,
|
||
|
"reponame": repository.name,
|
||
|
"rev": payload.after,
|
||
|
"url": repository.url
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
console.log(`Got push event for ${getBranchDescription(options)}`);
|
||
8 years ago
|
|
||
|
builder.build(options, (err, result) => {
|
||
|
console.log("Done processing request from GitHub");
|
||
8 years ago
|
console.log(`Error: ${err}`);
|
||
|
res.send(`Done processing request from GitHub\r\nError: ${err}\r\nResult: ${result}`);
|
||
8 years ago
|
});
|
||
10 years ago
|
};
|
||
|
|
||
8 years ago
|
const processPullRequest = (req, res, payload) => {
|
||
8 years ago
|
const action = payload.action;
|
||
|
const number = payload.number;
|
||
|
const pullRequest = payload.pull_request;
|
||
|
const head = pullRequest.head;
|
||
|
const headRepo = head.repo;
|
||
|
const headRepoOptions = {
|
||
8 years ago
|
"branch": `refs/heads/${head.ref}`,
|
||
|
"branchname": head.ref,
|
||
|
"owner": headRepo.owner.name || headRepo.owner.login,
|
||
|
"reponame": headRepo.name,
|
||
|
"rev": head.sha,
|
||
|
"url": headRepo.url
|
||
8 years ago
|
};
|
||
|
const base = pullRequest.base;
|
||
|
const baseRepo = base.repo;
|
||
|
const baseRepoOptions = {
|
||
8 years ago
|
"branchname": base.ref,
|
||
|
"owner": baseRepo.owner.name || baseRepo.owner.login,
|
||
|
"reponame": baseRepo.name
|
||
8 years ago
|
};
|
||
|
const options = {
|
||
8 years ago
|
action,
|
||
|
"app": req.app,
|
||
|
baseRepoOptions,
|
||
|
headRepoOptions,
|
||
|
number
|
||
8 years ago
|
};
|
||
|
const masterOptions = {
|
||
8 years ago
|
action,
|
||
|
"app": req.app,
|
||
|
baseRepoOptions,
|
||
|
"headRepoOptions": baseRepoOptions,
|
||
|
number
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
console.log(`Got pull request ${action} event, `
|
||
|
+ `from ${getBranchDescription(headRepoOptions)} (${headRepoOptions.rev}) to ${getBranchDescription(baseRepoOptions)}`);
|
||
8 years ago
|
|
||
|
if (action !== "opened" && action !== "reopened" && action !== "synchronize" && action !== "closed") {
|
||
|
return res.send("Only opened/reopened/synchronize/closed actions are supported");
|
||
|
}
|
||
|
|
||
|
if (action === "closed" && !pullRequest.merged) {
|
||
|
console.log("Pull request closed without merging");
|
||
8 years ago
|
|
||
8 years ago
|
return res.send("Pull request closed without merging");
|
||
|
}
|
||
|
|
||
|
if (action === "closed") {
|
||
|
return res.send("");
|
||
|
}
|
||
|
|
||
8 years ago
|
return commenter.commentOnPullRequest(
|
||
|
(action === "closed" && masterOptions) || options,
|
||
8 years ago
|
(err, data) => {
|
||
|
if (err) {
|
||
8 years ago
|
console.log(`Unable to post comment: ${err}`);
|
||
8 years ago
|
}
|
||
|
|
||
|
res.send(err || data);
|
||
|
}
|
||
|
);
|
||
10 years ago
|
};
|
||
|
|
||
8 years ago
|
export = (req, res) => {
|
||
8 years ago
|
if (!req.body || (!req.body.payload && !req.body.repository)) {
|
||
|
return res.end();
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
const eventType = req.header("x-github-event");
|
||
8 years ago
|
const payload = (req.body.payload && JSON.parse(req.body.payload)) || req.body;
|
||
10 years ago
|
|
||
8 years ago
|
if (eventType === "push") {
|
||
|
return processPush(req, res, payload);
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if (eventType === "pull_request") {
|
||
|
return processPullRequest(req, res, payload);
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
console.log(`Got "${eventType}" event:`);
|
||
|
|
||
8 years ago
|
return res.send("Only push/pull_request events are supported");
|
||
10 years ago
|
};
|