2017-10-17 19:46:49 +00:00
|
|
|
const get = require('./gl-got-wrapper');
|
2017-02-11 07:14:19 +00:00
|
|
|
|
2017-11-05 07:18:20 +00:00
|
|
|
const { createFile, updateFile } = require('./helpers');
|
|
|
|
|
2017-02-11 07:14:19 +00:00
|
|
|
const config = {};
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
findPr,
|
|
|
|
createPr,
|
|
|
|
getPr,
|
2017-11-10 12:07:06 +00:00
|
|
|
getPrFiles,
|
2017-02-11 07:14:19 +00:00
|
|
|
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-02-11 07:14:19 +00:00
|
|
|
// Initialize GitLab by getting base branch
|
2017-11-08 05:44:03 +00:00
|
|
|
async function initRepo(repoName, token, endpoint) {
|
2017-02-11 07:14:19 +00:00
|
|
|
logger.debug(`initRepo(${repoName})`);
|
|
|
|
if (token) {
|
|
|
|
process.env.GITLAB_TOKEN = token;
|
|
|
|
} else if (!process.env.GITLAB_TOKEN) {
|
|
|
|
throw new Error(`No token found for GitLab repository ${repoName}`);
|
|
|
|
}
|
|
|
|
if (token) {
|
|
|
|
process.env.GITLAB_TOKEN = token;
|
|
|
|
}
|
|
|
|
if (endpoint) {
|
|
|
|
process.env.GITLAB_ENDPOINT = endpoint;
|
|
|
|
}
|
|
|
|
config.repoName = repoName.replace('/', '%2F');
|
2017-10-16 09:59:59 +00:00
|
|
|
config.fileList = null;
|
2017-11-14 08:55:05 +00:00
|
|
|
config.prList = null;
|
2017-02-11 07:14:19 +00:00
|
|
|
try {
|
2017-10-17 05:15:01 +00:00
|
|
|
const res = await get(`projects/${config.repoName}`);
|
2017-07-06 12:12:52 +00:00
|
|
|
config.defaultBranch = res.body.default_branch;
|
|
|
|
config.baseBranch = config.defaultBranch;
|
2017-07-06 08:26:18 +00:00
|
|
|
logger.debug(`${repoName} 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-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;
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:56:25 +00:00
|
|
|
function setBaseBranch(branchName) {
|
2017-07-06 08:26:18 +00:00
|
|
|
if (branchName) {
|
|
|
|
config.baseBranch = branchName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-07 10:52:15 +00:00
|
|
|
`projects/${config.repoName}/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 {
|
2017-11-07 10:52:15 +00:00
|
|
|
const url = `projects/${
|
|
|
|
config.repoName
|
|
|
|
}/repository/branches/${branchName.replace('/', '%2F')}`;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 07:18:20 +00:00
|
|
|
function getAllRenovateBranches() {
|
|
|
|
logger.warn('Unimplemented in GitLab: getAllRenovateBranches');
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
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})`);
|
2017-11-07 10:52:15 +00:00
|
|
|
const urlString = `projects/${
|
|
|
|
config.repoName
|
|
|
|
}/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
|
2017-11-07 10:52:15 +00:00
|
|
|
let url = `projects/${
|
|
|
|
config.repoName
|
|
|
|
}/repository/branches/${branchName.replace('/', '%2F')}`;
|
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
|
|
|
|
url = `projects/${config.repoName}/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') {
|
2017-04-17 04:46:24 +00:00
|
|
|
if (check.status === 'failed') {
|
2017-06-16 13:24:59 +00:00
|
|
|
status = 'failure';
|
2017-04-17 04:46:24 +00:00
|
|
|
} else if (check.status !== 'success') {
|
2017-09-15 17:46:25 +00:00
|
|
|
({ 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
|
2017-11-07 10:52:15 +00:00
|
|
|
let url = `projects/${
|
|
|
|
config.repoName
|
|
|
|
}/repository/branches/${branchName.replace('/', '%2F')}`;
|
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
|
|
|
|
url = `projects/${config.repoName}/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
|
2017-11-07 10:52:15 +00:00
|
|
|
let url = `projects/${
|
|
|
|
config.repoName
|
|
|
|
}/repository/branches/${branchName.replace('/', '%2F')}`;
|
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
|
|
|
|
url = `projects/${config.repoName}/statuses/${branchSha}`;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-06-22 09:56:23 +00:00
|
|
|
async function deleteBranch(branchName) {
|
2017-10-17 05:15:01 +00:00
|
|
|
await get.delete(
|
2017-08-31 19:06:19 +00:00
|
|
|
`projects/${config.repoName}/repository/branches/${branchName.replace(
|
|
|
|
'/',
|
|
|
|
'%2F'
|
|
|
|
)}`
|
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(
|
2017-08-28 09:37:09 +00:00
|
|
|
`projects/${config.repoName}/repository/commits?ref_name=${branchName}`
|
|
|
|
);
|
|
|
|
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-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;
|
|
|
|
let url = `projects/${config.repoName}/merge_requests/${iid}`;
|
|
|
|
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) {
|
|
|
|
const urlString = `projects/${config.repoName}/merge_requests?per_page=100`;
|
|
|
|
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,
|
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}`);
|
2017-10-17 05:15:01 +00:00
|
|
|
const res = await get.post(`projects/${config.repoName}/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;
|
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})`);
|
|
|
|
const url = `projects/${config.repoName}/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
|
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');
|
|
|
|
pr.isUnmergeable = true;
|
|
|
|
}
|
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/${
|
|
|
|
config.repoName
|
|
|
|
}/repository/branches/${pr.source_branch.replace('/', '%2F')}`;
|
2017-11-05 07:18:20 +00:00
|
|
|
const branch = (await get(branchUrl)).body;
|
|
|
|
if (branch && branch.commit && branch.commit.author_email === config.email) {
|
2017-06-22 09:56:23 +00:00
|
|
|
pr.canRebase = true;
|
|
|
|
}
|
2017-02-11 07:14:19 +00:00
|
|
|
return pr;
|
|
|
|
}
|
|
|
|
|
2017-11-10 12:07:06 +00:00
|
|
|
function getPrFiles() {
|
|
|
|
// TODO
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-11-10 08:29:24 +00:00
|
|
|
async function updatePr(iid, title, description) {
|
|
|
|
await get.put(`projects/${config.repoName}/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) {
|
|
|
|
await get.put(`projects/${config.repoName}/merge_requests/${iid}/merge`, {
|
|
|
|
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-02-11 07:14:19 +00:00
|
|
|
try {
|
2017-11-08 11:23:32 +00:00
|
|
|
const url = `projects/${
|
|
|
|
config.repoName
|
|
|
|
}/repository/files/${filePath.replace(/\//g, '%2F')}?ref=${branchName ||
|
|
|
|
config.baseBranch}`;
|
|
|
|
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,
|
2017-07-06 08:26:18 +00:00
|
|
|
parentBranch = config.baseBranch
|
2017-04-21 08:12:41 +00:00
|
|
|
) {
|
2017-05-10 07:26:09 +00:00
|
|
|
logger.debug(
|
|
|
|
`commitFilesToBranch('${branchName}', files, message, '${parentBranch})'`
|
|
|
|
);
|
2017-02-11 07:14:19 +00:00
|
|
|
if (branchName !== parentBranch) {
|
|
|
|
const isBranchExisting = await branchExists(branchName);
|
|
|
|
if (isBranchExisting) {
|
|
|
|
logger.debug(`Branch ${branchName} already exists`);
|
|
|
|
} else {
|
|
|
|
logger.debug(`Creating branch ${branchName}`);
|
2017-11-05 07:18:20 +00:00
|
|
|
const opts = {
|
|
|
|
body: {
|
|
|
|
branch: branchName,
|
2017-11-09 14:30:00 +00:00
|
|
|
ref: config.baseBranch,
|
2017-11-05 07:18:20 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
await get.post(`projects/${config.repoName}/repository/branches`, opts);
|
2017-02-11 07:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const file of files) {
|
2017-11-08 11:23:32 +00:00
|
|
|
const existingFile = await getFile(file.name, branchName);
|
2017-02-11 07:14:19 +00:00
|
|
|
if (existingFile) {
|
|
|
|
logger.debug(`${file.name} exists - updating it`);
|
2017-11-05 07:18:20 +00:00
|
|
|
await updateFile(
|
|
|
|
config.repoName,
|
|
|
|
branchName,
|
|
|
|
file.name,
|
|
|
|
file.contents,
|
|
|
|
message
|
|
|
|
);
|
2017-02-11 07:14:19 +00:00
|
|
|
} else {
|
|
|
|
logger.debug(`Creating file ${file.name}`);
|
2017-11-05 07:18:20 +00:00
|
|
|
await createFile(
|
|
|
|
config.repoName,
|
|
|
|
branchName,
|
|
|
|
file.name,
|
|
|
|
file.contents,
|
|
|
|
message
|
|
|
|
);
|
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 {
|
2017-10-17 05:15:01 +00:00
|
|
|
const res = await get(`projects/${config.repoName}/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 [];
|
|
|
|
}
|
|
|
|
}
|