2017-10-17 19:46:49 +00:00
|
|
|
const get = require('./gl-got-wrapper');
|
2018-02-12 12:31:41 +00:00
|
|
|
const addrs = require('email-addresses');
|
2017-02-11 07:14:19 +00:00
|
|
|
|
2017-12-23 07:03:16 +00:00
|
|
|
let config = {};
|
2017-02-11 07:14:19 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2017-04-21 05:00:26 +00:00
|
|
|
getRepos,
|
2017-02-11 07:14:19 +00:00
|
|
|
initRepo,
|
2017-11-15 14:31:20 +00:00
|
|
|
getRepoForceRebase,
|
2017-07-06 08:26:18 +00:00
|
|
|
setBaseBranch,
|
2017-02-11 07:14:19 +00:00
|
|
|
// Search
|
2017-10-25 04:00:07 +00:00
|
|
|
getFileList,
|
2017-02-11 07:14:19 +00:00
|
|
|
// Branch
|
|
|
|
branchExists,
|
2017-11-05 07:18:20 +00:00
|
|
|
getAllRenovateBranches,
|
|
|
|
isBranchStale,
|
2017-02-11 07:14:19 +00:00
|
|
|
getBranchPr,
|
2017-04-17 04:46:24 +00:00
|
|
|
getBranchStatus,
|
2017-08-08 21:03:52 +00:00
|
|
|
getBranchStatusCheck,
|
2017-08-06 13:38:10 +00:00
|
|
|
setBranchStatus,
|
2017-06-22 09:56:23 +00:00
|
|
|
deleteBranch,
|
2017-11-05 07:18:20 +00:00
|
|
|
mergeBranch,
|
2017-08-28 09:37:09 +00:00
|
|
|
getBranchLastCommitTime,
|
2017-02-11 07:14:19 +00:00
|
|
|
// issue
|
2017-12-18 08:39:52 +00:00
|
|
|
ensureIssue,
|
|
|
|
ensureIssueClosing,
|
2017-02-11 07:14:19 +00:00
|
|
|
addAssignees,
|
|
|
|
addReviewers,
|
2017-10-19 11:30:26 +00:00
|
|
|
// Comments
|
2017-10-18 13:28:51 +00:00
|
|
|
ensureComment,
|
2017-10-19 11:30:26 +00:00
|
|
|
ensureCommentRemoval,
|
2017-02-11 07:14:19 +00:00
|
|
|
// PR
|
2018-01-11 10:49:01 +00:00
|
|
|
getPrList,
|
2017-02-11 07:14:19 +00:00
|
|
|
findPr,
|
|
|
|
createPr,
|
|
|
|
getPr,
|
|
|
|
updatePr,
|
2017-04-20 11:01:23 +00:00
|
|
|
mergePr,
|
2017-02-11 07:14:19 +00:00
|
|
|
// file
|
|
|
|
commitFilesToBranch,
|
|
|
|
getFile,
|
2017-07-07 05:54:09 +00:00
|
|
|
// commits
|
|
|
|
getCommitMessages,
|
2017-02-11 07:14:19 +00:00
|
|
|
};
|
|
|
|
|
2017-04-21 05:00:26 +00:00
|
|
|
// Get all repositories that the user has access to
|
|
|
|
async function getRepos(token, endpoint) {
|
|
|
|
logger.debug('getRepos(token, endpoint)');
|
|
|
|
if (token) {
|
|
|
|
process.env.GITLAB_TOKEN = token;
|
|
|
|
} else if (!process.env.GITLAB_TOKEN) {
|
|
|
|
throw new Error('No token found for getRepos');
|
|
|
|
}
|
|
|
|
if (endpoint) {
|
|
|
|
process.env.GITLAB_ENDPOINT = endpoint;
|
|
|
|
}
|
|
|
|
try {
|
2017-10-18 09:40:48 +00:00
|
|
|
const url = `projects?membership=true&per_page=100`;
|
|
|
|
const res = await get(url, { paginate: true });
|
|
|
|
logger.info(`Discovered ${res.body.length} project(s)`);
|
|
|
|
return res.body.map(repo => repo.path_with_namespace);
|
2017-04-21 05:00:26 +00:00
|
|
|
} catch (err) {
|
2017-07-19 06:05:26 +00:00
|
|
|
logger.error({ err }, `GitLab getRepos error`);
|
2017-04-21 05:00:26 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:19:24 +00:00
|
|
|
function urlEscape(str) {
|
2018-02-03 11:06:25 +00:00
|
|
|
return str ? str.replace(/\//g, '%2F') : str;
|
2017-12-14 05:19:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 07:14:19 +00:00
|
|
|
// Initialize GitLab by getting base branch
|
2018-01-25 11:24:13 +00:00
|
|
|
async function initRepo({ repository, token, endpoint }) {
|
2017-02-11 07:14:19 +00:00
|
|
|
if (token) {
|
|
|
|
process.env.GITLAB_TOKEN = token;
|
|
|
|
} else if (!process.env.GITLAB_TOKEN) {
|
2018-01-25 11:24:13 +00:00
|
|
|
throw new Error(`No token found for GitLab repository ${repository}`);
|
2017-02-11 07:14:19 +00:00
|
|
|
}
|
|
|
|
if (token) {
|
|
|
|
process.env.GITLAB_TOKEN = token;
|
|
|
|
}
|
|
|
|
if (endpoint) {
|
|
|
|
process.env.GITLAB_ENDPOINT = endpoint;
|
|
|
|
}
|
2017-12-23 07:03:16 +00:00
|
|
|
config = {};
|
|
|
|
get.reset();
|
2018-01-25 11:24:13 +00:00
|
|
|
config.repository = urlEscape(repository);
|
2017-02-11 07:14:19 +00:00
|
|
|
try {
|
2018-01-25 11:24:13 +00:00
|
|
|
const res = await get(`projects/${config.repository}`);
|
2017-07-06 12:12:52 +00:00
|
|
|
config.defaultBranch = res.body.default_branch;
|
|
|
|
config.baseBranch = config.defaultBranch;
|
2018-01-25 11:24:13 +00:00
|
|
|
logger.debug(`${repository} default branch = ${config.baseBranch}`);
|
2017-06-22 09:56:23 +00:00
|
|
|
// Discover our user email
|
2017-10-17 05:15:01 +00:00
|
|
|
config.email = (await get(`user`)).body.email;
|
2017-12-23 07:03:16 +00:00
|
|
|
delete config.prList;
|
|
|
|
delete config.fileList;
|
|
|
|
await Promise.all([getPrList(), getFileList()]);
|
2017-02-11 07:14:19 +00:00
|
|
|
} catch (err) {
|
2017-07-19 06:05:26 +00:00
|
|
|
logger.error({ err }, `GitLab init error`);
|
2017-02-11 07:14:19 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2017-07-26 08:56:11 +00:00
|
|
|
return {};
|
2017-02-11 07:14:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 14:31:20 +00:00
|
|
|
function getRepoForceRebase() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-03 09:39:04 +00:00
|
|
|
async function setBaseBranch(branchName) {
|
2017-07-06 08:26:18 +00:00
|
|
|
if (branchName) {
|
2018-02-03 09:39:04 +00:00
|
|
|
logger.debug(`Setting baseBranch to ${branchName}`);
|
2017-07-06 08:26:18 +00:00
|
|
|
config.baseBranch = branchName;
|
2018-02-03 09:39:04 +00:00
|
|
|
delete config.fileList;
|
|
|
|
await getFileList(branchName);
|
2017-07-06 08:26:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 07:14:19 +00:00
|
|
|
// Search
|
|
|
|
|
2017-10-16 09:59:59 +00:00
|
|
|
// Get full file list
|
2017-10-25 04:00:07 +00:00
|
|
|
async function getFileList(branchName = config.baseBranch) {
|
2017-10-16 09:59:59 +00:00
|
|
|
if (config.fileList) {
|
|
|
|
return config.fileList;
|
|
|
|
}
|
2017-10-18 06:25:42 +00:00
|
|
|
try {
|
|
|
|
const res = await get(
|
2017-12-05 10:50:16 +00:00
|
|
|
`projects/${
|
2018-01-25 11:24:13 +00:00
|
|
|
config.repository
|
2017-12-05 10:50:16 +00:00
|
|
|
}/repository/tree?ref=${branchName}&recursive=true&per_page=100`,
|
2017-10-18 06:25:42 +00:00
|
|
|
{ paginate: true }
|
|
|
|
);
|
|
|
|
config.fileList = res.body
|
2017-10-22 18:24:01 +00:00
|
|
|
.filter(item => item.type === 'blob' && item.mode !== '120000')
|
2017-10-18 06:25:42 +00:00
|
|
|
.map(item => item.path)
|
|
|
|
.sort();
|
|
|
|
} catch (err) {
|
2017-10-20 05:18:57 +00:00
|
|
|
logger.info('Error retrieving git tree - no files detected');
|
2017-10-18 06:25:42 +00:00
|
|
|
config.fileList = [];
|
|
|
|
}
|
2017-10-16 09:59:59 +00:00
|
|
|
return config.fileList;
|
|
|
|
}
|
|
|
|
|
2017-02-11 07:14:19 +00:00
|
|
|
// Branch
|
|
|
|
|
|
|
|
// Returns true if branch exists, otherwise false
|
|
|
|
async function branchExists(branchName) {
|
|
|
|
logger.debug(`Checking if branch exists: ${branchName}`);
|
|
|
|
try {
|
2018-01-25 11:24:13 +00:00
|
|
|
const url = `projects/${config.repository}/repository/branches/${urlEscape(
|
2017-12-14 05:19:24 +00:00
|
|
|
branchName
|
|
|
|
)}`;
|
2017-10-17 05:15:01 +00:00
|
|
|
const res = await get(url);
|
2017-02-11 07:14:19 +00:00
|
|
|
if (res.statusCode === 200) {
|
|
|
|
logger.debug('Branch exists');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// This probably shouldn't happen
|
2017-04-21 08:12:41 +00:00
|
|
|
logger.debug("Branch doesn't exist");
|
2017-02-11 07:14:19 +00:00
|
|
|
return false;
|
|
|
|
} catch (error) {
|
|
|
|
if (error.statusCode === 404) {
|
|
|
|
// If file not found, then return false
|
2017-04-21 08:12:41 +00:00
|
|
|
logger.debug("Branch doesn't exist");
|
2017-02-11 07:14:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Propagate if it's any other error
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:06:25 +00:00
|
|
|
async function getAllRenovateBranches(branchPrefix) {
|
|
|
|
logger.debug(`getAllRenovateBranches(${branchPrefix})`);
|
|
|
|
const allBranches = await get(
|
|
|
|
`projects/${config.repository}/repository/branches`
|
|
|
|
);
|
|
|
|
return allBranches.body.reduce((arr, branch) => {
|
|
|
|
if (branch.name.startsWith(branchPrefix)) {
|
|
|
|
arr.push(branch.name);
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}, []);
|
2017-11-05 07:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isBranchStale() {
|
|
|
|
logger.warn('Unimplemented in GitLab: isBranchStale');
|
|
|
|
return false;
|
2017-06-22 09:56:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 07:14:19 +00:00
|
|
|
// Returns the Pull Request for a branch. Null if not exists.
|
|
|
|
async function getBranchPr(branchName) {
|
|
|
|
logger.debug(`getBranchPr(${branchName})`);
|
2018-02-28 03:43:30 +00:00
|
|
|
if (!await branchExists(branchName)) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-11-07 10:52:15 +00:00
|
|
|
const urlString = `projects/${
|
2018-01-25 11:24:13 +00:00
|
|
|
config.repository
|
2017-11-07 10:52:15 +00:00
|
|
|
}/merge_requests?state=opened&per_page=100`;
|
2017-10-20 12:22:28 +00:00
|
|
|
const res = await get(urlString, { paginate: true });
|
2017-02-11 07:14:19 +00:00
|
|
|
logger.debug(`Got res with ${res.body.length} results`);
|
|
|
|
let pr = null;
|
2017-04-21 08:12:41 +00:00
|
|
|
res.body.forEach(result => {
|
2017-02-11 07:14:19 +00:00
|
|
|
if (result.source_branch === branchName) {
|
|
|
|
pr = result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!pr) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-11-01 09:36:58 +00:00
|
|
|
return getPr(pr.iid);
|
2017-02-11 07:14:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 04:46:24 +00:00
|
|
|
// Returns the combined status for a branch.
|
2017-07-05 05:02:25 +00:00
|
|
|
async function getBranchStatus(branchName, requiredStatusChecks) {
|
2017-04-17 04:46:24 +00:00
|
|
|
logger.debug(`getBranchStatus(${branchName})`);
|
2017-07-05 05:02:25 +00:00
|
|
|
if (!requiredStatusChecks) {
|
|
|
|
// null means disable status checks, so it always succeeds
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
if (requiredStatusChecks.length) {
|
|
|
|
// This is Unsupported
|
2017-07-19 06:05:26 +00:00
|
|
|
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
|
2017-07-05 05:02:25 +00:00
|
|
|
return 'failed';
|
|
|
|
}
|
2017-04-17 04:46:24 +00:00
|
|
|
// First, get the branch to find the commit SHA
|
2018-01-25 11:24:13 +00:00
|
|
|
let url = `projects/${config.repository}/repository/branches/${urlEscape(
|
2017-12-14 05:19:24 +00:00
|
|
|
branchName
|
|
|
|
)}`;
|
2017-10-17 05:15:01 +00:00
|
|
|
let res = await get(url);
|
2017-04-17 04:46:24 +00:00
|
|
|
const branchSha = res.body.commit.id;
|
|
|
|
// Now, check the statuses for that commit
|
2018-01-25 11:24:13 +00:00
|
|
|
url = `projects/${
|
|
|
|
config.repository
|
|
|
|
}/repository/commits/${branchSha}/statuses`;
|
2017-10-17 05:15:01 +00:00
|
|
|
res = await get(url);
|
2017-04-17 04:46:24 +00:00
|
|
|
logger.debug(`Got res with ${res.body.length} results`);
|
|
|
|
if (res.body.length === 0) {
|
|
|
|
// Return 'pending' if we have no status checks
|
|
|
|
return 'pending';
|
|
|
|
}
|
|
|
|
let status = 'success';
|
|
|
|
// Return 'success' if all are success
|
2017-04-21 08:12:41 +00:00
|
|
|
res.body.forEach(check => {
|
2017-04-17 04:46:24 +00:00
|
|
|
// If one is failed then don't overwrite that
|
2017-06-16 13:24:59 +00:00
|
|
|
if (status !== 'failure') {
|
2018-03-05 20:02:00 +00:00
|
|
|
if (!check.allow_failure) {
|
|
|
|
if (check.status === 'failed') {
|
|
|
|
status = 'failure';
|
|
|
|
} else if (check.status !== 'success') {
|
|
|
|
({ status } = check);
|
|
|
|
}
|
2017-04-17 04:46:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-08-08 21:03:52 +00:00
|
|
|
async function getBranchStatusCheck(branchName, context) {
|
|
|
|
// First, get the branch to find the commit SHA
|
2018-01-25 11:24:13 +00:00
|
|
|
let url = `projects/${config.repository}/repository/branches/${urlEscape(
|
2017-12-14 05:19:24 +00:00
|
|
|
branchName
|
|
|
|
)}`;
|
2017-10-17 05:15:01 +00:00
|
|
|
let res = await get(url);
|
2017-08-08 21:03:52 +00:00
|
|
|
const branchSha = res.body.commit.id;
|
|
|
|
// Now, check the statuses for that commit
|
2018-01-25 11:24:13 +00:00
|
|
|
url = `projects/${
|
|
|
|
config.repository
|
|
|
|
}/repository/commits/${branchSha}/statuses`;
|
2017-10-17 05:15:01 +00:00
|
|
|
res = await get(url);
|
2017-08-08 21:03:52 +00:00
|
|
|
logger.debug(`Got res with ${res.body.length} results`);
|
|
|
|
for (const check of res.body) {
|
|
|
|
if (check.name === context) {
|
|
|
|
return check.state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-08-06 13:38:10 +00:00
|
|
|
async function setBranchStatus(
|
|
|
|
branchName,
|
|
|
|
context,
|
|
|
|
description,
|
|
|
|
state,
|
|
|
|
targetUrl
|
|
|
|
) {
|
|
|
|
// First, get the branch to find the commit SHA
|
2018-01-25 11:24:13 +00:00
|
|
|
let url = `projects/${config.repository}/repository/branches/${urlEscape(
|
2017-12-14 05:19:24 +00:00
|
|
|
branchName
|
|
|
|
)}`;
|
2017-10-17 05:15:01 +00:00
|
|
|
const res = await get(url);
|
2017-08-06 13:38:10 +00:00
|
|
|
const branchSha = res.body.commit.id;
|
|
|
|
// Now, check the statuses for that commit
|
2018-01-25 11:24:13 +00:00
|
|
|
url = `projects/${config.repository}/statuses/${branchSha}`;
|
2017-08-06 13:38:10 +00:00
|
|
|
const options = {
|
|
|
|
state,
|
|
|
|
description,
|
|
|
|
context,
|
|
|
|
};
|
|
|
|
if (targetUrl) {
|
|
|
|
options.target_url = targetUrl;
|
|
|
|
}
|
2017-10-17 05:15:01 +00:00
|
|
|
await get.post(url, { body: options });
|
2017-08-06 13:38:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-03 11:06:25 +00:00
|
|
|
async function deleteBranch(branchName, closePr = false) {
|
|
|
|
if (closePr) {
|
|
|
|
logger.debug('Closing PR');
|
|
|
|
const pr = await getBranchPr(branchName);
|
|
|
|
// istanbul ignore if
|
|
|
|
if (pr) {
|
|
|
|
await get.put(
|
|
|
|
`projects/${config.repository}/merge_requests/${pr.number}`,
|
|
|
|
{
|
|
|
|
body: {
|
|
|
|
state_event: 'close',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 05:15:01 +00:00
|
|
|
await get.delete(
|
2018-01-25 11:24:13 +00:00
|
|
|
`projects/${config.repository}/repository/branches/${urlEscape(branchName)}`
|
2017-06-22 09:56:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-05 07:18:20 +00:00
|
|
|
function mergeBranch() {
|
|
|
|
logger.warn('Unimplemented in GitLab: mergeBranch');
|
|
|
|
}
|
|
|
|
|
2017-08-28 09:37:09 +00:00
|
|
|
async function getBranchLastCommitTime(branchName) {
|
|
|
|
try {
|
2017-10-17 05:15:01 +00:00
|
|
|
const res = await get(
|
2018-01-25 11:24:13 +00:00
|
|
|
`projects/${config.repository}/repository/commits?ref_name=${urlEscape(
|
2017-12-14 05:19:24 +00:00
|
|
|
branchName
|
|
|
|
)}`
|
2017-08-28 09:37:09 +00:00
|
|
|
);
|
|
|
|
return new Date(res.body[0].committed_date);
|
|
|
|
} catch (err) {
|
|
|
|
logger.error({ err }, `getBranchLastCommitTime error`);
|
|
|
|
return new Date();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 07:14:19 +00:00
|
|
|
// Issue
|
|
|
|
|
2017-12-18 08:39:52 +00:00
|
|
|
function ensureIssue() {
|
|
|
|
// istanbul ignore next
|
|
|
|
logger.warn(`ensureIssue() is not implemented`);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureIssueClosing() {}
|
|
|
|
|
2017-11-01 12:55:36 +00:00
|
|
|
async function addAssignees(iid, assignees) {
|
|
|
|
logger.debug(`Adding assignees ${assignees} to #${iid}`);
|
2017-02-11 07:14:19 +00:00
|
|
|
if (assignees.length > 1) {
|
2017-11-10 08:59:12 +00:00
|
|
|
logger.warn('Cannot assign more than one assignee to Merge Requests');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const assigneeId = (await get(`users?username=${assignees[0]}`)).body[0].id;
|
2018-01-25 11:24:13 +00:00
|
|
|
let url = `projects/${config.repository}/merge_requests/${iid}`;
|
2017-11-10 08:59:12 +00:00
|
|
|
url += `?assignee_id=${assigneeId}`;
|
|
|
|
await get.put(url);
|
|
|
|
} catch (err) {
|
|
|
|
logger.error({ iid, assignees }, 'Failed to add assignees');
|
2017-02-11 07:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 08:29:24 +00:00
|
|
|
function addReviewers(iid, reviewers) {
|
|
|
|
logger.debug(`addReviewers('${iid}, '${reviewers})`);
|
2017-02-11 07:14:19 +00:00
|
|
|
logger.error('No reviewer functionality in GitLab');
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:28:51 +00:00
|
|
|
async function ensureComment() {
|
|
|
|
// Todo: implement. See GitHub API for example
|
|
|
|
}
|
|
|
|
|
2017-10-19 11:30:26 +00:00
|
|
|
async function ensureCommentRemoval() {
|
|
|
|
// Todo: implement. See GitHub API for example
|
|
|
|
}
|
|
|
|
|
2017-11-14 08:55:05 +00:00
|
|
|
async function getPrList() {
|
|
|
|
if (!config.prList) {
|
2018-01-25 11:24:13 +00:00
|
|
|
const urlString = `projects/${
|
|
|
|
config.repository
|
|
|
|
}/merge_requests?per_page=100`;
|
2017-11-14 08:55:05 +00:00
|
|
|
const res = await get(urlString, { paginate: true });
|
|
|
|
config.prList = res.body.map(pr => ({
|
|
|
|
number: pr.iid,
|
|
|
|
branchName: pr.source_branch,
|
|
|
|
title: pr.title,
|
2017-11-24 06:31:20 +00:00
|
|
|
state: pr.state === 'opened' ? 'open' : pr.state,
|
2018-01-11 10:49:01 +00:00
|
|
|
createdAt: pr.created_at,
|
2017-11-14 08:55:05 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
return config.prList;
|
|
|
|
}
|
|
|
|
|
2017-11-24 06:31:20 +00:00
|
|
|
function matchesState(state, desiredState) {
|
|
|
|
if (desiredState === 'all') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (desiredState[0] === '!') {
|
|
|
|
return state !== desiredState.substring(1);
|
|
|
|
}
|
|
|
|
return state === desiredState;
|
|
|
|
}
|
|
|
|
|
2017-02-11 07:14:19 +00:00
|
|
|
async function findPr(branchName, prTitle, state = 'all') {
|
|
|
|
logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`);
|
2017-11-14 08:55:05 +00:00
|
|
|
const prList = await getPrList();
|
|
|
|
return prList.find(
|
|
|
|
p =>
|
|
|
|
p.branchName === branchName &&
|
|
|
|
(!prTitle || p.title === prTitle) &&
|
2017-11-24 06:31:20 +00:00
|
|
|
matchesState(p.state, state)
|
2017-11-14 08:55:05 +00:00
|
|
|
);
|
2017-02-11 07:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pull Request
|
|
|
|
|
2017-11-08 10:09:26 +00:00
|
|
|
async function createPr(
|
|
|
|
branchName,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
labels,
|
|
|
|
useDefaultBranch
|
|
|
|
) {
|
2017-07-06 12:12:52 +00:00
|
|
|
const targetBranch = useDefaultBranch
|
|
|
|
? config.defaultBranch
|
|
|
|
: config.baseBranch;
|
2017-02-11 07:14:19 +00:00
|
|
|
logger.debug(`Creating Merge Request: ${title}`);
|
2018-01-25 11:24:13 +00:00
|
|
|
const res = await get.post(`projects/${config.repository}/merge_requests`, {
|
2017-02-11 07:14:19 +00:00
|
|
|
body: {
|
|
|
|
source_branch: branchName,
|
2017-07-06 12:12:52 +00:00
|
|
|
target_branch: targetBranch,
|
2017-03-13 09:21:28 +00:00
|
|
|
remove_source_branch: true,
|
2017-02-11 07:14:19 +00:00
|
|
|
title,
|
2017-06-28 08:10:40 +00:00
|
|
|
description,
|
2017-11-01 12:55:36 +00:00
|
|
|
labels: Array.isArray(labels) ? labels.join(',') : null,
|
2017-02-11 07:14:19 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
const pr = res.body;
|
2017-11-10 08:29:24 +00:00
|
|
|
pr.number = pr.iid;
|
2018-02-19 20:23:37 +00:00
|
|
|
pr.branchName = branchName;
|
2017-02-11 07:14:19 +00:00
|
|
|
pr.displayNumber = `Merge Request #${pr.iid}`;
|
|
|
|
return pr;
|
|
|
|
}
|
|
|
|
|
2017-11-10 08:29:24 +00:00
|
|
|
async function getPr(iid) {
|
|
|
|
logger.debug(`getPr(${iid})`);
|
2018-01-25 11:24:13 +00:00
|
|
|
const url = `projects/${config.repository}/merge_requests/${iid}`;
|
2017-10-17 05:15:01 +00:00
|
|
|
const pr = (await get(url)).body;
|
2017-02-11 07:14:19 +00:00
|
|
|
// Harmonize fields with GitHub
|
2018-02-19 19:01:10 +00:00
|
|
|
pr.branchName = pr.source_branch;
|
2017-11-01 09:36:58 +00:00
|
|
|
pr.number = pr.iid;
|
2017-02-11 07:14:19 +00:00
|
|
|
pr.displayNumber = `Merge Request #${pr.iid}`;
|
|
|
|
pr.body = pr.description;
|
|
|
|
if (pr.merge_status === 'cannot_be_merged') {
|
|
|
|
logger.debug('pr cannot be merged');
|
2018-02-27 18:50:16 +00:00
|
|
|
pr.canMerge = false;
|
2017-02-11 07:14:19 +00:00
|
|
|
pr.isUnmergeable = true;
|
2018-02-27 18:50:16 +00:00
|
|
|
} else {
|
|
|
|
// Actually.. we can't be sure
|
|
|
|
pr.canMerge = true;
|
2017-02-11 07:14:19 +00:00
|
|
|
}
|
2017-06-22 09:56:23 +00:00
|
|
|
// Check if the most recent branch commit is by us
|
|
|
|
// If not then we don't allow it to be rebased, in case someone's changes would be lost
|
2017-11-07 10:52:15 +00:00
|
|
|
const branchUrl = `projects/${
|
2018-01-25 11:24:13 +00:00
|
|
|
config.repository
|
2017-12-14 05:19:24 +00:00
|
|
|
}/repository/branches/${urlEscape(pr.source_branch)}`;
|
2018-02-28 03:43:30 +00:00
|
|
|
try {
|
|
|
|
const branch = (await get(branchUrl)).body;
|
|
|
|
if (
|
|
|
|
branch &&
|
|
|
|
branch.commit &&
|
|
|
|
branch.commit.author_email === config.email
|
|
|
|
) {
|
|
|
|
pr.canRebase = true;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn({ err }, 'Error getting PR branch');
|
|
|
|
pr.isUnmergeable = true;
|
2017-06-22 09:56:23 +00:00
|
|
|
}
|
2017-02-11 07:14:19 +00:00
|
|
|
return pr;
|
|
|
|
}
|
|
|
|
|
2017-11-10 08:29:24 +00:00
|
|
|
async function updatePr(iid, title, description) {
|
2018-01-25 11:24:13 +00:00
|
|
|
await get.put(`projects/${config.repository}/merge_requests/${iid}`, {
|
2017-02-11 07:14:19 +00:00
|
|
|
body: {
|
|
|
|
title,
|
2017-06-28 08:10:40 +00:00
|
|
|
description,
|
2017-02-11 07:14:19 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-10 08:29:24 +00:00
|
|
|
async function mergePr(iid) {
|
2018-01-25 11:24:13 +00:00
|
|
|
await get.put(`projects/${config.repository}/merge_requests/${iid}/merge`, {
|
2017-11-10 08:29:24 +00:00
|
|
|
body: {
|
|
|
|
should_remove_source_branch: true,
|
|
|
|
},
|
|
|
|
});
|
2017-08-31 05:15:53 +00:00
|
|
|
return true;
|
2017-04-20 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 07:14:19 +00:00
|
|
|
// Generic File operations
|
|
|
|
|
2017-11-08 11:23:32 +00:00
|
|
|
async function getFile(filePath, branchName) {
|
2017-12-23 07:03:16 +00:00
|
|
|
logger.debug(`getFile(filePath=${filePath}, branchName=${branchName})`);
|
|
|
|
if (!branchName || branchName === config.baseBranch) {
|
|
|
|
if (config.fileList && !config.fileList.includes(filePath)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 07:14:19 +00:00
|
|
|
try {
|
2018-01-25 11:24:13 +00:00
|
|
|
const url = `projects/${config.repository}/repository/files/${urlEscape(
|
2017-12-14 05:19:24 +00:00
|
|
|
filePath
|
|
|
|
)}?ref=${branchName || config.baseBranch}`;
|
2017-11-08 11:23:32 +00:00
|
|
|
const res = await get(url);
|
|
|
|
return Buffer.from(res.body.content, 'base64').toString();
|
2017-02-11 07:14:19 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (error.statusCode === 404) {
|
|
|
|
// If file not found, then return null JSON
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Propagate if it's any other error
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new commit, create branch if not existing
|
|
|
|
async function commitFilesToBranch(
|
|
|
|
branchName,
|
|
|
|
files,
|
|
|
|
message,
|
2018-02-12 12:31:41 +00:00
|
|
|
parentBranch = config.baseBranch,
|
|
|
|
gitAuthor
|
2017-04-21 08:12:41 +00:00
|
|
|
) {
|
2017-05-10 07:26:09 +00:00
|
|
|
logger.debug(
|
|
|
|
`commitFilesToBranch('${branchName}', files, message, '${parentBranch})'`
|
|
|
|
);
|
2018-02-12 05:20:20 +00:00
|
|
|
const opts = {
|
|
|
|
body: {
|
|
|
|
branch: branchName,
|
|
|
|
commit_message: message,
|
|
|
|
start_branch: parentBranch,
|
|
|
|
actions: [],
|
|
|
|
},
|
|
|
|
};
|
2018-02-12 12:31:41 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (gitAuthor) {
|
|
|
|
logger.debug({ gitAuthor }, 'Found gitAuthor');
|
|
|
|
const { name, address } = addrs.parseOneAddress(gitAuthor);
|
|
|
|
if (name && address) {
|
|
|
|
opts.body.author_name = name;
|
|
|
|
opts.body.author_email = address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn({ gitAuthor }, 'Error parsing gitAuthor');
|
|
|
|
}
|
|
|
|
|
2017-02-11 07:14:19 +00:00
|
|
|
for (const file of files) {
|
2018-02-12 05:20:20 +00:00
|
|
|
const action = {
|
|
|
|
file_path: file.name,
|
|
|
|
content: Buffer.from(file.contents).toString('base64'),
|
|
|
|
encoding: 'base64',
|
|
|
|
};
|
|
|
|
action.action = (await getFile(file.name)) ? 'update' : 'create';
|
|
|
|
opts.body.actions.push(action);
|
2017-02-11 07:14:19 +00:00
|
|
|
}
|
2018-02-12 05:58:33 +00:00
|
|
|
if (await branchExists(branchName)) {
|
|
|
|
await deleteBranch(branchName);
|
|
|
|
}
|
2018-02-12 05:20:20 +00:00
|
|
|
await get.post(`projects/${config.repository}/repository/commits`, opts);
|
2017-02-11 07:14:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 05:54:09 +00:00
|
|
|
// GET /projects/:id/repository/commits
|
|
|
|
async function getCommitMessages() {
|
|
|
|
logger.debug('getCommitMessages');
|
|
|
|
try {
|
2018-01-25 11:24:13 +00:00
|
|
|
const res = await get(`projects/${config.repository}/repository/commits`);
|
2017-07-07 05:54:09 +00:00
|
|
|
return res.body.map(commit => commit.title);
|
|
|
|
} catch (err) {
|
2017-07-19 06:05:26 +00:00
|
|
|
logger.error({ err }, `getCommitMessages error`);
|
2017-07-07 05:54:09 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|