Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(multiple-labels): considering the case if user has added some other labels while creating an issue #136

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,3 @@ export const GENERAL = {
}
]
};

37 changes: 36 additions & 1 deletion utils/webhook/github.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = [];
for (const githubLabel of githubLabels) {
rockingrohit9639 marked this conversation as resolved.
Show resolved Hide resolved
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) {
rockingrohit9639 marked this conversation as resolved.
Show resolved Hide resolved
linearCreatedLabels.push(existingLinearLabel.nodes[0].id);
}

/** If not, then create it */
if (existingLinearLabel.nodes.length === 0) {
rockingrohit9639 marked this conversation as resolved.
Show resolved Hide resolved
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 }
Expand All @@ -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
Expand Down