2018-08-29 05:30:03 +00:00
|
|
|
const parseDiff = require('parse-diff');
|
|
|
|
const api = require('./bb-got-wrapper');
|
|
|
|
const utils = require('./utils');
|
2018-09-12 10:16:17 +00:00
|
|
|
const hostRules = require('../../util/host-rules');
|
2018-08-29 05:30:03 +00:00
|
|
|
|
|
|
|
let config = {};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
// Initialization
|
|
|
|
getRepos,
|
|
|
|
cleanRepo,
|
|
|
|
initRepo,
|
2018-09-10 11:56:53 +00:00
|
|
|
getRepoStatus: () => ({}),
|
2018-08-29 05:30:03 +00:00
|
|
|
getRepoForceRebase,
|
|
|
|
setBaseBranch,
|
|
|
|
// Search
|
|
|
|
getFileList,
|
|
|
|
// Branch
|
|
|
|
branchExists,
|
|
|
|
getAllRenovateBranches,
|
|
|
|
isBranchStale,
|
|
|
|
getBranchPr,
|
|
|
|
getBranchStatus,
|
|
|
|
getBranchStatusCheck,
|
|
|
|
setBranchStatus,
|
|
|
|
deleteBranch,
|
|
|
|
mergeBranch,
|
|
|
|
getBranchLastCommitTime,
|
2018-09-14 10:51:33 +00:00
|
|
|
// Issue
|
2018-10-03 13:47:03 +00:00
|
|
|
findIssue,
|
2018-08-29 05:30:03 +00:00
|
|
|
ensureIssue,
|
|
|
|
ensureIssueClosing,
|
|
|
|
addAssignees,
|
|
|
|
addReviewers,
|
2018-09-14 10:51:33 +00:00
|
|
|
deleteLabel,
|
2018-08-29 05:30:03 +00:00
|
|
|
// Comments
|
|
|
|
ensureComment,
|
|
|
|
ensureCommentRemoval,
|
|
|
|
// PR
|
|
|
|
getPrList,
|
|
|
|
findPr,
|
|
|
|
createPr,
|
|
|
|
getPr,
|
|
|
|
getPrFiles,
|
|
|
|
updatePr,
|
|
|
|
mergePr,
|
|
|
|
getPrBody,
|
|
|
|
// file
|
|
|
|
commitFilesToBranch,
|
|
|
|
getFile,
|
|
|
|
// Commits
|
|
|
|
getCommitMessages,
|
|
|
|
// vulnerability alerts
|
|
|
|
getVulnerabilityAlerts,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get all repositories that the user has access to
|
|
|
|
async function getRepos(token, endpoint) {
|
|
|
|
logger.debug('getRepos(token, endpoint)');
|
2018-09-12 10:16:17 +00:00
|
|
|
const opts = hostRules.find({ platform: 'bitbucket' }, { token, endpoint });
|
2018-08-29 05:30:03 +00:00
|
|
|
// istanbul ignore next
|
|
|
|
if (!opts.token) {
|
|
|
|
throw new Error('No token found for getRepos');
|
|
|
|
}
|
2018-09-12 10:16:17 +00:00
|
|
|
hostRules.update({ ...opts, platform: 'bitbucket', default: true });
|
2018-08-29 05:30:03 +00:00
|
|
|
try {
|
|
|
|
const repos = await utils.accumulateValues(
|
|
|
|
`/2.0/repositories/?role=contributor`
|
|
|
|
);
|
|
|
|
return repos.map(repo => repo.full_name);
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
logger.error({ err }, `bitbucket getRepos error`);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize bitbucket by getting base branch and SHA
|
|
|
|
async function initRepo({ repository, token, endpoint }) {
|
|
|
|
logger.debug(`initRepo("${repository}")`);
|
2018-09-12 10:16:17 +00:00
|
|
|
const opts = hostRules.find({ platform: 'bitbucket' }, { token, endpoint });
|
2018-08-29 05:30:03 +00:00
|
|
|
// istanbul ignore next
|
|
|
|
if (!opts.token) {
|
|
|
|
throw new Error(`No token found for Bitbucket repository ${repository}`);
|
|
|
|
}
|
2018-09-12 10:16:17 +00:00
|
|
|
hostRules.update({ ...opts, platform: 'bitbucket', default: true });
|
2018-08-29 05:30:03 +00:00
|
|
|
api.reset();
|
|
|
|
config = {};
|
|
|
|
// TODO: get in touch with @rarkins about lifting up the caching into the app layer
|
|
|
|
config.repository = repository;
|
|
|
|
const platformConfig = {};
|
|
|
|
try {
|
|
|
|
const info = utils.repoInfoTransformer(
|
|
|
|
(await api.get(`/2.0/repositories/${repository}`)).body
|
|
|
|
);
|
|
|
|
platformConfig.privateRepo = info.privateRepo;
|
|
|
|
platformConfig.isFork = info.isFork;
|
|
|
|
platformConfig.repoFullName = info.repoFullName;
|
|
|
|
config.owner = info.owner;
|
|
|
|
logger.debug(`${repository} owner = ${config.owner}`);
|
|
|
|
config.defaultBranch = info.mainbranch;
|
|
|
|
config.baseBranch = config.defaultBranch;
|
|
|
|
config.mergeMethod = info.mergeMethod;
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
if (err.statusCode === 404) {
|
|
|
|
throw new Error('not-found');
|
|
|
|
}
|
|
|
|
logger.info({ err }, 'Unknown Bitbucket initRepo error');
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
delete config.prList;
|
|
|
|
delete config.fileList;
|
|
|
|
await Promise.all([getPrList(), getFileList()]);
|
|
|
|
return platformConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if repository has rule enforcing PRs are up-to-date with base branch before merging
|
|
|
|
function getRepoForceRebase() {
|
|
|
|
// BB doesnt have an option to flag staled branches
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setBaseBranch(branchName) {
|
|
|
|
if (branchName) {
|
|
|
|
logger.debug(`Setting baseBranch to ${branchName}`);
|
|
|
|
config.baseBranch = branchName;
|
|
|
|
delete config.baseCommitSHA;
|
|
|
|
delete config.fileList;
|
|
|
|
await getFileList(branchName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search
|
|
|
|
|
|
|
|
// Get full file list
|
|
|
|
async function getFileList(branchName) {
|
|
|
|
const branch = branchName || config.baseBranch;
|
|
|
|
config.fileList = config.fileList || {};
|
|
|
|
if (config.fileList[branch]) {
|
|
|
|
return config.fileList[branch];
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const branchSha = await getBranchCommit(branch);
|
|
|
|
const filesRaw = await utils.files(
|
|
|
|
`/2.0/repositories/${config.repository}/src/${branchSha}/`
|
|
|
|
);
|
|
|
|
config.fileList[branch] = filesRaw.map(file => file.path);
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
logger.info(
|
|
|
|
{ repository: config.repository },
|
|
|
|
'Error retrieving git tree - no files detected'
|
|
|
|
);
|
|
|
|
config.fileList[branch] = [];
|
|
|
|
}
|
|
|
|
return config.fileList[branch];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Branch
|
|
|
|
|
|
|
|
// Returns true if branch exists, otherwise false
|
|
|
|
async function branchExists(branchName) {
|
|
|
|
logger.debug(`branchExists(${branchName})`);
|
|
|
|
try {
|
|
|
|
const { name } = (await api.get(
|
|
|
|
`/2.0/repositories/${config.repository}/refs/branches/${branchName}`
|
|
|
|
)).body;
|
|
|
|
return name === branchName;
|
|
|
|
} catch (err) {
|
|
|
|
if (err.statusCode === 404) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// istanbul ignore next
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO rewrite mutating reduce to filter in other adapters
|
|
|
|
async function getAllRenovateBranches(branchPrefix) {
|
|
|
|
logger.trace('getAllRenovateBranches');
|
|
|
|
const allBranches = await utils.accumulateValues(
|
|
|
|
`/2.0/repositories/${config.repository}/refs/branches`
|
|
|
|
);
|
|
|
|
return allBranches
|
|
|
|
.map(branch => branch.name)
|
|
|
|
.filter(name => name.startsWith(branchPrefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if branch's parent SHA = master SHA
|
|
|
|
async function isBranchStale(branchName) {
|
|
|
|
logger.debug(`isBranchStale(${branchName})`);
|
|
|
|
const [branch, baseBranch] = (await Promise.all([
|
|
|
|
api.get(
|
|
|
|
`/2.0/repositories/${config.repository}/refs/branches/${branchName}`
|
|
|
|
),
|
|
|
|
api.get(
|
|
|
|
`/2.0/repositories/${config.repository}/refs/branches/${
|
|
|
|
config.baseBranch
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
])).map(res => res.body);
|
|
|
|
|
|
|
|
const branchParentCommit = branch.target.parents[0].hash;
|
|
|
|
const baseBranchLatestCommit = baseBranch.target.hash;
|
|
|
|
|
|
|
|
return branchParentCommit !== baseBranchLatestCommit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the Pull Request for a branch. Null if not exists.
|
|
|
|
async function getBranchPr(branchName) {
|
|
|
|
logger.debug(`getBranchPr(${branchName})`);
|
|
|
|
const existingPr = await findPr(branchName, null, 'open');
|
|
|
|
return existingPr ? getPr(existingPr.number) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
logger.debug('Status checks disabled = returning "success"');
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
if (requiredStatusChecks.length) {
|
|
|
|
// This is Unsupported
|
|
|
|
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
|
|
|
|
return 'failed';
|
|
|
|
}
|
|
|
|
const sha = await getBranchCommit(branchName);
|
|
|
|
const statuses = await utils.accumulateValues(
|
|
|
|
`/2.0/repositories/${config.repository}/commit/${sha}/statuses`
|
|
|
|
);
|
|
|
|
const noOfFailures = statuses.filter(status => status.state === 'FAILED')
|
|
|
|
.length;
|
2018-09-24 09:47:49 +00:00
|
|
|
logger.debug(
|
|
|
|
{ branch: branchName, sha, statuses },
|
|
|
|
'branch status check result'
|
|
|
|
);
|
2018-08-29 05:30:03 +00:00
|
|
|
if (noOfFailures) {
|
|
|
|
return 'failed';
|
|
|
|
}
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getBranchStatusCheck(branchName, context) {
|
|
|
|
const sha = await getBranchCommit(branchName);
|
|
|
|
const statuses = await utils.accumulateValues(
|
|
|
|
`/2.0/repositories/${config.repository}/commit/${sha}/statuses`
|
|
|
|
);
|
|
|
|
const bbState = (statuses.find(status => status.key === context) || {}).state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
Object.keys(utils.buildStates).find(
|
|
|
|
stateKey => utils.buildStates[stateKey] === bbState
|
|
|
|
) || null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setBranchStatus(
|
|
|
|
branchName,
|
|
|
|
context,
|
|
|
|
description,
|
|
|
|
state,
|
|
|
|
targetUrl
|
|
|
|
) {
|
|
|
|
const sha = await getBranchCommit(branchName);
|
|
|
|
|
|
|
|
// TargetUrl can not be empty so default to bitbucket
|
|
|
|
const url = targetUrl || 'http://bitbucket.org';
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
name: context,
|
|
|
|
state: utils.buildStates[state],
|
|
|
|
key: context,
|
|
|
|
description,
|
|
|
|
url,
|
|
|
|
};
|
|
|
|
|
|
|
|
await api.post(
|
|
|
|
`/2.0/repositories/${config.repository}/commit/${sha}/statuses/build`,
|
|
|
|
{ body }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:18:06 +00:00
|
|
|
async function deleteBranch(branchName, closePr = false) {
|
|
|
|
// istanbul ignore if
|
|
|
|
if (closePr) {
|
|
|
|
logger.debug('Closing PR');
|
|
|
|
const pr = await getBranchPr(branchName);
|
|
|
|
if (pr) {
|
|
|
|
await api.post(
|
|
|
|
`/2.0/repositories/${config.repository}/pullrequests/${
|
|
|
|
pr.number
|
|
|
|
}/decline`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-08-29 05:30:03 +00:00
|
|
|
return api.delete(
|
|
|
|
`/2.0/repositories/${config.repository}/refs/branches/${branchName}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergeBranch() {
|
|
|
|
// The api does not support merging branches, so any automerge must be done via PR
|
|
|
|
return Promise.reject(new Error('Branch automerge not supported'));
|
|
|
|
}
|
|
|
|
|
2018-10-29 16:07:50 +00:00
|
|
|
async function findOpenIssues(title) {
|
|
|
|
try {
|
|
|
|
const currentUser = (await api.get('/2.0/user')).body.username;
|
|
|
|
const filter = encodeURIComponent(
|
|
|
|
[
|
|
|
|
`title=${JSON.stringify(title)}`,
|
|
|
|
'(state = "new" OR state = "open")',
|
|
|
|
`reporter.username="${currentUser}"`,
|
|
|
|
].join(' AND ')
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
(await api.get(
|
|
|
|
`/2.0/repositories/${config.repository}/issues?q=${filter}`
|
|
|
|
)).body.values || []
|
|
|
|
);
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
logger.warn('Error finding issues');
|
|
|
|
return [];
|
|
|
|
}
|
2018-10-03 13:47:03 +00:00
|
|
|
}
|
2018-10-29 16:07:50 +00:00
|
|
|
|
|
|
|
async function findIssue(title) {
|
|
|
|
logger.debug(`findIssue(${title})`);
|
|
|
|
const issues = await findOpenIssues(title);
|
|
|
|
if (!issues.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const [issue] = issues;
|
|
|
|
return {
|
|
|
|
number: issue.id,
|
|
|
|
body: issue.content && issue.content.raw,
|
|
|
|
};
|
2018-08-29 05:30:03 +00:00
|
|
|
}
|
2018-10-29 16:07:50 +00:00
|
|
|
|
|
|
|
async function closeIssue(issueNumber) {
|
|
|
|
await api.put(
|
|
|
|
`/2.0/repositories/${config.repository}/issues/${issueNumber}`,
|
|
|
|
{
|
|
|
|
body: { state: 'closed' },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function ensureIssue(title, body) {
|
|
|
|
logger.debug(`ensureIssue()`);
|
|
|
|
try {
|
|
|
|
const issues = await findOpenIssues(title);
|
|
|
|
if (issues.length) {
|
|
|
|
// Close any duplicates
|
|
|
|
for (const issue of issues.slice(1)) {
|
|
|
|
await closeIssue(issue.id);
|
|
|
|
}
|
|
|
|
const [issue] = issues;
|
|
|
|
if (String(issue.content.raw).trim() !== body.trim()) {
|
|
|
|
logger.info('Issue updated');
|
|
|
|
await api.put(
|
|
|
|
`/2.0/repositories/${config.repository}/issues/${issue.id}`,
|
|
|
|
{
|
|
|
|
body: { content: { raw: body, markup: 'markdown' } },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return 'updated';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.info('Issue created');
|
|
|
|
await api.post(`/2.0/repositories/${config.repository}/issues`, {
|
|
|
|
body: {
|
|
|
|
title,
|
|
|
|
content: { raw: body, markup: 'markdown' },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return 'created';
|
|
|
|
}
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
if (err.message.startsWith('Repository has no issue tracker.')) {
|
|
|
|
logger.info(
|
|
|
|
`Issues are disabled, so could not create issue: ${err.message}`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
logger.warn({ err }, 'Could not ensure issue');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function ensureIssueClosing(title) {
|
|
|
|
const issues = await findOpenIssues(title);
|
|
|
|
for (const issue of issues) {
|
|
|
|
await closeIssue(issue.id);
|
|
|
|
}
|
2018-08-29 05:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getBranchLastCommitTime(branchName) {
|
|
|
|
const branches = await utils.accumulateValues(
|
|
|
|
`/2.0/repositories/${config.repository}/refs/branches`
|
|
|
|
);
|
|
|
|
const branch = branches.find(br => br.name === branchName) || {
|
|
|
|
target: {},
|
|
|
|
};
|
|
|
|
return branch.target.date ? new Date(branch.target.date) : new Date();
|
|
|
|
}
|
|
|
|
|
|
|
|
function addAssignees() {
|
|
|
|
// Bitbucket supports "participants" and "reviewers" so does not seem to have the concept of "assignee"
|
|
|
|
logger.warn('Cannot add assignees');
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
function addReviewers() {
|
|
|
|
// TODO
|
|
|
|
logger.warn('Cannot add reviewers');
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2018-09-14 10:51:33 +00:00
|
|
|
// istanbul ignore next
|
|
|
|
function deleteLabel() {
|
|
|
|
throw new Error('deleteLabel not implemented');
|
|
|
|
}
|
|
|
|
|
2018-08-29 05:30:03 +00:00
|
|
|
function ensureComment() {
|
|
|
|
// https://developer.atlassian.com/bitbucket/api/2/reference/search?q=pullrequest+comment
|
|
|
|
logger.warn('Comment functionality not implemented yet');
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureCommentRemoval() {
|
|
|
|
// The api does not support removing comments
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const isRelevantPr = (branchName, prTitle, states) => p =>
|
|
|
|
p.branchName === branchName &&
|
|
|
|
(!prTitle || p.title === prTitle) &&
|
|
|
|
states.includes(p.state);
|
|
|
|
|
|
|
|
async function findPr(branchName, prTitle, inputStates = utils.prStates.all) {
|
|
|
|
let states;
|
|
|
|
// istanbul ignore if
|
|
|
|
if (inputStates === '!open') {
|
|
|
|
states = utils.prStates.notOpen;
|
|
|
|
} else {
|
|
|
|
states = inputStates;
|
|
|
|
}
|
|
|
|
logger.debug(`findPr(${branchName}, ${prTitle}, ${states})`);
|
|
|
|
const prList = await getPrList();
|
|
|
|
const pr = prList.find(isRelevantPr(branchName, prTitle, states));
|
|
|
|
if (pr) {
|
|
|
|
logger.debug(`Found PR #${pr.number}`);
|
|
|
|
}
|
|
|
|
return pr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates PR and returns PR number
|
|
|
|
async function createPr(
|
|
|
|
branchName,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
labels,
|
|
|
|
useDefaultBranch = true
|
|
|
|
) {
|
|
|
|
// labels is not supported in Bitbucket: https://bitbucket.org/site/master/issues/11976/ability-to-add-labels-to-pull-requests-bb
|
|
|
|
|
|
|
|
const base = useDefaultBranch ? config.defaultBranch : config.baseBranch;
|
|
|
|
|
|
|
|
logger.debug({ repository: config.repository, title, base }, 'Creating PR');
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
source: {
|
|
|
|
branch: {
|
|
|
|
name: branchName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
destination: {
|
|
|
|
branch: {
|
|
|
|
name: base,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
close_source_branch: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const prInfo = (await api.post(
|
|
|
|
`/2.0/repositories/${config.repository}/pullrequests`,
|
|
|
|
{ body }
|
|
|
|
)).body;
|
2018-10-03 13:08:34 +00:00
|
|
|
const pr = { id: prInfo.id, displayNumber: `Pull Request #${prInfo.id}` };
|
|
|
|
// istanbul ignore if
|
|
|
|
if (config.prList) {
|
|
|
|
config.prList.push(pr);
|
|
|
|
}
|
|
|
|
return pr;
|
2018-08-29 05:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function isPrConflicted(prNo) {
|
|
|
|
const diff = (await api.get(
|
|
|
|
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/diff`,
|
|
|
|
{ json: false }
|
|
|
|
)).body;
|
|
|
|
|
|
|
|
return utils.isConflicted(parseDiff(diff));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets details for a PR
|
|
|
|
async function getPr(prNo) {
|
|
|
|
const pr = (await api.get(
|
|
|
|
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`
|
|
|
|
)).body;
|
|
|
|
|
|
|
|
// istanbul ignore if
|
|
|
|
if (!pr) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = {
|
|
|
|
displayNumber: `Pull Request #${pr.id}`,
|
|
|
|
...utils.prInfo(pr),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (utils.prStates.open.includes(pr.state)) {
|
2018-09-14 18:02:51 +00:00
|
|
|
res.isConflicted = await isPrConflicted(prNo);
|
2018-08-29 05:30:03 +00:00
|
|
|
const commits = await utils.accumulateValues(pr.links.commits.href);
|
|
|
|
if (commits.length === 1) {
|
|
|
|
res.canRebase = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.isStale = await isBranchStale(pr.source.branch.name);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a list of all modified files in a PR
|
|
|
|
async function getPrFiles(prNo) {
|
|
|
|
logger.debug({ prNo }, 'getPrFiles');
|
|
|
|
const diff = (await api.get(
|
|
|
|
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/diff`,
|
|
|
|
{ json: false }
|
|
|
|
)).body;
|
|
|
|
const files = parseDiff(diff).map(file => file.to);
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updatePr(prNo, title, description) {
|
|
|
|
logger.debug(`updatePr(${prNo}, ${title}, body)`);
|
|
|
|
await api.put(`/2.0/repositories/${config.repository}/pullrequests/${prNo}`, {
|
|
|
|
body: { title, description },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function mergePr(prNo, branchName) {
|
|
|
|
logger.debug(`mergePr(${prNo}, ${branchName})`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await api.post(
|
|
|
|
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/merge`,
|
|
|
|
{
|
|
|
|
body: {
|
|
|
|
close_source_branch: true,
|
|
|
|
merge_strategy: 'merge_commit',
|
|
|
|
message: 'auto merged',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
delete config.baseCommitSHA;
|
|
|
|
logger.info('Automerging succeeded');
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPrBody(input) {
|
|
|
|
// Remove any HTML we use
|
2018-09-21 09:48:13 +00:00
|
|
|
return input
|
|
|
|
.replace(/<\/?summary>/g, '**')
|
|
|
|
.replace(/<\/?details>/g, '')
|
2018-12-05 06:21:48 +00:00
|
|
|
.replace(/\n---\n\n.*?<!-- renovate-rebase -->.*?\n/, '')
|
2018-09-21 09:48:13 +00:00
|
|
|
.substring(0, 50000);
|
2018-08-29 05:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the commit SHA for a branch
|
|
|
|
async function getBranchCommit(branchName) {
|
|
|
|
try {
|
|
|
|
const branch = (await api.get(
|
|
|
|
`/2.0/repositories/${config.repository}/refs/branches/${branchName}`
|
|
|
|
)).body;
|
|
|
|
return branch.target.hash;
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
logger.debug({ err }, `getBranchCommit('${branchName}') failed'`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new commit, create branch if not existing
|
|
|
|
async function commitFilesToBranch(
|
|
|
|
branchName,
|
|
|
|
files,
|
|
|
|
message,
|
|
|
|
parentBranch = config.baseBranch
|
|
|
|
) {
|
|
|
|
logger.debug(
|
|
|
|
`commitFilesToBranch('${branchName}', files, message, '${parentBranch})'`
|
|
|
|
);
|
|
|
|
if (branchName !== parentBranch && (await branchExists(branchName))) {
|
|
|
|
logger.debug('Deleting existing branch');
|
|
|
|
await deleteBranch(branchName);
|
|
|
|
delete config.fileList[branchName];
|
|
|
|
}
|
|
|
|
const parents = await getBranchCommit(parentBranch);
|
|
|
|
|
|
|
|
const form = utils.commitForm({
|
|
|
|
message,
|
|
|
|
gitAuthor: config.gitAuthor,
|
|
|
|
parents,
|
|
|
|
branchName,
|
|
|
|
files,
|
|
|
|
});
|
|
|
|
|
|
|
|
await api.post(`/2.0/repositories/${config.repository}/src`, {
|
|
|
|
json: false,
|
|
|
|
body: form,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic File operations
|
|
|
|
async function getFile(filePath, branchName) {
|
|
|
|
logger.debug(`getFile(filePath=${filePath}, branchName=${branchName})`);
|
|
|
|
if (!branchName || branchName === config.baseBranch) {
|
|
|
|
const fileList = await getFileList(branchName);
|
|
|
|
if (!fileList.includes(filePath)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const branchSha = await getBranchCommit(branchName || config.baseBranch);
|
|
|
|
const file = (await api.get(
|
|
|
|
`/2.0/repositories/${config.repository}/src/${branchSha}/${filePath}`,
|
|
|
|
{ json: false }
|
|
|
|
)).body;
|
|
|
|
return file;
|
|
|
|
} catch (err) {
|
|
|
|
if (err.statusCode === 404) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getCommitMessages() {
|
|
|
|
logger.debug('getCommitMessages');
|
|
|
|
const values = await utils.accumulateValues(
|
|
|
|
`/2.0/repositories/${config.repository}/commits`
|
|
|
|
);
|
|
|
|
return values.map(commit => commit.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull Request
|
|
|
|
|
|
|
|
async function getPrList(state = utils.prStates.open) {
|
|
|
|
logger.debug('getPrList()');
|
|
|
|
if (!config.prList) {
|
|
|
|
logger.debug('Retrieving PR list');
|
|
|
|
const prs = await utils.accumulateValues(
|
|
|
|
`/2.0/repositories/${config.repository}/pullrequests?state=${state}`,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
50
|
|
|
|
);
|
|
|
|
config.prList = prs.map(utils.prInfo);
|
|
|
|
logger.info({ length: config.prList.length }, 'Retrieved Pull Requests');
|
|
|
|
}
|
|
|
|
return config.prList;
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanRepo() {
|
|
|
|
api.reset();
|
|
|
|
config = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVulnerabilityAlerts() {
|
|
|
|
return [];
|
|
|
|
}
|