From 5b0f801eec272a7976bdea347d82ff71c3d845d7 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 18 Feb 2019 10:39:55 +0100
Subject: [PATCH] fix(bitbucket-server): catch empty pull requests

---
 lib/platform/bitbucket-server/index.js | 31 ++++++++++++++++++++------
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/lib/platform/bitbucket-server/index.js b/lib/platform/bitbucket-server/index.js
index a4af93f758..c4fe76884f 100644
--- a/lib/platform/bitbucket-server/index.js
+++ b/lib/platform/bitbucket-server/index.js
@@ -490,13 +490,30 @@ async function createPr(
       id: `refs/heads/${base}`,
     },
   };
-
-  const prInfoRes = await api.post(
-    `/rest/api/1.0/projects/${config.projectKey}/repos/${
-      config.repositorySlug
-    }/pull-requests`,
-    { body }
-  );
+  let prInfoRes;
+  try {
+    prInfoRes = await api.post(
+      `/rest/api/1.0/projects/${config.projectKey}/repos/${
+        config.repositorySlug
+      }/pull-requests`,
+      { body }
+    );
+  } catch (err) /* istanbul ignore next */ {
+    if (
+      err.body &&
+      err.body.errors &&
+      err.body.errors.length &&
+      err.body.errors[0].exceptionName ===
+        'com.atlassian.bitbucket.pull.EmptyPullRequestException'
+    ) {
+      logger.info(
+        'Empty pull request - deleting branch so it can be recreated next run'
+      );
+      await deleteBranch(branchName);
+      throw new Error('repository-changed');
+    }
+    throw err;
+  }
 
   const pr = {
     id: prInfoRes.body.id,
-- 
GitLab