Skip to content

Commit

Permalink
chore: changed logic to always allow admins and warn users more preci…
Browse files Browse the repository at this point in the history
…sely
  • Loading branch information
gentlementlegen committed Jan 12, 2025
1 parent 24a7b79 commit 705492f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions src/handlers/shared/get-user-task-limit-and-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 14 additions & 11 deletions src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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(
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
});
});
Expand Down

0 comments on commit 705492f

Please sign in to comment.