diff --git a/lib/platform/gitlab/helpers.js b/lib/platform/gitlab/helpers.js deleted file mode 100644 index a267bdae609345a16939fd33279e32a33635cec8..0000000000000000000000000000000000000000 --- a/lib/platform/gitlab/helpers.js +++ /dev/null @@ -1,49 +0,0 @@ -const get = require('./gl-got-wrapper'); - -module.exports = { - createFile, - updateFile, -}; - -async function createFile( - repository, - branchName, - filePath, - fileContents, - message -) { - const opts = {}; - const url = `projects/${repository}/repository/files/${filePath.replace( - /\//g, - '%2F' - )}`; - opts.body = { - branch: branchName, - commit_message: message, - encoding: 'base64', - content: Buffer.from(fileContents).toString('base64'), - }; - await get.post(url, opts); -} - -async function updateFile( - repository, - branchName, - filePath, - fileContents, - message -) { - const opts = {}; - const url = `projects/${repository}/repository/files/${filePath.replace( - /\//g, - '%2F' - )}`; - opts.body = { - branch: branchName, - commit_message: message, - encoding: 'base64', - content: Buffer.from(fileContents).toString('base64'), - }; - - await get.put(url, opts); -} diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index aa0f37c8396011cfc5d2919c9bbfbe00993fcd7e..224a3e52dafe9f9117769a65a448692f39016839 100644 --- a/lib/platform/gitlab/index.js +++ b/lib/platform/gitlab/index.js @@ -1,7 +1,5 @@ const get = require('./gl-got-wrapper'); -const { createFile, updateFile } = require('./helpers'); - let config = {}; module.exports = { @@ -523,40 +521,24 @@ async function commitFilesToBranch( logger.debug( `commitFilesToBranch('${branchName}', files, message, '${parentBranch})'` ); - if (branchName !== parentBranch) { - try { - logger.debug(`Creating branch ${branchName}`); - const opts = { - body: { - branch: branchName, - ref: config.baseBranch, - }, - }; - await get.post(`projects/${config.repository}/repository/branches`, opts); - } catch (err) { - logger.info({ err }, 'Branch could not be created - already exists?'); - } - } + const opts = { + body: { + branch: branchName, + commit_message: message, + start_branch: parentBranch, + actions: [], + }, + }; for (const file of files) { - try { - await updateFile( - config.repository, - branchName, - file.name, - file.contents, - message - ); - } catch (err) { - logger.debug({ err }, 'Cannot update file - trying to create it'); - await createFile( - config.repository, - branchName, - file.name, - file.contents, - message - ); - } + const action = { + file_path: file.name, + content: Buffer.from(file.contents).toString('base64'), + encoding: 'base64', + }; + action.action = (await getFile(file.name)) ? 'update' : 'create'; + opts.body.actions.push(action); } + await get.post(`projects/${config.repository}/repository/commits`, opts); } // GET /projects/:id/repository/commits diff --git a/test/platform/gitlab/__snapshots__/index.spec.js.snap b/test/platform/gitlab/__snapshots__/index.spec.js.snap index ee4878063d6c36b121c20f53717ccdf1effe278a..a640f6e4ac36534f62007ea19c676d88e45ac190 100644 --- a/test/platform/gitlab/__snapshots__/index.spec.js.snap +++ b/test/platform/gitlab/__snapshots__/index.spec.js.snap @@ -16,6 +16,58 @@ Array [ ] `; +exports[`platform/gitlab commitFilesToBranch(branchName, files, message, parentBranch) creates file 1`] = ` +Array [ + Array [ + "projects/undefined/repository/commits", + Object { + "body": Object { + "actions": Array [ + Object { + "action": "create", + "content": "c29tZSBuZXctY29udGVudHM=", + "encoding": "base64", + "file_path": "some-new-file", + }, + ], + "branch": "renovate/something", + "commit_message": "Create something", + "start_branch": undefined, + }, + }, + ], +] +`; + +exports[`platform/gitlab commitFilesToBranch(branchName, files, message, parentBranch) updates multiple files 1`] = ` +Array [ + Array [ + "projects/undefined/repository/commits", + Object { + "body": Object { + "actions": Array [ + Object { + "action": "update", + "content": "dXBkYXRlZCBjb250ZW50", + "encoding": "base64", + "file_path": "some-existing-file", + }, + Object { + "action": "update", + "content": "b3RoZXIgdXBkYXRlZCBjb250ZW50", + "encoding": "base64", + "file_path": "some-other-existing-file", + }, + ], + "branch": "renovate/something", + "commit_message": "Update something", + "start_branch": undefined, + }, + }, + ], +] +`; + exports[`platform/gitlab createPr(branchName, title, body) returns the PR 1`] = ` Object { "displayNumber": "Merge Request #12345", diff --git a/test/platform/gitlab/helpers.spec.js b/test/platform/gitlab/helpers.spec.js deleted file mode 100644 index 624f265a3e11e35340f09c46225326dd6a97fce1..0000000000000000000000000000000000000000 --- a/test/platform/gitlab/helpers.spec.js +++ /dev/null @@ -1,26 +0,0 @@ -const helpers = require('../../../lib/platform/gitlab/helpers'); - -describe('platform/gitlab/helpers', () => { - describe('createFile(branchName, filePath, fileContents, message)', () => { - it('creates file', async () => { - await helpers.createFile( - 'some%2Frepo', - 'some-branch', - 'some-path', - 'some-contents', - 'some-message' - ); - }); - }); - describe('updateFile(branchName, filePath, fileContents, message)', () => { - it('updates file', async () => { - await helpers.updateFile( - 'some%2Frepo', - 'some-branch', - 'some-path', - 'some-contents', - 'some-message' - ); - }); - }); -}); diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js index 83b7ff7c0597f85ed2d82ee44993861909cd669c..7e837bdfcd8cfaf4df22c7c416649fdfb295c1db 100644 --- a/test/platform/gitlab/index.spec.js +++ b/test/platform/gitlab/index.spec.js @@ -1,7 +1,6 @@ describe('platform/gitlab', () => { let gitlab; let get; - let helpers; beforeEach(() => { // clean up env delete process.env.GITLAB_TOKEN; @@ -10,10 +9,8 @@ describe('platform/gitlab', () => { // reset module jest.resetModules(); jest.mock('../../../lib/platform/gitlab/gl-got-wrapper'); - 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', () => { @@ -643,33 +640,48 @@ describe('platform/gitlab', () => { }); }); describe('commitFilesToBranch(branchName, files, message, parentBranch)', () => { - it('creates branch and updates file', async () => { + it('creates file', async () => { + get.mockImplementationOnce(() => Promise.reject({ statusCode: 404 })); const file = { - name: 'some-file', - contents: 'some contents', + name: 'some-new-file', + contents: 'some new-contents', }; await gitlab.commitFilesToBranch( 'renovate/something', [file], - 'Update something' + 'Create something' ); + expect(get.post.mock.calls).toMatchSnapshot(); + expect(get.post.mock.calls).toHaveLength(1); }); - it('updates branch and creates file', async () => { - const file = { - 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 + it('updates multiple files', async () => { + get.mockReturnValueOnce({ + body: { + content: 'foo', + }, + }); + get.mockReturnValueOnce({ + body: { + content: 'foo', + }, + }); + const files = [ + { + name: 'some-existing-file', + contents: 'updated content', + }, + { + name: 'some-other-existing-file', + contents: 'other updated content', + }, + ]; await gitlab.commitFilesToBranch( - 'renovate/configure', - [file], - 'Add renovate.json' + 'renovate/something', + files, + 'Update something' ); + expect(get.post.mock.calls).toMatchSnapshot(); + expect(get.post.mock.calls).toHaveLength(1); }); }); describe('getCommitMessages()', () => {