From 66062d946eeffe8edd4f8e426daec564afac08ec Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Thu, 24 Jan 2019 06:30:00 +0100 Subject: [PATCH] fix(github): handle comment failure --- lib/platform/github/index.js | 92 ++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js index 305461f2a5..0c32a62c13 100644 --- a/lib/platform/github/index.js +++ b/lib/platform/github/index.js @@ -811,52 +811,64 @@ async function deleteComment(commentId) { } async function ensureComment(issueNo, topic, content) { - const comments = await getComments(issueNo); - let body; - let commentId; - let commentNeedsUpdating; - if (topic) { - logger.debug(`Ensuring comment "${topic}" in #${issueNo}`); - body = `### ${topic}\n\n${content}`; + try { + const comments = await getComments(issueNo); + let body; + let commentId; + let commentNeedsUpdating; + if (topic) { + logger.debug(`Ensuring comment "${topic}" in #${issueNo}`); + body = `### ${topic}\n\n${content}`; + comments.forEach(comment => { + if (comment.body.startsWith(`### ${topic}\n\n`)) { + commentId = comment.id; + commentNeedsUpdating = comment.body !== body; + } + }); + } else { + logger.debug(`Ensuring content-only comment in #${issueNo}`); + body = `${content}`; + comments.forEach(comment => { + if (comment.body === body) { + commentId = comment.id; + commentNeedsUpdating = false; + } + }); + } + if (!commentId) { + await addComment(issueNo, body); + logger.info({ repository: config.repository, issueNo }, 'Comment added'); + } else if (commentNeedsUpdating) { + await editComment(commentId, body); + logger.info( + { repository: config.repository, issueNo }, + 'Comment updated' + ); + } else { + logger.debug('Comment is already update-to-date'); + } + return true; + } catch (err) /* istanbul ignore next */ { + logger.warn({ err }, 'Error ensuring comment'); + return false; + } +} + +async function ensureCommentRemoval(issueNo, topic) { + try { + 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; - commentNeedsUpdating = comment.body !== body; - } - }); - } else { - logger.debug(`Ensuring content-only comment in #${issueNo}`); - body = `${content}`; - comments.forEach(comment => { - if (comment.body === body) { - commentId = comment.id; - commentNeedsUpdating = false; } }); - } - if (!commentId) { - await addComment(issueNo, body); - logger.info({ repository: config.repository, issueNo }, 'Comment added'); - } else if (commentNeedsUpdating) { - await editComment(commentId, body); - logger.info({ repository: config.repository, issueNo }, 'Comment updated'); - } else { - logger.debug('Comment is already update-to-date'); - } - return true; -} - -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); } - }); - if (commentId) { - await deleteComment(commentId); + } catch (err) /* istanbul ignore next */ { + logger.warn({ err }, 'Error ensuring comment removal'); } } -- GitLab