2017-01-07 07:22:48 +00:00
|
|
|
const ghGot = require('gh-got');
|
|
|
|
|
2017-01-11 12:19:59 +00:00
|
|
|
const config = {};
|
2017-01-11 09:05:42 +00:00
|
|
|
let logger = null;
|
2017-01-07 07:22:48 +00:00
|
|
|
|
2017-01-11 13:33:32 +00:00
|
|
|
module.exports = {
|
2017-01-12 16:04:25 +00:00
|
|
|
init,
|
2017-01-11 13:33:32 +00:00
|
|
|
initRepo,
|
2017-01-10 12:32:32 +00:00
|
|
|
// Package File
|
2017-01-11 13:33:32 +00:00
|
|
|
getPackageFile,
|
|
|
|
getPackageFileContents,
|
|
|
|
writePackageFile,
|
2017-01-10 12:32:32 +00:00
|
|
|
// Branch
|
2017-01-11 13:33:32 +00:00
|
|
|
createBranch,
|
2017-01-10 12:32:32 +00:00
|
|
|
// PR
|
2017-01-11 13:33:32 +00:00
|
|
|
checkForClosedPr,
|
|
|
|
createPr,
|
|
|
|
getPr,
|
|
|
|
updatePr,
|
2017-01-10 12:32:32 +00:00
|
|
|
};
|
|
|
|
|
2017-01-12 16:04:25 +00:00
|
|
|
function init(token, l) {
|
|
|
|
config.token = token;
|
2017-01-11 13:33:32 +00:00
|
|
|
logger = l;
|
|
|
|
}
|
|
|
|
|
2017-01-10 12:32:32 +00:00
|
|
|
// Initialize GitHub by getting base branch and SHA
|
2017-01-12 16:04:25 +00:00
|
|
|
function initRepo(repoName) {
|
2017-01-10 12:32:32 +00:00
|
|
|
config.repoName = repoName;
|
2017-01-11 12:19:59 +00:00
|
|
|
|
|
|
|
return getRepo()
|
|
|
|
.then(processRepo)
|
|
|
|
.catch((err) => {
|
|
|
|
logger.error(`GitHub init error: ${err}`);
|
|
|
|
throw err;
|
|
|
|
});
|
2017-01-10 12:32:32 +00:00
|
|
|
|
2017-01-10 21:28:22 +00:00
|
|
|
function getRepo() {
|
2017-01-11 12:19:59 +00:00
|
|
|
logger.debug(`Getting repo ${repoName}`);
|
2017-01-11 06:14:14 +00:00
|
|
|
return ghGot(`repos/${config.repoName}`, { token: config.token })
|
|
|
|
.then(res => res.body);
|
2017-01-10 21:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function processRepo(repo) {
|
2017-01-11 12:19:59 +00:00
|
|
|
logger.debug(`Processing repo ${repoName}`);
|
2017-01-10 21:28:22 +00:00
|
|
|
config.owner = repo.owner.login;
|
|
|
|
config.defaultBranch = repo.default_branch;
|
2017-01-10 22:06:25 +00:00
|
|
|
return ghGot(`repos/${config.repoName}/git/refs/head`, {
|
|
|
|
token: config.token,
|
|
|
|
}).then((res) => {
|
2017-01-10 12:32:32 +00:00
|
|
|
// Get the SHA for base branch
|
2017-01-10 21:28:22 +00:00
|
|
|
res.body.forEach((branch) => {
|
2017-01-07 07:22:48 +00:00
|
|
|
// Loop through all branches because the base branch may not be the first
|
2017-01-10 12:32:32 +00:00
|
|
|
if (branch.ref === `refs/heads/${config.defaultBranch}`) {
|
2017-01-07 07:22:48 +00:00
|
|
|
// This is the SHA we will create new branches from
|
|
|
|
config.baseSHA = branch.object.sha;
|
|
|
|
}
|
|
|
|
});
|
2017-01-10 21:28:22 +00:00
|
|
|
return Promise.resolve();
|
2017-01-07 20:19:15 +00:00
|
|
|
});
|
2017-01-10 21:28:22 +00:00
|
|
|
}
|
2017-01-10 12:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Package File
|
|
|
|
function getPackageFile(branchName) {
|
|
|
|
return getFile(config.packageFile, branchName);
|
|
|
|
}
|
|
|
|
|
2017-01-11 12:19:59 +00:00
|
|
|
function getPackageFileContents(packageFile) {
|
2017-01-12 16:04:25 +00:00
|
|
|
logger.debug(`Retrieving ${config.repoName} ${packageFile}`);
|
2017-01-11 12:19:59 +00:00
|
|
|
config.packageFile = packageFile;
|
2017-01-10 12:32:32 +00:00
|
|
|
return getFileContents(config.packageFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writePackageFile(branchName, oldFileSHA, fileContents, message) {
|
2017-01-10 22:06:25 +00:00
|
|
|
return writeFile(
|
|
|
|
branchName,
|
|
|
|
oldFileSHA,
|
|
|
|
config.packageFile,
|
|
|
|
fileContents,
|
2017-01-11 06:14:14 +00:00
|
|
|
message);
|
2017-01-10 12:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Branch
|
|
|
|
function createBranch(branchName) {
|
|
|
|
return ghGot.post(`repos/${config.repoName}/git/refs`, {
|
|
|
|
token: config.token,
|
|
|
|
body: {
|
|
|
|
ref: `refs/heads/${branchName}`,
|
|
|
|
sha: config.baseSHA,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull Request
|
|
|
|
function checkForClosedPr(branchName, prTitle) {
|
2017-01-10 22:06:25 +00:00
|
|
|
return ghGot(
|
|
|
|
`repos/${config.repoName}/pulls?state=closed&head=${config.owner}:${branchName}`,
|
2017-01-11 06:54:27 +00:00
|
|
|
{ token: config.token })
|
|
|
|
.then(res =>
|
|
|
|
res.body.some(pr => pr.title === prTitle && pr.head.label === `${config.owner}:${branchName}`))
|
|
|
|
.catch((error) => {
|
2017-01-11 09:05:42 +00:00
|
|
|
logger.error(`Error checking if PR already existed: ${error}`);
|
2017-01-10 22:06:25 +00:00
|
|
|
});
|
2017-01-10 12:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createPr(branchName, title, body) {
|
2017-01-10 22:06:25 +00:00
|
|
|
return ghGot
|
|
|
|
.post(`repos/${config.repoName}/pulls`, {
|
|
|
|
token: config.token,
|
|
|
|
body: { title, head: branchName, base: config.defaultBranch, body },
|
|
|
|
})
|
|
|
|
.then(res => res.body);
|
2017-01-10 12:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPr(branchName) {
|
2017-01-10 22:06:25 +00:00
|
|
|
const gotString = `repos/${config.repoName}/pulls?` +
|
2017-01-10 21:28:22 +00:00
|
|
|
`state=open&base=${config.defaultBranch}&head=${config.owner}:${branchName}`;
|
2017-01-10 22:06:25 +00:00
|
|
|
return ghGot(gotString, { token: config.token }).then((res) => {
|
2017-01-10 12:32:32 +00:00
|
|
|
if (res.body.length) {
|
|
|
|
return res.body[0];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updatePr(prNo, title, body) {
|
|
|
|
return ghGot.patch(`repos/${config.repoName}/pulls/${prNo}`, {
|
|
|
|
token: config.token,
|
2017-01-10 22:06:25 +00:00
|
|
|
body: { title, body },
|
2017-01-10 12:32:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic File operations
|
2017-01-10 21:28:22 +00:00
|
|
|
function getFile(filePath, branchName = config.defaultBranch) {
|
2017-01-11 06:14:14 +00:00
|
|
|
return ghGot(`repos/${config.repoName}/contents/${filePath}?ref=${branchName}`,
|
|
|
|
{
|
2017-01-11 06:54:27 +00:00
|
|
|
token: config.token,
|
2017-01-11 06:14:14 +00:00
|
|
|
});
|
2017-01-07 21:08:45 +00:00
|
|
|
}
|
2017-01-10 12:32:32 +00:00
|
|
|
|
|
|
|
function getFileContents(filePath, branchName) {
|
2017-01-11 06:14:14 +00:00
|
|
|
return getFile(filePath, branchName)
|
|
|
|
.then(res => JSON.parse(new Buffer(res.body.content, 'base64').toString()));
|
2017-01-10 12:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function writeFile(branchName, oldFileSHA, filePath, fileContents, message) {
|
|
|
|
return ghGot.put(`repos/${config.repoName}/contents/${filePath}`, {
|
|
|
|
token: config.token,
|
|
|
|
body: {
|
|
|
|
branch: branchName,
|
|
|
|
sha: oldFileSHA,
|
2017-01-10 21:28:22 +00:00
|
|
|
message,
|
|
|
|
content: new Buffer(fileContents).toString('base64'),
|
|
|
|
},
|
2017-01-10 12:32:32 +00:00
|
|
|
});
|
|
|
|
}
|