From efa5edbdf333466176080533d7e664ecb2f628c8 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sun, 22 Jul 2018 09:07:31 +0200
Subject: [PATCH] fix(onboarding): delete onboarding PR branch if pr not found

---
 lib/workers/repository/onboarding/pr/index.js | 35 ++++++++++++++-----
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/lib/workers/repository/onboarding/pr/index.js b/lib/workers/repository/onboarding/pr/index.js
index ffe72f2cae..6bbbbeb1e5 100644
--- a/lib/workers/repository/onboarding/pr/index.js
+++ b/lib/workers/repository/onboarding/pr/index.js
@@ -1,3 +1,4 @@
+const is = require('@sindresorhus/is');
 const { getConfigDesc } = require('./config-description');
 const { getErrors, getWarnings } = require('./errors-warnings');
 const { getBaseBranchDesc } = require('./base-branch');
@@ -78,14 +79,32 @@ You can post questions in [our Config Help repository](https://github.com/renova
   logger.info('Creating onboarding PR');
   const labels = [];
   const useDefaultBranch = true;
-  const pr = await platform.createPr(
-    onboardingBranch,
-    onboardingPrTitle,
-    prBody,
-    labels,
-    useDefaultBranch
-  );
-  logger.info({ pr: pr.displayNumber }, 'Created onboarding PR');
+  try {
+    const pr = await platform.createPr(
+      onboardingBranch,
+      onboardingPrTitle,
+      prBody,
+      labels,
+      useDefaultBranch
+    );
+    logger.info({ pr: pr.displayNumber }, 'Created onboarding PR');
+  } catch (err) /* istanbul ignore next */ {
+    if (
+      err.statusCode === 422 &&
+      err.response &&
+      err.response.body &&
+      !is.empty(err.response.body.errors) &&
+      err.response.body.errors[0].message &&
+      err.response.body.errors[0].message.startsWith(
+        'A pull request already exists'
+      )
+    ) {
+      logger.info('Onboarding PR already exists but cannot find it');
+      await platform.deleteBranch(onboardingBranch);
+      return;
+    }
+    throw err;
+  }
 }
 
 module.exports = {
-- 
GitLab