From d3dbd937c449bc7c8501e2f3378b6e87f9b90c11 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Thu, 2 May 2019 23:34:52 +0200
Subject: [PATCH] fix(gitlab): better debugging of pr.canRebase

---
 lib/platform/gitlab/index.js                    | 17 ++++++++++++-----
 .../gitlab/__snapshots__/index.spec.js.snap     |  1 +
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js
index fa30840c2e..891cb81d9d 100644
--- a/lib/platform/gitlab/index.js
+++ b/lib/platform/gitlab/index.js
@@ -122,6 +122,7 @@ async function initRepo({ repository, token, endpoint, gitFs, localDir }) {
     logger.debug(`${repository} default branch = ${config.baseBranch}`);
     // Discover our user email
     config.email = (await get(`user`)).body.email;
+    logger.debug('Bot email=' + config.email);
     delete config.prList;
     // istanbul ignore if
     if (config.gitFs) {
@@ -697,14 +698,20 @@ async function getPr(iid) {
   }/repository/branches/${urlEscape(pr.source_branch)}`;
   try {
     const branch = (await get(branchUrl)).body;
-    if (
-      branch &&
-      branch.commit &&
-      branch.commit.author_email === config.email
-    ) {
+    const branchCommitEmail =
+      branch && branch.commit ? branch.commit.author_email : null;
+    // istanbul ignore if
+    if (branchCommitEmail === config.email) {
       pr.canRebase = true;
+    } else {
+      logger.debug(
+        { branchCommitEmail, configEmail: config.email, iid: pr.iid },
+        'Last committer to branch does not match bot email, so PR cannot be rebased.'
+      );
+      pr.canRebase = false;
     }
   } catch (err) {
+    logger.debug({ err }, 'Error getting PR branch');
     if (pr.state === 'open' || err.statusCode !== 404) {
       logger.warn({ err }, 'Error getting PR branch');
       pr.isConflicted = true;
diff --git a/test/platform/gitlab/__snapshots__/index.spec.js.snap b/test/platform/gitlab/__snapshots__/index.spec.js.snap
index 971da13226..b9fdacb003 100644
--- a/test/platform/gitlab/__snapshots__/index.spec.js.snap
+++ b/test/platform/gitlab/__snapshots__/index.spec.js.snap
@@ -187,6 +187,7 @@ Object {
   "body": undefined,
   "branchName": "some-branch",
   "canMerge": true,
+  "canRebase": false,
   "commits": 1,
   "deletions": 1,
   "displayNumber": "Merge Request #91",
-- 
GitLab