Skip to content

Commit

Permalink
Merge pull request ubiquity-os-marketplace#61 from gentlementlegen/fi…
Browse files Browse the repository at this point in the history
…x/duplicate-error-messages
  • Loading branch information
0x4007 authored Oct 2, 2024
2 parents ea1b270 + be81469 commit 886425d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
20 changes: 9 additions & 11 deletions src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function start(
const { taskStaleTimeoutDuration } = config;

if (!sender) {
throw new Error(logger.error(`Skipping '/start' since there is no sender in the context.`).logMessage.raw);
throw logger.error(`Skipping '/start' since there is no sender in the context.`);
}

// is it a child issue?
Expand All @@ -27,7 +27,7 @@ export async function start(
context,
"```diff\n# Please select a child issue from the specification checklist to work on. The '/start' command is disabled on parent issues.\n```"
);
throw new Error(logger.error(`Skipping '/start' since the issue is a parent issue`).logMessage.raw);
throw logger.error(`Skipping '/start' since the issue is a parent issue`);
}

let commitHash: string | null = null;
Expand All @@ -46,19 +46,17 @@ export async function start(
// is it assignable?

if (issue.state === ISSUE_TYPE.CLOSED) {
throw new Error(logger.error("This issue is closed, please choose another.", { issueNumber: issue.number }).logMessage.raw);
throw logger.error("This issue is closed, please choose another.", { issueNumber: issue.number });
}

const assignees = issue?.assignees ?? [];

// find out if the issue is already assigned
if (assignees.length !== 0) {
const isCurrentUserAssigned = !!assignees.find((assignee) => assignee?.login === sender.login);
throw new Error(
logger.error(
isCurrentUserAssigned ? "You are already assigned to this task." : "This issue is already assigned. Please choose another unassigned task.",
{ issueNumber: issue.number }
).logMessage.raw
throw logger.error(
isCurrentUserAssigned ? "You are already assigned to this task." : "This issue is already assigned. Please choose another unassigned task.",
{ issueNumber: issue.number }
);
}

Expand All @@ -81,15 +79,15 @@ export async function start(
}

if (error) {
throw new Error(logger.error(error, { issueNumber: issue.number }).logMessage.raw);
throw logger.error(error, { issueNumber: issue.number });
}

// get labels
const labels = issue.labels ?? [];
const priceLabel = labels.find((label: Label) => label.name.startsWith("Price: "));

if (!priceLabel) {
throw new Error(logger.error("No price label is set to calculate the duration", { issueNumber: issue.number }).logMessage.raw);
throw logger.error("No price label is set to calculate the duration", { issueNumber: issue.number });
}

const deadline = getDeadline(labels);
Expand Down Expand Up @@ -161,7 +159,7 @@ async function handleTaskLimitChecks(username: string, context: Context, logger:
}

if (await hasUserBeenUnassigned(context, username)) {
throw new Error(logger.error(`${username} you were previously unassigned from this task. You cannot be reassigned.`, { username }).logMessage.raw);
throw logger.error(`${username} you were previously unassigned from this task. You cannot be reassigned.`, { username });
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/shared/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function stop(
const userToUnassign = assignees.find((assignee: Partial<Assignee>) => assignee?.login?.toLowerCase() === sender.login.toLowerCase());

if (!userToUnassign) {
throw new Error(logger.error("You are not assigned to this task", { issueNumber, user: sender.login })?.logMessage.raw as string);
throw logger.error("You are not assigned to this task", { issueNumber, user: sender.login });
}

// close PR
Expand Down
6 changes: 2 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ export async function startStopTask(inputs: PluginInputs, env: Env) {
let errorMessage;
if (err instanceof LogReturn) {
errorMessage = err;
} else if (err instanceof Error) {
errorMessage = context.logger.error(err.message, { error: err });
await addCommentToIssue(context, `${errorMessage?.logMessage.diff}\n<!--\n${sanitizeMetadata(errorMessage?.metadata)}\n-->`);
} else {
errorMessage = context.logger.error("An error occurred", { err });
context.logger.error("An error occurred", { err });
}
await addCommentToIssue(context, `${errorMessage?.logMessage.diff}\n<!--\n${sanitizeMetadata(errorMessage?.metadata)}\n-->`);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/utils/issue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods/dist-types/generated/parameters-and-response-types";
import { RestEndpointMethodTypes } from "@octokit/rest";
import ms from "ms";
import { Context } from "../types/context";
import { GitHubIssueSearch, Review } from "../types/payload";
Expand Down Expand Up @@ -142,9 +142,7 @@ async function confirmMultiAssignment(context: Context, issueNumber: number, use
});

if (!assignees?.length) {
throw new Error(
logger.error("We detected that this task was not assigned to anyone. Please report this to the maintainers.", { issueNumber, usernames }).logMessage.raw
);
throw logger.error("We detected that this task was not assigned to anyone. Please report this to the maintainers.", { issueNumber, usernames });
}

if (isPrivate && assignees?.length <= 1) {
Expand Down
22 changes: 14 additions & 8 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe("User start/stop", () => {

context.adapters = createAdapters(getSupabase(), context);

await expect(userStartStop(context)).rejects.toThrow("You are not assigned to this task");
await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "You are not assigned to this task" } });
});

test("User can't stop an issue without assignees", async () => {
Expand All @@ -151,7 +151,7 @@ describe("User start/stop", () => {
const context = createContext(issue, sender, "/stop") as Context<"issue_comment.created">;
context.adapters = createAdapters(getSupabase(), context as unknown as Context);

await expect(userStartStop(context)).rejects.toThrow("You are not assigned to this task");
await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "You are not assigned to this task" } });
});

test("User can't start an issue that's already assigned", async () => {
Expand All @@ -162,7 +162,9 @@ describe("User start/stop", () => {

context.adapters = createAdapters(getSupabase(), context);

await expect(userStartStop(context)).rejects.toThrow("This issue is already assigned. Please choose another unassigned task.");
await expect(userStartStop(context)).rejects.toMatchObject({
logMessage: { raw: "This issue is already assigned. Please choose another unassigned task." },
});
});

test("User can't start an issue without a price label", async () => {
Expand All @@ -173,7 +175,7 @@ describe("User start/stop", () => {

context.adapters = createAdapters(getSupabase(), context);

await expect(userStartStop(context)).rejects.toThrow("No price label is set to calculate the duration");
await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "No price label is set to calculate the duration" } });
});

test("User can't start an issue without a wallet address", async () => {
Expand All @@ -194,7 +196,7 @@ describe("User start/stop", () => {

context.adapters = createAdapters(getSupabase(), context as unknown as Context);

await expect(userStartStop(context)).rejects.toThrow("This issue is closed, please choose another.");
await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "This issue is closed, please choose another." } });
});

test("User can't start an issue that's a parent issue", async () => {
Expand All @@ -205,7 +207,7 @@ describe("User start/stop", () => {

context.adapters = createAdapters(getSupabase(), context);

await expect(userStartStop(context)).rejects.toThrow("Skipping '/start' since the issue is a parent issue");
await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "Skipping '/start' since the issue is a parent issue" } });
});

test("should set maxLimits to 6 if the user is a member", async () => {
Expand All @@ -218,7 +220,9 @@ describe("User start/stop", () => {
const context = createContext(issue, sender) as unknown as Context;

context.adapters = createAdapters(getSupabase(), context as unknown as Context);
await expect(userStartStop(context)).rejects.toThrow("You have reached your max task limit. Please close out some tasks before assigning new ones.");
await expect(userStartStop(context)).rejects.toMatchObject({
logMessage: { raw: "You have reached your max task limit. Please close out some tasks before assigning new ones." },
});

expect(memberLimit).toEqual(6);
});
Expand All @@ -230,7 +234,9 @@ describe("User start/stop", () => {
const context = createContext(issue, sender, "/start") as Context<"issue_comment.created">;
context.adapters = createAdapters(getSupabase(), context);

await expect(userStartStop(context)).rejects.toThrow("user2 you were previously unassigned from this task. You cannot be reassigned.");
await expect(userStartStop(context)).rejects.toMatchObject({
logMessage: { raw: "user2 you were previously unassigned from this task. You cannot be reassigned." },
});
});

test("Should throw if no BOT_USER_ID is set", async () => {
Expand Down

0 comments on commit 886425d

Please sign in to comment.