mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-12 06:56:24 +00:00
feat: gitlab branch cleanup (#1475)
Removes the GitHub-only check when cleaning/pruning branches.
This commit is contained in:
parent
aa0b0d68fd
commit
720b46696a
5 changed files with 64 additions and 16 deletions
|
@ -68,7 +68,7 @@ async function getRepos(token, endpoint) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function urlEscape(str) {
|
function urlEscape(str) {
|
||||||
return str.replace(/\//g, '%2F');
|
return str ? str.replace(/\//g, '%2F') : str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize GitLab by getting base branch
|
// Initialize GitLab by getting base branch
|
||||||
|
@ -170,9 +170,17 @@ async function branchExists(branchName) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAllRenovateBranches() {
|
async function getAllRenovateBranches(branchPrefix) {
|
||||||
logger.warn('Unimplemented in GitLab: getAllRenovateBranches');
|
logger.debug(`getAllRenovateBranches(${branchPrefix})`);
|
||||||
return [];
|
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;
|
||||||
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isBranchStale() {
|
function isBranchStale() {
|
||||||
|
@ -290,7 +298,22 @@ async function setBranchStatus(
|
||||||
await get.post(url, { body: options });
|
await get.post(url, { body: options });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteBranch(branchName) {
|
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',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
await get.delete(
|
await get.delete(
|
||||||
`projects/${config.repository}/repository/branches/${urlEscape(branchName)}`
|
`projects/${config.repository}/repository/branches/${urlEscape(branchName)}`
|
||||||
);
|
);
|
||||||
|
|
|
@ -12,14 +12,12 @@ async function pruneStaleBranches(config) {
|
||||||
logger.debug('No branchList');
|
logger.debug('No branchList');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (config.platform !== 'github') {
|
|
||||||
// TODO: Implement for GitLab
|
|
||||||
logger.debug('Platform is not GitHub - returning');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let renovateBranches = await platform.getAllRenovateBranches(
|
let renovateBranches = await platform.getAllRenovateBranches(
|
||||||
config.branchPrefix
|
config.branchPrefix
|
||||||
);
|
);
|
||||||
|
if (!(renovateBranches && renovateBranches.length)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
logger.debug({ branchList, renovateBranches });
|
logger.debug({ branchList, renovateBranches });
|
||||||
const lockFileBranch = `${config.branchPrefix}lock-file-maintenance`;
|
const lockFileBranch = `${config.branchPrefix}lock-file-maintenance`;
|
||||||
if (renovateBranches.includes(lockFileBranch)) {
|
if (renovateBranches.includes(lockFileBranch)) {
|
||||||
|
@ -48,6 +46,7 @@ async function pruneStaleBranches(config) {
|
||||||
logger.info({ prNo: pr.number, prTitle: pr.title }, 'Autoclosing PR');
|
logger.info({ prNo: pr.number, prTitle: pr.title }, 'Autoclosing PR');
|
||||||
await platform.updatePr(pr.number, `${pr.title} - autoclosed`);
|
await platform.updatePr(pr.number, `${pr.title} - autoclosed`);
|
||||||
}
|
}
|
||||||
await platform.deleteBranch(branchName);
|
const closePr = true;
|
||||||
|
await platform.deleteBranch(branchName, closePr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,6 +70,13 @@ Array [
|
||||||
]
|
]
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`platform/gitlab getAllRenovateBranches() should return all renovate branches 1`] = `
|
||||||
|
Array [
|
||||||
|
"renovate/a",
|
||||||
|
"renovate/b",
|
||||||
|
]
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`platform/gitlab getBranchLastCommitTime should return a Date 1`] = `2012-09-20T08:50:22.000Z`;
|
exports[`platform/gitlab getBranchLastCommitTime should return a Date 1`] = `2012-09-20T08:50:22.000Z`;
|
||||||
|
|
||||||
exports[`platform/gitlab getBranchPr(branchName) should return null if no PR exists 1`] = `
|
exports[`platform/gitlab getBranchPr(branchName) should return null if no PR exists 1`] = `
|
||||||
|
|
|
@ -219,8 +219,22 @@ describe('platform/gitlab', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('getAllRenovateBranches()', () => {
|
describe('getAllRenovateBranches()', () => {
|
||||||
it('exists', () => {
|
it('should return all renovate branches', async () => {
|
||||||
gitlab.getAllRenovateBranches();
|
get.mockImplementationOnce(() => ({
|
||||||
|
body: [
|
||||||
|
{
|
||||||
|
name: 'renovate/a',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'master',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'renovate/b',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}));
|
||||||
|
const res = await gitlab.getAllRenovateBranches('renovate/');
|
||||||
|
expect(res).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('isBranchStale()', () => {
|
describe('isBranchStale()', () => {
|
||||||
|
@ -377,6 +391,12 @@ describe('platform/gitlab', () => {
|
||||||
await gitlab.deleteBranch('some-branch');
|
await gitlab.deleteBranch('some-branch');
|
||||||
expect(get.delete.mock.calls.length).toBe(1);
|
expect(get.delete.mock.calls.length).toBe(1);
|
||||||
});
|
});
|
||||||
|
it('should close PR', async () => {
|
||||||
|
get.delete = jest.fn();
|
||||||
|
get.mockReturnValueOnce({ body: [] }); // getBranchPr
|
||||||
|
await gitlab.deleteBranch('some-branch', true);
|
||||||
|
expect(get.delete.mock.calls.length).toBe(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
describe('mergeBranch()', () => {
|
describe('mergeBranch()', () => {
|
||||||
it('exists', () => {
|
it('exists', () => {
|
||||||
|
|
|
@ -16,11 +16,10 @@ describe('workers/repository/cleanup', () => {
|
||||||
await cleanup.pruneStaleBranches(config, config.branchList);
|
await cleanup.pruneStaleBranches(config, config.branchList);
|
||||||
expect(platform.getAllRenovateBranches.mock.calls).toHaveLength(0);
|
expect(platform.getAllRenovateBranches.mock.calls).toHaveLength(0);
|
||||||
});
|
});
|
||||||
it('returns if config is not github', async () => {
|
it('returns if no renovate branches', async () => {
|
||||||
config.branchList = [];
|
config.branchList = [];
|
||||||
config.platform = 'gitlab';
|
platform.getAllRenovateBranches.mockReturnValueOnce([]);
|
||||||
await cleanup.pruneStaleBranches(config, config.branchList);
|
await cleanup.pruneStaleBranches(config, config.branchList);
|
||||||
expect(platform.getAllRenovateBranches.mock.calls).toHaveLength(0);
|
|
||||||
});
|
});
|
||||||
it('returns if no remaining branches', async () => {
|
it('returns if no remaining branches', async () => {
|
||||||
config.branchList = ['renovate/a', 'renovate/b'];
|
config.branchList = ['renovate/a', 'renovate/b'];
|
||||||
|
|
Loading…
Reference in a new issue