diff --git a/bun.lockb b/bun.lockb index a8d91362..7c99c50a 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 082dc4ef..8fbfebb0 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "@octokit/types": "^13.6.2", "@sinclair/typebox": "0.34.3", "@supabase/supabase-js": "2.42.0", - "@ubiquity-os/plugin-sdk": "^2.0.0", + "@ubiquity-os/plugin-sdk": "^2.0.1", "@ubiquity-os/ubiquity-os-logger": "^1.3.2", "dotenv": "^16.4.7", "hono": "^4.6.14", diff --git a/src/handlers/shared/get-user-task-limit-and-role.ts b/src/handlers/shared/get-user-task-limit-and-role.ts index 56eccfe9..35b58ed2 100644 --- a/src/handlers/shared/get-user-task-limit-and-role.ts +++ b/src/handlers/shared/get-user-task-limit-and-role.ts @@ -13,6 +13,15 @@ export function isCollaboratorRole(role: string) { return COLLABORATOR_ROLES.includes(role.toLowerCase()); } +export function getTransformedRole(role: string) { + if (isAdminRole(role)) { + return "admin"; + } else if (isCollaboratorRole(role)) { + return "collaborator"; + } + return "contributor"; +} + export function getUserTaskLimit(maxConcurrentTasks: PluginSettings["maxConcurrentTasks"], role: string) { if (isAdminRole(role)) { return Infinity; diff --git a/src/handlers/shared/start.ts b/src/handlers/shared/start.ts index a41a6d75..6b21a0b3 100644 --- a/src/handlers/shared/start.ts +++ b/src/handlers/shared/start.ts @@ -5,7 +5,7 @@ import { HttpStatusCode, Result } from "../result-types"; import { hasUserBeenUnassigned } from "./check-assignments"; import { checkTaskStale } from "./check-task-stale"; import { generateAssignmentComment } from "./generate-assignment-comment"; -import { getUserRoleAndTaskLimit, isAdminRole, isCollaboratorRole } from "./get-user-task-limit-and-role"; +import { getTransformedRole, getUserRoleAndTaskLimit } from "./get-user-task-limit-and-role"; import structuredMetadata from "./structured-metadata"; import { assignTableComment } from "./table"; @@ -21,6 +21,13 @@ async function checkRequirements(context: Context, issue: Context<"issue_comment const currentLabelConfiguration = requiredLabelsToStart.find((label) => issueLabels.some((issueLabel) => label.name.toLowerCase() === issueLabel.toLowerCase()) ); + const userRole = getTransformedRole(userAssociation.role); + + // Admins can start any task + if (userRole === "admin") { + return; + } + if (!currentLabelConfiguration) { // If we didn't find the label in the allowed list, then the user cannot start this task. throw logger.error( @@ -31,17 +38,13 @@ async function checkRequirements(context: Context, issue: Context<"issue_comment issue: issue.html_url, } ); - } else if (!currentLabelConfiguration.allowedRoles.length && !isAdminRole(userAssociation.role)) { - // An empty allowedRoles list implies admin only task - throw logger.error("You must be an admin to start this task", { - currentLabelConfiguration, - issueLabels, - issue: issue.html_url, - userAssociation, - }); - } else if (!currentLabelConfiguration.allowedRoles.includes("contributor") && !isCollaboratorRole(userAssociation.role)) { + } else if (!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. - throw logger.error("You must be a core team member to 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`, { currentLabelConfiguration, issueLabels, issue: issue.html_url, diff --git a/tests/main.test.ts b/tests/main.test.ts index cb0f1897..152b31bb 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -339,7 +339,7 @@ describe("User start/stop", () => { await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { - raw: "You must be a core team member to start this task", + raw: "You must be a core team member, or an administrator to start this task", }, }); });