From 7bf22712209e6d211aa4dd1ac559dc122f21a748 Mon Sep 17 00:00:00 2001 From: Elad Ivanir Date: Sun, 25 Aug 2024 15:24:38 +0300 Subject: [PATCH] support multiple asana urls --- src/asana.ts | 26 +++++++++++++++----------- src/index.ts | 6 +++--- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/asana.ts b/src/asana.ts index f88c07d..c082ba4 100644 --- a/src/asana.ts +++ b/src/asana.ts @@ -15,27 +15,31 @@ export enum QaStatus { const asanaPat = core.getInput('asana-pat'); -export async function updatePrTaskStatus( +export async function updatePrTaskStatuses( prDescription: string, status: QaStatus ) { - const taskGid = extractTaskGid(prDescription); - await updateQaStatus(taskGid, status); + const taskIds = extractTaskIds(prDescription); + await Promise.all(taskIds.map(taskGid => updateQaStatus(taskGid, status))); } -function extractTaskGid(prDescription: string) { - const asanaUrlRegex = /https:\/\/app\.asana\.com\/0\/.*/; - const taskUrl = prDescription.match(asanaUrlRegex)?.[0]; +function extractTaskIds(prDescription: string) { + const asanaUrlRegex = /https:\/\/app\.asana\.com\/0\/.*/g; + const taskUrls = [...prDescription.matchAll(asanaUrlRegex)].map( + match => match[0] + ); - if (!taskUrl) { + if (!taskUrls) { throw new Error('Asana task URL not found in PR description'); } - const [_, __, taskGid] = [...taskUrl.matchAll(/\d+/g)].map( - match => match[0] - ); + return taskUrls.map(taskUrl => { + const [_, __, taskGid] = [...taskUrl.matchAll(/\d+/g)].map( + match => match[0] + ); - return taskGid; + return taskGid; + }); } function updateQaStatus(taskGid: string, status: QaStatus) { diff --git a/src/index.ts b/src/index.ts index 9a48376..f5e8ed4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import * as core from '@actions/core'; -import { QaStatus, updatePrTaskStatus } from './asana'; +import { QaStatus, updatePrTaskStatuses } from './asana'; import env from 'env-var'; import { @@ -11,7 +11,7 @@ import { async function handleSinglePr(status: QaStatus) { try { const description = getPrDescription(); - await updatePrTaskStatus(description, status); + await updatePrTaskStatuses(description, status); } catch (err: any) { console.log(`PR number ${getPrNumber()} failed. ${err.message}`); } @@ -22,7 +22,7 @@ async function handleInProd() { await Promise.all( descriptions.map(async ({ description, prNumber }) => { try { - await updatePrTaskStatus(description, QaStatus.Prod); + await updatePrTaskStatuses(description, QaStatus.Prod); } catch (err: any) { console.log( `PR number ${prNumber} failed. ${err.message}. PR description:\n ${description}`