Skip to content

Commit

Permalink
feat: user self assign message display
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Aug 20, 2024
1 parent 30ca585 commit c8a018d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 18 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Start | Stop",
"description": "Assign or un-assign yourself from an issue.",
"ubiquity:listeners": ["issue_comment.created"],
"ubiquity:listeners": ["issue_comment.created", "issue.assigned"],
"commands": {
"start": {
"ubiquity:example": "/start",
Expand Down
25 changes: 25 additions & 0 deletions src/handlers/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Context, Env, SupportedEventsU } from "../types";
import { userSelfAssign, userStartStop } from "./user-start-stop";

export interface Result {
status: "ok" | "failed" | "skipped";
content?: string;
reason?: string;
}

const callbacks: { [k in SupportedEventsU]: (context: Context, env: Env) => Result | Promise<Result> } = {
"issue.assigned": userSelfAssign,
"issue_comment.created": userStartStop,
};

export function proxyCallbacks({ logger }: Context) {
return new Proxy(callbacks, {
get(target, prop: SupportedEventsU) {
if (!(prop in target)) {
logger.error(`${prop} is not supported, skipping.`);
return async () => ({ status: "skipped", reason: "unsupported_event" });
}
return target[prop].bind(target);
},
});
}
2 changes: 1 addition & 1 deletion src/handlers/shared/generate-assignment-comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from "../../types/context";

const options: Intl.DateTimeFormatOptions = {
export const options: Intl.DateTimeFormatOptions = {
weekday: "short",
month: "short",
day: "numeric",
Expand Down
5 changes: 3 additions & 2 deletions src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Assignee, Context, ISSUE_TYPE, Label, Sender } from "../../types";
import { isParentIssue, getAvailableOpenedPullRequests, getAssignedIssues, addAssignees, addCommentToIssue } from "../../utils/issue";
import { calculateDurations } from "../../utils/shared";
import { Result } from "../proxy";
import { checkTaskStale } from "./check-task-stale";
import { hasUserBeenUnassigned } from "./check-assignments";
import { generateAssignmentComment } from "./generate-assignment-comment";
import structuredMetadata from "./structured-metadata";
import { assignTableComment } from "./table";

export async function start(context: Context, issue: Context["payload"]["issue"], sender: Sender) {
export async function start(context: Context, issue: Context["payload"]["issue"], sender: Sender): Promise<Result> {
const { logger, config } = context;
const { maxConcurrentTasks } = config.miscellaneous;
const { taskStaleTimeoutDuration } = config.timers;
Expand Down Expand Up @@ -123,5 +124,5 @@ export async function start(context: Context, issue: Context["payload"]["issue"]
].join("\n") as string
);

return { output: "Task assigned successfully" };
return { content: "Task assigned successfully", status: "ok" };
}
7 changes: 4 additions & 3 deletions src/handlers/shared/stop.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Assignee, Context, Sender } from "../../types";
import { addCommentToIssue, closePullRequestForAnIssue } from "../../utils/issue";
import { Result } from "../proxy";

export async function stop(context: Context, issue: Context["payload"]["issue"], sender: Sender, repo: Context["payload"]["repository"]) {
export async function stop(context: Context, issue: Context["payload"]["issue"], sender: Sender, repo: Context["payload"]["repository"]): Promise<Result> {
const { logger } = context;
const issueNumber = issue.number;

Expand All @@ -13,7 +14,7 @@ export async function stop(context: Context, issue: Context["payload"]["issue"],
if (!userToUnassign) {
const log = logger.error("You are not assigned to this task", { issueNumber, user: sender.login });
await addCommentToIssue(context, log?.logMessage.diff as string);
return { output: "You are not assigned to this task" };
return { content: "You are not assigned to this task", status: "ok" };
}

// close PR
Expand All @@ -39,5 +40,5 @@ export async function stop(context: Context, issue: Context["payload"]["issue"],
});

await addCommentToIssue(context, unassignedLog?.logMessage.diff as string);
return { output: "Task unassigned successfully" };
return { content: "Task unassigned successfully", status: "ok" };
}
31 changes: 27 additions & 4 deletions src/handlers/user-start-stop.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
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 { start } from "./shared/start";
import { stop } from "./shared/stop";

export async function userStartStop(context: Context): Promise<{ output: string | null }> {
function getUser(payload: Context["payload"]) {
const { comment, sender } = payload;
return comment.user?.login ? { login: comment.user.login, id: comment.user.id } : { login: sender.login, id: sender.id };
}

export async function userStartStop(context: Context): Promise<Result> {
const { payload } = context;
const { issue, comment, sender, repository } = payload;
const { issue, comment, repository } = payload;
const slashCommand = comment.body.split(" ")[0].replace("/", "");

const user = comment.user?.login ? { login: comment.user.login, id: comment.user.id } : { login: sender.login, id: sender.id };
const user = getUser(context.payload);

if (slashCommand === "stop") {
return await stop(context, issue, user, repository);
} else if (slashCommand === "start") {
return await start(context, issue, user);
}

return { output: null };
return { status: "skipped" };
}

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 users = issue.assignees.map((user) => `@${user?.login}`).join(", ");

await addCommentToIssue(context, `${users} the deadline is at ${deadline}`);
return { status: "ok" };
}
8 changes: 2 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Octokit } from "@octokit/rest";
import { createClient } from "@supabase/supabase-js";
import { Logs } from "@ubiquity-dao/ubiquibot-logger";
import { createAdapters } from "./adapters";
import { userStartStop } from "./handlers/user-start-stop";
import { proxyCallbacks } from "./handlers/proxy";
import { Context, Env, PluginInputs } from "./types";

export async function startStopTask(inputs: PluginInputs, env: Env) {
Expand All @@ -21,9 +21,5 @@ export async function startStopTask(inputs: PluginInputs, env: Env) {

context.adapters = createAdapters(supabase, context);

if (context.eventName === "issue_comment.created") {
await userStartStop(context);
} else {
context.logger.error(`Unsupported event: ${context.eventName}`);
}
return proxyCallbacks(context)[inputs.eventName](context, env);
}
2 changes: 1 addition & 1 deletion src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createAdapters } from "../adapters";
import { Env } from "./env";
import { Logs } from "@ubiquity-dao/ubiquibot-logger";

export type SupportedEventsU = "issue_comment.created";
export type SupportedEventsU = "issue_comment.created" | "issue.assigned";

export type SupportedEvents = {
[K in SupportedEventsU]: K extends WebhookEventName ? WebhookEvent<K> : never;
Expand Down

0 comments on commit c8a018d

Please sign in to comment.