diff --git a/src/handlers/shared/check-unassigns.ts b/src/handlers/shared/check-unassigns.ts new file mode 100644 index 00000000..7d2c3487 --- /dev/null +++ b/src/handlers/shared/check-unassigns.ts @@ -0,0 +1,20 @@ +import { Context } from "../../types"; + +export async function wasPreviouslyUnassigned( + context: Context, + senderLogin: Context["payload"]["sender"], + issueNumber: Context["payload"]["issue"] +): Promise { + try { + const issueEvents = await context.octokit.issues.listEventsForTimeline({ + owner: context.payload.repository.owner.login, + repo: context.payload.repository.name, + issue_number: issueNumber.id, + }); + + return issueEvents.data.some((event) => event.event === "unassigned" && event.event === senderLogin); + } catch (error) { + console.error("Error while checking previous assignment:", error); + return false; + } +} diff --git a/src/handlers/shared/start.ts b/src/handlers/shared/start.ts index 42c87bc0..e8aeeb50 100644 --- a/src/handlers/shared/start.ts +++ b/src/handlers/shared/start.ts @@ -2,6 +2,7 @@ import { Context, ISSUE_TYPE, Label } from "../../types"; import { isParentIssue, getAvailableOpenedPullRequests, getAssignedIssues, addAssignees, addCommentToIssue } from "../../utils/issue"; import { calculateDurations } from "../../utils/shared"; import { checkTaskStale } from "./check-task-stale"; +import { wasPreviouslyUnassigned } from "./check-unassigns"; import { generateAssignmentComment } from "./generate-assignment-comment"; import structuredMetadata from "./structured-metadata"; import { assignTableComment } from "./table"; @@ -66,12 +67,18 @@ export async function start(context: Context, issue: Context["payload"]["issue"] const labels = issue.labels; const priceLabel = labels.find((label: Label) => label.name.startsWith("Price: ")); + const isUnassigned = await wasPreviouslyUnassigned(context, sender, issue); if (!priceLabel) { await addCommentToIssue(context, "```diff\n! No price label is set to calculate the duration.\n```"); throw new Error("No price label is set to calculate the duration"); } + if (isUnassigned) { + await addCommentToIssue(context, "```diff\n! You were previously unassigned from this task. You cannot reassign yourself\n```"); + throw new Error(`This user was unassigned from this task previously. Cannot auto assign`); + } + const duration: number = calculateDurations(labels).shift() ?? 0; const { id, login } = sender;