From 910b15ea442f04ffe23f7c954d55b3e0518dcdb1 Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Mon, 9 Sep 2024 17:28:04 +0900 Subject: [PATCH] chore: user is properly assigned on PR opened --- .../shared/generate-assignment-comment.ts | 6 ++--- src/handlers/shared/start.ts | 9 +++++-- src/handlers/user-start-stop.ts | 27 ++++++++----------- src/utils/get-closing-issue-references.ts | 1 + src/utils/shared.ts | 8 +++--- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/handlers/shared/generate-assignment-comment.ts b/src/handlers/shared/generate-assignment-comment.ts index c8c3317a..fa03e8a5 100644 --- a/src/handlers/shared/generate-assignment-comment.ts +++ b/src/handlers/shared/generate-assignment-comment.ts @@ -11,12 +11,12 @@ export const options: Intl.DateTimeFormatOptions = { timeZoneName: "short", }; -export function getDeadline(issue: Context["payload"]["issue"]): string | null { - if (!issue?.labels) { +export function getDeadline(labels: Context<"issue_comment.created">["payload"]["issue"]["labels"] | undefined | null): string | null { + if (!labels?.length) { throw new Error("No labels are set."); } const startTime = new Date().getTime(); - const duration: number = calculateDurations(issue.labels).shift() ?? 0; + const duration: number = calculateDurations(labels).shift() ?? 0; if (!duration) return null; const endTime = new Date(startTime + duration * 1000); return endTime.toLocaleString("en-US", options); diff --git a/src/handlers/shared/start.ts b/src/handlers/shared/start.ts index 19a66494..fdfbcd3e 100644 --- a/src/handlers/shared/start.ts +++ b/src/handlers/shared/start.ts @@ -7,7 +7,12 @@ import { generateAssignmentComment, getDeadline } from "./generate-assignment-co import structuredMetadata from "./structured-metadata"; import { assignTableComment } from "./table"; -export async function start(context: Context, issue: Context["payload"]["issue"], sender: Context["payload"]["sender"], teammates: string[]): Promise { +export async function start( + context: Context, + issue: Context<"issue_comment.created">["payload"]["issue"], + sender: Context["payload"]["sender"], + teammates: string[] +): Promise { const { logger, config } = context; const { maxConcurrentTasks, taskStaleTimeoutDuration } = config; @@ -82,7 +87,7 @@ export async function start(context: Context, issue: Context["payload"]["issue"] throw new Error(logger.error("No price label is set to calculate the duration", { issueNumber: issue.number }).logMessage.raw); } - const deadline = getDeadline(issue); + const deadline = getDeadline(labels); const toAssignIds = await fetchUserIds(context, toAssign); const assignmentComment = await generateAssignmentComment(context, issue.created_at, issue.number, sender.id, deadline); diff --git a/src/handlers/user-start-stop.ts b/src/handlers/user-start-stop.ts index 830c0676..d65fcdf6 100644 --- a/src/handlers/user-start-stop.ts +++ b/src/handlers/user-start-stop.ts @@ -31,7 +31,7 @@ export async function userStartStop(context: Context): Promise { export async function userSelfAssign(context: Context<"issues.assigned">): Promise { const { payload } = context; const { issue } = payload; - const deadline = getDeadline(issue); + const deadline = getDeadline(issue.labels); if (!deadline) { context.logger.debug("Skipping deadline posting message because no deadline has been set."); @@ -53,27 +53,22 @@ export async function userPullRequest(context: Context<"pull_request.opened"> | repo, issue_number: pull_request.number, }); - console.log(linkedIssues); const issues = linkedIssues.repository.pullRequest?.closingIssuesReferences?.nodes; if (!issues) { context.logger.info("No linked issues were found, nothing to do."); return { status: HttpStatusCode.NOT_MODIFIED }; } for (const issue of issues) { - console.log(issue, pull_request.user); - if (!issue?.assignees.nodes?.includes((node) => node.id === pull_request.user?.id)) { - try { - const deadline = getDeadline(issue); - console.log(deadline); - if (!deadline) { - context.logger.debug("Skipping deadline posting message because no deadline has been set."); - return { status: HttpStatusCode.NOT_MODIFIED }; - } else { - console.log("assigning!"); - return await start(context, issue, payload.sender, []); - } - } catch (e) { - context.logger.error("Failed to assign the user to the issue.", { e }); + if (!issue?.assignees.nodes?.some((node) => node?.id.toString() === pull_request.user?.id.toString())) { + const deadline = getDeadline(issue?.labels?.nodes); + if (!deadline) { + context.logger.debug("Skipping deadline posting message because no deadline has been set."); + return { status: HttpStatusCode.NOT_MODIFIED }; + } else { + issue.assignees = issue.assignees.nodes; + issue.labels = issue.labels.nodes; + context.payload.issue = issue; + return await start(context, issue, payload.sender, []); } } } diff --git a/src/utils/get-closing-issue-references.ts b/src/utils/get-closing-issue-references.ts index a181df99..5d840e1f 100644 --- a/src/utils/get-closing-issue-references.ts +++ b/src/utils/get-closing-issue-references.ts @@ -7,6 +7,7 @@ export const QUERY_CLOSING_ISSUE_REFERENCES = /* GraphQL */ ` nodes { id url + number labels(first: 100) { nodes { id diff --git a/src/utils/shared.ts b/src/utils/shared.ts index 4b8a4152..6895e78b 100644 --- a/src/utils/shared.ts +++ b/src/utils/shared.ts @@ -1,12 +1,12 @@ import ms from "ms"; -import { Label } from "../types"; +import { Context } from "../types"; -export function calculateDurations(labels: Label[]): number[] { +export function calculateDurations(labels: Context<"issue_comment.created">["payload"]["issue"]["labels"]): number[] { // from shortest to longest const durations: number[] = []; - labels.forEach((label: Label) => { - const matches = label.name.match(/<(\d+)\s*(\w+)/); + labels.forEach((label) => { + const matches = label?.name.match(/<(\d+)\s*(\w+)/); if (matches && matches.length >= 3) { const number = parseInt(matches[1]); const unit = matches[2];