From e8810b33fa3156329306f598db7995759f5d2640 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Thu, 19 Oct 2017 13:30:26 +0200
Subject: [PATCH] feat: add ensure comment removal api (github) (#992)

---
 lib/api/github.js       | 22 ++++++++++++++++++++++
 lib/api/gitlab.js       |  6 ++++++
 test/api/github.spec.js | 10 ++++++++++
 test/api/gitlab.spec.js |  5 +++++
 4 files changed, 43 insertions(+)

diff --git a/lib/api/github.js b/lib/api/github.js
index 8615d258bf..9e4cda7757 100644
--- a/lib/api/github.js
+++ b/lib/api/github.js
@@ -29,10 +29,13 @@ module.exports = {
   addAssignees,
   addReviewers,
   addLabels,
+  // Comments
   getComments,
   addComment,
   editComment,
+  deleteComment,
   ensureComment,
+  ensureCommentRemoval,
   // PR
   getPrList,
   findPr,
@@ -491,6 +494,11 @@ async function editComment(commentId, body) {
   });
 }
 
+async function deleteComment(commentId) {
+  // DELETE /repos/:owner/:repo/issues/comments/:id
+  await get.delete(`repos/${config.repoName}/issues/comments/${commentId}`);
+}
+
 async function ensureComment(issueNo, topic, content) {
   logger.debug(`Ensuring comment "${topic}" in #${issueNo}`);
   const body = `### ${topic}\n\n${content}`;
@@ -514,6 +522,20 @@ async function ensureComment(issueNo, topic, content) {
   }
 }
 
+async function ensureCommentRemoval(issueNo, topic) {
+  logger.debug(`Ensuring comment "${topic}" in #${issueNo} is removed`);
+  const comments = await getComments(issueNo);
+  let commentId;
+  comments.forEach(comment => {
+    if (comment.body.startsWith(`### ${topic}\n\n`)) {
+      commentId = comment.id;
+    }
+  });
+  if (commentId) {
+    await deleteComment(commentId);
+  }
+}
+
 // Pull Request
 
 async function getPrList() {
diff --git a/lib/api/gitlab.js b/lib/api/gitlab.js
index f30a6c1712..635af849a6 100644
--- a/lib/api/gitlab.js
+++ b/lib/api/gitlab.js
@@ -23,7 +23,9 @@ module.exports = {
   addAssignees,
   addReviewers,
   addLabels,
+  // Comments
   ensureComment,
+  ensureCommentRemoval,
   // PR
   findPr,
   createPr,
@@ -342,6 +344,10 @@ async function ensureComment() {
   // Todo: implement. See GitHub API for example
 }
 
+async function ensureCommentRemoval() {
+  // Todo: implement. See GitHub API for example
+}
+
 async function findPr(branchName, prTitle, state = 'all') {
   logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`);
   const urlString = `projects/${config.repoName}/merge_requests?state=${state}&per_page=100`;
diff --git a/test/api/github.spec.js b/test/api/github.spec.js
index 91f81948e7..5b0c646826 100644
--- a/test/api/github.spec.js
+++ b/test/api/github.spec.js
@@ -995,6 +995,16 @@ describe('api/github', () => {
       expect(get.patch.mock.calls).toHaveLength(0);
     });
   });
+  describe('ensureCommentRemoval', () => {
+    it('deletes comment if found', async () => {
+      await initRepo('some/repo', 'token');
+      get.mockReturnValueOnce({
+        body: [{ id: 1234, body: '### some-subject\n\nblablabla' }],
+      });
+      await github.ensureCommentRemoval(42, 'some-subject');
+      expect(get.delete.mock.calls).toHaveLength(1);
+    });
+  });
   describe('findPr(branchName, prTitle, state)', () => {
     it('returns true if no title and all state', async () => {
       get.mockReturnValueOnce({
diff --git a/test/api/gitlab.spec.js b/test/api/gitlab.spec.js
index 72d71f7f99..7921118cfa 100644
--- a/test/api/gitlab.spec.js
+++ b/test/api/gitlab.spec.js
@@ -493,6 +493,11 @@ describe('api/gitlab', () => {
       await gitlab.ensureComment(42, 'some-subject', 'some\ncontent');
     });
   });
+  describe('ensureCommentRemoval', () => {
+    it('exists', async () => {
+      await gitlab.ensureCommentRemoval(42, 'some-subject');
+    });
+  });
   describe('findPr(branchName, prTitle, state)', () => {
     it('returns null if no results', async () => {
       get.mockReturnValueOnce({
-- 
GitLab