Skip to content

Commit

Permalink
feat: aggregate price label and requirements errors
Browse files Browse the repository at this point in the history
  • Loading branch information
koya0 committed Jan 18, 2025
1 parent 9b37967 commit 088126b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { getTransformedRole, getUserRoleAndTaskLimit } from "./get-user-task-lim
import structuredMetadata from "./structured-metadata";
import { assignTableComment } from "./table";

async function checkRequirements(context: Context, issue: Context<"issue_comment.created">["payload"]["issue"], login: string) {
async function checkRequirements(context: Context, issue: Context<"issue_comment.created">["payload"]["issue"], login: string): Promise<string[]> {
const {
config: { requiredLabelsToStart },
logger,
} = context;
const issueLabels = issue.labels.map((label) => label.name.toLowerCase());
const userAssociation = await getUserRoleAndTaskLimit(context, login);

const errors: string[] = [];

if (requiredLabelsToStart.length) {
const currentLabelConfiguration = requiredLabelsToStart.find((label) =>
issueLabels.some((issueLabel) => label.name.toLowerCase() === issueLabel.toLowerCase())
Expand All @@ -25,33 +27,39 @@ async function checkRequirements(context: Context, issue: Context<"issue_comment

// Admins can start any task
if (userRole === "admin") {
return;
return errors;
}

if (!currentLabelConfiguration) {
// If we didn't find the label in the allowed list, then the user cannot start this task.
throw logger.error(
errors.push(
`This task does not reflect a business priority at the moment. You may start tasks with one of the following labels: ${requiredLabelsToStart.map((label) => label.name).join(", ")}`
);
logger.error(
`This task does not reflect a business priority at the moment. You may start tasks with one of the following labels: ${requiredLabelsToStart.map((label) => label.name).join(", ")}`,
{
requiredLabelsToStart,
issueLabels,
issue: issue.html_url,
}
);
} else if (!currentLabelConfiguration.allowedRoles.includes(userRole)) {
}
if (currentLabelConfiguration && !currentLabelConfiguration.allowedRoles.includes(userRole)) {
// If we found the label in the allowed list, but the user role does not match the allowed roles, then the user cannot start this task.
const humanReadableRoles = [
...currentLabelConfiguration.allowedRoles.map((o) => (o === "collaborator" ? "a core team member" : `a ${o}`)),
"an administrator",
].join(", or ");
throw logger.error(`You must be ${humanReadableRoles} to start this task`, {
errors.push(`You must be ${humanReadableRoles} to start this task`);
logger.error(`You must be ${humanReadableRoles} to start this task`, {
currentLabelConfiguration,
issueLabels,
issue: issue.html_url,
userAssociation,
});
}
}
return errors;
}

export async function start(
Expand All @@ -70,11 +78,19 @@ export async function start(
const labels = issue.labels ?? [];
const priceLabel = labels.find((label: Label) => label.name.startsWith("Price: "));

const startErrors: string[] = [];

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

await checkRequirements(context, issue, sender.login);
startErrors.push(...(await checkRequirements(context, issue, sender.login)));

if (startErrors.length) {
const startErrorsString = startErrors.join("\n");
throw logger.error(startErrorsString);
}

// is it a child issue?
if (issue.body && isParentIssue(issue.body)) {
Expand Down

0 comments on commit 088126b

Please sign in to comment.