diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index 224a3e52dafe9f9117769a65a448692f39016839..37d740beb6df65fefd6cccb36dc371d8d6c590eb 100644 --- a/lib/platform/gitlab/index.js +++ b/lib/platform/gitlab/index.js @@ -538,6 +538,9 @@ async function commitFilesToBranch( action.action = (await getFile(file.name)) ? 'update' : 'create'; opts.body.actions.push(action); } + if (await branchExists(branchName)) { + await deleteBranch(branchName); + } await get.post(`projects/${config.repository}/repository/commits`, opts); } diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js index 7e837bdfcd8cfaf4df22c7c416649fdfb295c1db..8d5e3e0b8b9e413a1dd22c4b762c1fbd2bd9b130 100644 --- a/test/platform/gitlab/index.spec.js +++ b/test/platform/gitlab/index.spec.js @@ -641,7 +641,12 @@ describe('platform/gitlab', () => { }); describe('commitFilesToBranch(branchName, files, message, parentBranch)', () => { it('creates file', async () => { - get.mockImplementationOnce(() => Promise.reject({ statusCode: 404 })); + get.mockImplementationOnce(() => Promise.reject({ statusCode: 404 })); // file exists + get.mockImplementationOnce(() => + Promise.reject({ + statusCode: 404, + }) + ); // branch exists const file = { name: 'some-new-file', contents: 'some new-contents', @@ -655,6 +660,7 @@ describe('platform/gitlab', () => { expect(get.post.mock.calls).toHaveLength(1); }); it('updates multiple files', async () => { + // Two files exist get.mockReturnValueOnce({ body: { content: 'foo', @@ -665,6 +671,8 @@ describe('platform/gitlab', () => { content: 'foo', }, }); + // branch exists + get.mockImplementationOnce(() => ({ statusCode: 200 })); const files = [ { name: 'some-existing-file',