From aa0b0d68fddd5340c92f2b058d51adbdd3502c44 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 3 Feb 2018 11:04:27 +0100
Subject: [PATCH] =?UTF-8?q?fix:=20don=E2=80=99t=20check=20for=20branch=20e?=
 =?UTF-8?q?xisting=20when=20rebasing=20(gitlab)=20(#1474)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Previously, our GitLab API library was checking if a a branch existed first before trying to create it. But due to caching, a branch we'd deleted ourselves still showed up as existing, so then there was no branch to update the files in. Skip this check and use try/catch for creating branch instead.

Fixes #1468
---
 lib/platform/gitlab/index.js       | 15 +++++------
 test/platform/gitlab/index.spec.js | 42 +++++++++++++-----------------
 2 files changed, 24 insertions(+), 33 deletions(-)

diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js
index 9c94affb2b..70af0bcd59 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 23fb264a62..3cbf5c1a04 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'
       );
     });
   });
-- 
GitLab