renovate/lib/platform/gitlab/index.js

523 lines
13 KiB
JavaScript
Raw Normal View History

const get = require('./gl-got-wrapper');
2017-02-11 07:14:19 +00:00
const { createFile, updateFile } = require('./helpers');
2017-02-11 07:14:19 +00:00
const config = {};
module.exports = {
getRepos,
2017-02-11 07:14:19 +00:00
initRepo,
getRepoForceRebase,
setBaseBranch,
2017-02-11 07:14:19 +00:00
// Search
getFileList,
2017-02-11 07:14:19 +00:00
// Branch
branchExists,
getAllRenovateBranches,
isBranchStale,
2017-02-11 07:14:19 +00:00
getBranchPr,
getBranchStatus,
getBranchStatusCheck,
setBranchStatus,
deleteBranch,
mergeBranch,
getBranchLastCommitTime,
2017-02-11 07:14:19 +00:00
// issue
addAssignees,
addReviewers,
// Comments
ensureComment,
ensureCommentRemoval,
2017-02-11 07:14:19 +00:00
// PR
findPr,
createPr,
getPr,
getPrFiles,
2017-02-11 07:14:19 +00:00
updatePr,
mergePr,
2017-02-11 07:14:19 +00:00
// file
commitFilesToBranch,
getFile,
// commits
getCommitMessages,
2017-02-11 07:14:19 +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 {
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);
} catch (err) {
logger.error({ err }, `GitLab getRepos error`);
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');
config.fileList = null;
config.prList = null;
2017-02-11 07:14:19 +00:00
try {
const res = await get(`projects/${config.repoName}`);
config.defaultBranch = res.body.default_branch;
config.baseBranch = config.defaultBranch;
logger.debug(`${repoName} default branch = ${config.baseBranch}`);
// Discover our user email
config.email = (await get(`user`)).body.email;
2017-02-11 07:14:19 +00:00
} catch (err) {
logger.error({ err }, `GitLab init error`);
2017-02-11 07:14:19 +00:00
throw err;
}
return {};
2017-02-11 07:14:19 +00:00
}
function getRepoForceRebase() {
return false;
}
function setBaseBranch(branchName) {
if (branchName) {
config.baseBranch = branchName;
}
}
2017-02-11 07:14:19 +00:00
// Search
// Get full file list
async function getFileList(branchName = config.baseBranch) {
if (config.fileList) {
return config.fileList;
}
try {
const res = await get(
`projects/${config.repoName}/repository/tree?ref=${
branchName
}&recursive=true&per_page=100`,
{ paginate: true }
);
config.fileList = res.body
.filter(item => item.type === 'blob' && item.mode !== '120000')
.map(item => item.path)
.sort();
} catch (err) {
logger.info('Error retrieving git tree - no files detected');
config.fileList = [];
}
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 {
const url = `projects/${
config.repoName
}/repository/branches/${branchName.replace('/', '%2F')}`;
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;
}
}
function getAllRenovateBranches() {
logger.warn('Unimplemented in GitLab: getAllRenovateBranches');
return [];
}
function isBranchStale() {
logger.warn('Unimplemented in GitLab: isBranchStale');
return false;
}
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})`);
const urlString = `projects/${
config.repoName
}/merge_requests?state=opened&per_page=100`;
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;
}
return getPr(pr.iid);
2017-02-11 07:14:19 +00:00
}
// Returns the combined status for a branch.
async function getBranchStatus(branchName, requiredStatusChecks) {
logger.debug(`getBranchStatus(${branchName})`);
if (!requiredStatusChecks) {
// null means disable status checks, so it always succeeds
return 'success';
}
if (requiredStatusChecks.length) {
// This is Unsupported
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
return 'failed';
}
// First, get the branch to find the commit SHA
let url = `projects/${
config.repoName
}/repository/branches/${branchName.replace('/', '%2F')}`;
let res = await get(url);
const branchSha = res.body.commit.id;
// Now, check the statuses for that commit
url = `projects/${config.repoName}/repository/commits/${branchSha}/statuses`;
res = await get(url);
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 => {
// If one is failed then don't overwrite that
if (status !== 'failure') {
if (check.status === 'failed') {
status = 'failure';
} else if (check.status !== 'success') {
({ status } = check);
}
}
});
return status;
}
async function getBranchStatusCheck(branchName, context) {
// First, get the branch to find the commit SHA
let url = `projects/${
config.repoName
}/repository/branches/${branchName.replace('/', '%2F')}`;
let res = await get(url);
const branchSha = res.body.commit.id;
// Now, check the statuses for that commit
url = `projects/${config.repoName}/repository/commits/${branchSha}/statuses`;
res = await get(url);
logger.debug(`Got res with ${res.body.length} results`);
for (const check of res.body) {
if (check.name === context) {
return check.state;
}
}
return null;
}
async function setBranchStatus(
branchName,
context,
description,
state,
targetUrl
) {
// First, get the branch to find the commit SHA
let url = `projects/${
config.repoName
}/repository/branches/${branchName.replace('/', '%2F')}`;
const res = await get(url);
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;
}
await get.post(url, { body: options });
}
async function deleteBranch(branchName) {
await get.delete(
`projects/${config.repoName}/repository/branches/${branchName.replace(
'/',
'%2F'
)}`
);
}
function mergeBranch() {
logger.warn('Unimplemented in GitLab: mergeBranch');
}
async function getBranchLastCommitTime(branchName) {
try {
const res = await get(
`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
async function addAssignees(iid, assignees) {
logger.debug(`Adding assignees ${assignees} to #${iid}`);
2017-02-11 07:14:19 +00:00
if (assignees.length > 1) {
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
}
}
function addReviewers(iid, reviewers) {
logger.debug(`addReviewers('${iid}, '${reviewers})`);
2017-02-11 07:14:19 +00:00
logger.error('No reviewer functionality in GitLab');
}
async function ensureComment() {
// Todo: implement. See GitHub API for example
}
async function ensureCommentRemoval() {
// Todo: implement. See GitHub API for example
}
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,
state: pr.state === 'opened' ? 'open' : pr.state,
}));
}
return config.prList;
}
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})`);
const prList = await getPrList();
return prList.find(
p =>
p.branchName === branchName &&
(!prTitle || p.title === prTitle) &&
matchesState(p.state, state)
);
2017-02-11 07:14:19 +00:00
}
// Pull Request
async function createPr(
branchName,
title,
description,
labels,
useDefaultBranch
) {
const targetBranch = useDefaultBranch
? config.defaultBranch
: config.baseBranch;
2017-02-11 07:14:19 +00:00
logger.debug(`Creating Merge Request: ${title}`);
const res = await get.post(`projects/${config.repoName}/merge_requests`, {
2017-02-11 07:14:19 +00:00
body: {
source_branch: branchName,
target_branch: targetBranch,
remove_source_branch: true,
2017-02-11 07:14:19 +00:00
title,
description,
labels: Array.isArray(labels) ? labels.join(',') : null,
2017-02-11 07:14:19 +00:00
},
});
const pr = res.body;
pr.number = pr.iid;
2017-02-11 07:14:19 +00:00
pr.displayNumber = `Merge Request #${pr.iid}`;
return pr;
}
async function getPr(iid) {
logger.debug(`getPr(${iid})`);
const url = `projects/${config.repoName}/merge_requests/${iid}`;
const pr = (await get(url)).body;
2017-02-11 07:14:19 +00:00
// Harmonize fields with GitHub
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;
}
// 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
const branchUrl = `projects/${
config.repoName
}/repository/branches/${pr.source_branch.replace('/', '%2F')}`;
const branch = (await get(branchUrl)).body;
if (branch && branch.commit && branch.commit.author_email === config.email) {
pr.canRebase = true;
}
2017-02-11 07:14:19 +00:00
return pr;
}
function getPrFiles() {
// TODO
return [];
}
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,
description,
2017-02-11 07:14:19 +00:00
},
});
}
async function mergePr(iid) {
await get.put(`projects/${config.repoName}/merge_requests/${iid}/merge`, {
body: {
should_remove_source_branch: true,
},
});
return true;
}
2017-02-11 07:14:19 +00:00
// Generic File operations
async function getFile(filePath, branchName) {
2017-02-11 07:14:19 +00:00
try {
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,
parentBranch = config.baseBranch
2017-04-21 08:12:41 +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}`);
const opts = {
body: {
branch: branchName,
ref: config.baseBranch,
},
};
await get.post(`projects/${config.repoName}/repository/branches`, opts);
2017-02-11 07:14:19 +00:00
}
}
for (const file of files) {
const existingFile = await getFile(file.name, branchName);
2017-02-11 07:14:19 +00:00
if (existingFile) {
logger.debug(`${file.name} exists - updating it`);
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}`);
await createFile(
config.repoName,
branchName,
file.name,
file.contents,
message
);
2017-02-11 07:14:19 +00:00
}
}
}
// GET /projects/:id/repository/commits
async function getCommitMessages() {
logger.debug('getCommitMessages');
try {
const res = await get(`projects/${config.repoName}/repository/commits`);
return res.body.map(commit => commit.title);
} catch (err) {
logger.error({ err }, `getCommitMessages error`);
return [];
}
}