From 5e243d436114b8cfdee6393e45ee57e0900b9f1b Mon Sep 17 00:00:00 2001 From: = Date: Fri, 29 Sep 2023 10:56:40 +0530 Subject: [PATCH 1/2] fix(multiple-labels): considering the case if user has added some other labels while creating an issue --- utils/constants.ts | 1 - utils/webhook/github.handler.ts | 37 ++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/utils/constants.ts b/utils/constants.ts index 4004cd2..7d5fa0a 100644 --- a/utils/constants.ts +++ b/utils/constants.ts @@ -117,4 +117,3 @@ export const GENERAL = { } ] }; - diff --git a/utils/webhook/github.handler.ts b/utils/webhook/github.handler.ts index d076078..45e2dc1 100644 --- a/utils/webhook/github.handler.ts +++ b/utils/webhook/github.handler.ts @@ -340,6 +340,41 @@ export async function githubWebhookHandler( } ); + /** In case if user has added some other labels on issue */ + const githubLabels = issue.labels.filter( + label => label.name !== "linear" + ); + + const linearCreatedLabels: Array = []; + for (const githubLabel of githubLabels) { + const existingLinearLabel = await linear.issueLabels({ + includeArchived: true, + filter: { + name: { eqIgnoreCase: githubLabel.name }, + team: { id: { eq: linearTeamId } } + } + }); + + /** A label is already created with the same name */ + if (existingLinearLabel.nodes.length > 0) { + linearCreatedLabels.push(existingLinearLabel.nodes[0].id); + } + + /** If not, then create it */ + if (existingLinearLabel.nodes.length === 0) { + const createdLabel = await linear.issueLabelCreate({ + name: githubLabel.name, + description: githubLabel.description, + color: `#${githubLabel.color}`, + teamId: linearTeamId + }); + if (createdLabel.success) { + const fetchedLabel = await createdLabel.issueLabel; + linearCreatedLabels.push(fetchedLabel.id); + } + } + } + const assignee = await prisma.user.findFirst({ where: { githubUserId: issue.assignee?.id }, select: { linearUserId: true } @@ -350,7 +385,7 @@ export async function githubWebhookHandler( title: issue.title, description: `${modifiedDescription ?? ""}`, teamId: linearTeamId, - labelIds: [publicLabelId], + labelIds: [...linearCreatedLabels, publicLabelId], ...(issue.assignee?.id && assignee && { assigneeId: assignee.linearUserId From 9cb54965fc22ab404e7c3fc1fa58e7b9108b860f Mon Sep 17 00:00:00 2001 From: = Date: Sat, 30 Sep 2023 15:49:55 +0530 Subject: [PATCH 2/2] fix(multiple-labels): refactor code to meet requirements --- utils/webhook/github.handler.ts | 41 +++++++++++---------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/utils/webhook/github.handler.ts b/utils/webhook/github.handler.ts index 45e2dc1..d0c0d1f 100644 --- a/utils/webhook/github.handler.ts +++ b/utils/webhook/github.handler.ts @@ -345,35 +345,17 @@ export async function githubWebhookHandler( label => label.name !== "linear" ); - const linearCreatedLabels: Array = []; - for (const githubLabel of githubLabels) { - const existingLinearLabel = await linear.issueLabels({ - includeArchived: true, - filter: { - name: { eqIgnoreCase: githubLabel.name }, - team: { id: { eq: linearTeamId } } + const linearLabels = await linear.issueLabels({ + includeArchived: true, + filter: { + team: { id: { eq: linearTeamId } }, + name: { + in: githubLabels.map(label => + label.name.trim().toLowerCase() + ) } - }); - - /** A label is already created with the same name */ - if (existingLinearLabel.nodes.length > 0) { - linearCreatedLabels.push(existingLinearLabel.nodes[0].id); } - - /** If not, then create it */ - if (existingLinearLabel.nodes.length === 0) { - const createdLabel = await linear.issueLabelCreate({ - name: githubLabel.name, - description: githubLabel.description, - color: `#${githubLabel.color}`, - teamId: linearTeamId - }); - if (createdLabel.success) { - const fetchedLabel = await createdLabel.issueLabel; - linearCreatedLabels.push(fetchedLabel.id); - } - } - } + }); const assignee = await prisma.user.findFirst({ where: { githubUserId: issue.assignee?.id }, @@ -385,7 +367,10 @@ export async function githubWebhookHandler( title: issue.title, description: `${modifiedDescription ?? ""}`, teamId: linearTeamId, - labelIds: [...linearCreatedLabels, publicLabelId], + labelIds: [ + ...linearLabels?.nodes?.map(node => node.id), + publicLabelId + ], ...(issue.assignee?.id && assignee && { assigneeId: assignee.linearUserId