Skip to content

Commit

Permalink
fix(multiple-labels): refactor code to meet requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
rockingrohit9639 committed Sep 30, 2023
1 parent 5e243d4 commit 9cb5496
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions utils/webhook/github.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,35 +345,17 @@ export async function githubWebhookHandler(
label => label.name !== "linear"
);

const linearCreatedLabels: Array<string> = [];
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 },
Expand All @@ -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
Expand Down

0 comments on commit 9cb5496

Please sign in to comment.