diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index 9c94affb2b78cbf16a843c85458b145810737328..70af0bcd5924ce7a6bd39cfe08e9929714168526 100644 --- a/lib/platform/gitlab/index.js +++ b/lib/platform/gitlab/index.js @@ -501,10 +501,7 @@ async function commitFilesToBranch( `commitFilesToBranch('${branchName}', files, message, '${parentBranch})'` ); if (branchName !== parentBranch) { - const isBranchExisting = await branchExists(branchName); - if (isBranchExisting) { - logger.debug(`Branch ${branchName} already exists`); - } else { + try { logger.debug(`Creating branch ${branchName}`); const opts = { body: { @@ -513,12 +510,12 @@ async function commitFilesToBranch( }, }; await get.post(`projects/${config.repository}/repository/branches`, opts); + } catch (err) { + logger.info({ err }, 'Branch could not be created - already exists?'); } } for (const file of files) { - const existingFile = await getFile(file.name, branchName); - if (existingFile) { - logger.debug(`${file.name} exists - updating it`); + try { await updateFile( config.repository, branchName, @@ -526,8 +523,8 @@ async function commitFilesToBranch( file.contents, message ); - } else { - logger.debug(`Creating file ${file.name}`); + } catch (err) { + logger.debug({ err }, 'Cannot update file - trying to create it'); await createFile( config.repository, branchName, diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js index 23fb264a62ee9be8ea1d994068e4b34170cf009f..3cbf5c1a049b843bec1d94f1734dd0b710251a1a 100644 --- a/test/platform/gitlab/index.spec.js +++ b/test/platform/gitlab/index.spec.js @@ -1,6 +1,7 @@ describe('platform/gitlab', () => { let gitlab; let get; + let helpers; beforeEach(() => { // clean up env delete process.env.GITLAB_TOKEN; @@ -12,6 +13,7 @@ describe('platform/gitlab', () => { jest.mock('../../../lib/platform/gitlab/helpers'); gitlab = require('../../../lib/platform/gitlab'); get = require('../../../lib/platform/gitlab/gl-got-wrapper'); + helpers = require('../../../lib/platform/gitlab/helpers'); }); describe('getRepos', () => { @@ -621,40 +623,32 @@ describe('platform/gitlab', () => { }); }); describe('commitFilesToBranch(branchName, files, message, parentBranch)', () => { - it('creates branch', async () => { - get.mockReturnValueOnce({ statusCode: 404 }); - await gitlab.commitFilesToBranch('some-branch', [], 'some-message'); - }); - it('does not create branch and updates file', async () => { - get.mockReturnValueOnce({ statusCode: 200 }); - get.mockReturnValueOnce({ - body: { - content: 'hello', - }, - }); + it('creates branch and updates file', async () => { const file = { - name: 'foo', - contents: 'bar', + name: 'some-file', + contents: 'some contents', }; await gitlab.commitFilesToBranch( - 'some-branch', + 'renovate/something', [file], - 'some-message', - 'parent-branch' + 'Update something' ); }); - it('does not create branch and creates file', async () => { - get.mockReturnValueOnce({ statusCode: 200 }); - get.mockReturnValueOnce(Promise.reject({ statusCode: 404 })); + it('updates branch and creates file', async () => { const file = { - name: 'foo', - contents: 'bar', + name: 'renovate.json', + contents: '{}', }; + get.post.mockImplementationOnce(() => { + throw new Error('branch exists'); + }); // create branch + helpers.updateFile.mockImplementationOnce(() => { + throw new Error('file does not exist'); + }); // update file await gitlab.commitFilesToBranch( - 'some-branch', + 'renovate/configure', [file], - 'some-message', - 'parent-branch' + 'Add renovate.json' ); }); });