Skip to content

Commit

Permalink
chore: simplified deadline calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Aug 20, 2024
1 parent d0a00d8 commit 6fc8f09
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
17 changes: 12 additions & 5 deletions src/handlers/shared/generate-assignment-comment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context } from "../../types/context";
import { calculateDurations } from "../../utils/shared";

export const options: Intl.DateTimeFormatOptions = {
weekday: "short",
Expand All @@ -10,12 +11,18 @@ export const options: Intl.DateTimeFormatOptions = {
timeZoneName: "short",
};

export async function generateAssignmentComment(context: Context, issueCreatedAt: string, issueNumber: number, senderId: number, duration: number) {
export function getDeadline(issue: Context["payload"]["issue"]) {
if (!issue?.labels) {
throw new Error("No labels are set.");
}
const startTime = new Date().getTime();
const duration: number = calculateDurations(issue.labels).shift() ?? 0;
const endTime = new Date(startTime + duration * 1000);
return endTime.toLocaleString("en-US", options);
}

export async function generateAssignmentComment(context: Context, issueCreatedAt: string, issueNumber: number, senderId: number, deadline: string) {
const startTime = new Date().getTime();
let endTime: null | Date = null;
let deadline: null | string = null;
endTime = new Date(startTime + duration * 1000);
deadline = endTime.toLocaleString("en-US", options);

return {
daysElapsedSinceTaskCreation: Math.floor((startTime - new Date(issueCreatedAt).getTime()) / 1000 / 60 / 60 / 24),
Expand Down
11 changes: 5 additions & 6 deletions src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Assignee, Context, ISSUE_TYPE, Label, Sender } from "../../types";
import { isParentIssue, getAvailableOpenedPullRequests, getAssignedIssues, addAssignees, addCommentToIssue } from "../../utils/issue";
import { calculateDurations } from "../../utils/shared";
import { addAssignees, addCommentToIssue, getAssignedIssues, getAvailableOpenedPullRequests, isParentIssue } from "../../utils/issue";
import { Result } from "../proxy";
import { checkTaskStale } from "./check-task-stale";
import { hasUserBeenUnassigned } from "./check-assignments";
import { generateAssignmentComment } from "./generate-assignment-comment";
import { checkTaskStale } from "./check-task-stale";
import { generateAssignmentComment, getDeadline } from "./generate-assignment-comment";
import structuredMetadata from "./structured-metadata";
import { assignTableComment } from "./table";

Expand Down Expand Up @@ -92,9 +91,9 @@ export async function start(context: Context, issue: Context["payload"]["issue"]
throw new Error("No price label is set to calculate the duration");
}

const duration: number = calculateDurations(labels).shift() ?? 0;
const deadline = getDeadline(issue);

const assignmentComment = await generateAssignmentComment(context, issue.created_at, issue.number, sender.id, duration);
const assignmentComment = await generateAssignmentComment(context, issue.created_at, issue.number, sender.id, deadline);
const logMessage = logger.info("Task assigned successfully", {
taskDeadline: assignmentComment.deadline,
taskAssignees: [...assignees.map((a) => a?.login), sender.id],
Expand Down
8 changes: 2 additions & 6 deletions src/handlers/user-start-stop.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Context } from "../types";
import { addCommentToIssue } from "../utils/issue";
import { calculateDurations } from "../utils/shared";
import { Result } from "./proxy";
import { options } from "./shared/generate-assignment-comment";
import { getDeadline } from "./shared/generate-assignment-comment";
import { start } from "./shared/start";
import { stop } from "./shared/stop";

Expand Down Expand Up @@ -30,10 +29,7 @@ export async function userStartStop(context: Context): Promise<Result> {
export async function userSelfAssign(context: Context): Promise<Result> {
const { payload } = context;
const { issue } = payload;
const startTime = new Date().getTime();
const duration: number = calculateDurations(issue.labels).shift() ?? 0;
const endTime = new Date(startTime + duration * 1000);
const deadline = endTime.toLocaleString("en-US", options);
const deadline = getDeadline(issue);

const users = issue.assignees.map((user) => `@${user?.login}`).join(", ");

Expand Down

0 comments on commit 6fc8f09

Please sign in to comment.