From 180e68fd43e440932b7ff6029c306aa33c3eb571 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Thu, 5 Jul 2018 14:05:29 +0200
Subject: [PATCH] feat(github): delete duplicate issues when ensuring issue

---
 lib/platform/github/index.js       | 30 +++++++++++++++++++-----------
 test/platform/github/index.spec.js | 17 +++++++++++++++++
 2 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js
index 3cfc485241..e27e4dbe89 100644
--- a/lib/platform/github/index.js
+++ b/lib/platform/github/index.js
@@ -609,13 +609,18 @@ async function ensureIssue(title, body) {
   logger.debug(`ensureIssue()`);
   try {
     const issueList = await getIssueList();
-    const issue = issueList.find(i => i.title === title);
-    if (issue) {
+    const issues = issueList.filter(i => i.title === title).reverse();
+    if (issues.length) {
+      // Close any duplicates
+      for (const issue of issues.slice(1)) {
+        await closeIssue(issue.number);
+      }
+      const [issue] = issues;
       const issueBody = (await get(
         `repos/${config.parentRepo || config.repository}/issues/${issue.number}`
       )).body.body;
       if (issueBody !== body) {
-        logger.debug('Updating issue body');
+        logger.info('Updating issue body');
         await get.patch(
           `repos/${config.parentRepo || config.repository}/issues/${
             issue.number
@@ -627,6 +632,7 @@ async function ensureIssue(title, body) {
         return 'updated';
       }
     } else {
+      logger.info('Creating issue');
       await get.post(`repos/${config.parentRepo || config.repository}/issues`, {
         body: {
           title,
@@ -647,20 +653,22 @@ async function ensureIssue(title, body) {
   return null;
 }
 
+async function closeIssue(issueNumber) {
+  await get.patch(
+    `repos/${config.parentRepo || config.repository}/issues/${issueNumber}`,
+    {
+      body: { state: 'closed' },
+    }
+  );
+}
+
 async function ensureIssueClosing(title) {
   logger.debug(`ensureIssueClosing()`);
   const issueList = await getIssueList();
   for (const issue of issueList) {
     if (issue.title === title) {
       logger.info({ issue }, 'Closing issue');
-      await get.patch(
-        `repos/${config.parentRepo || config.repository}/issues/${
-          issue.number
-        }`,
-        {
-          body: { state: 'closed' },
-        }
-      );
+      await closeIssue(issue.number);
     }
   }
 }
diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js
index 888442d477..7a238f598c 100644
--- a/test/platform/github/index.spec.js
+++ b/test/platform/github/index.spec.js
@@ -973,6 +973,23 @@ describe('platform/github', () => {
       const res = await github.ensureIssue('title-2', 'newer-content');
       expect(res).toBe(null);
     });
+    it('deletes if duplicate', async () => {
+      get.mockReturnValueOnce({
+        body: [
+          {
+            number: 1,
+            title: 'title-1',
+          },
+          {
+            number: 2,
+            title: 'title-1',
+          },
+        ],
+      });
+      get.mockReturnValueOnce({ body: { body: 'newer-content' } });
+      const res = await github.ensureIssue('title-1', 'newer-content');
+      expect(res).toBe(null);
+    });
   });
   describe('ensureIssueClosing()', () => {
     it('closes issue', async () => {
-- 
GitLab