diff --git a/lib/api/github.js b/lib/api/github.js index 8615d258bfc7adc3b6a9e18e5100fab18c3a7a52..9e4cda775714b7452ed6a793a8bb7212d90c9ab8 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 f30a6c17120560e15f695c7a220ede83d049674c..635af849a660ffd94c06d5b0d41e85f15a923d7e 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 91f81948e789d9b0d583e4506a7230c588293847..5b0c6468261bc0e41e455b5131a63e4575087efa 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 72d71f7f99f9762617bea661df219511757de69b..7921118cfa8640c6612eb6531c17e22fbfae4675 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({