From a47526c51e0448b1fc16fe59e7bc601442545a76 Mon Sep 17 00:00:00 2001 From: Sergi Serrano Casalins Date: Wed, 19 May 2021 12:39:39 +0200 Subject: [PATCH 1/2] Prevent duplicate comments --- index.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 1a1ffc77..5b43e149 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,11 @@ const core = require('@actions/core'); const github = require('@actions/github'); const asana = require('asana'); +async function commentAlreadySent(client, taskId, taskComment) { + const stories = await client.tasks.stories(taskId); + return stories.data.some(s => s.type === 'comment' && s.text === taskComment); +} + async function asanaOperations( asanaPAT, targets, @@ -15,7 +20,7 @@ async function asanaOperations( }).useAccessToken(asanaPAT); const task = await client.tasks.findById(taskId); - + targets.forEach(async target => { let targetProject = task.projects.find(project => project.name === target.project); if (targetProject) { @@ -33,10 +38,13 @@ async function asanaOperations( }); if (taskComment) { - await client.tasks.addComment(taskId, { - text: taskComment - }); - core.info('Added the pull request link to the Asana task.'); + const shouldSend = !(await commentAlreadySent(client, taskId, taskComment)); + if (shouldSend) { + await client.tasks.addComment(taskId, { + text: taskComment + }); + core.info('Added the pull request link to the Asana task.'); + } } } catch (ex) { console.error(ex.value); From 66e22994fd7b674529e21ac8c6db013a8044cf5a Mon Sep 17 00:00:00 2001 From: Sergi Serrano Casalins Date: Wed, 19 May 2021 13:13:01 +0200 Subject: [PATCH 2/2] add log output when comment already exists --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5b43e149..2198f1a9 100644 --- a/index.js +++ b/index.js @@ -38,8 +38,10 @@ async function asanaOperations( }); if (taskComment) { - const shouldSend = !(await commentAlreadySent(client, taskId, taskComment)); - if (shouldSend) { + const shouldSkip = await commentAlreadySent(client, taskId, taskComment); + if (shouldSkip) { + core.info('Pull request link was already added.') + } else { await client.tasks.addComment(taskId, { text: taskComment });