Skip to content

Commit

Permalink
chore: user is properly assigned on PR opened
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Sep 9, 2024
1 parent 944c7a4 commit 910b15e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/handlers/shared/generate-assignment-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result> {
export async function start(
context: Context,
issue: Context<"issue_comment.created">["payload"]["issue"],
sender: Context["payload"]["sender"],
teammates: string[]
): Promise<Result> {
const { logger, config } = context;
const { maxConcurrentTasks, taskStaleTimeoutDuration } = config;

Expand Down Expand Up @@ -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);
Expand Down
27 changes: 11 additions & 16 deletions src/handlers/user-start-stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function userStartStop(context: Context): Promise<Result> {
export async function userSelfAssign(context: Context<"issues.assigned">): Promise<Result> {
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.");
Expand All @@ -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, []);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/get-closing-issue-references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const QUERY_CLOSING_ISSUE_REFERENCES = /* GraphQL */ `
nodes {
id
url
number
labels(first: 100) {
nodes {
id
Expand Down
8 changes: 4 additions & 4 deletions src/utils/shared.ts
Original file line number Diff line number Diff line change
@@ -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];
Expand Down

0 comments on commit 910b15e

Please sign in to comment.