Skip to content

Commit

Permalink
feat: enforce collaborator-only label restriction
Browse files Browse the repository at this point in the history
Adds a check to prevent non-collaborators from being assigned to issues.
  • Loading branch information
gentlementlegen committed Oct 10, 2024
1 parent 886425d commit 413c844
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context, ISSUE_TYPE, Label } from "../../types";
import { isUserCollaborator } from "../../utils/get-user-association";
import { addAssignees, addCommentToIssue, getAssignedIssues, getAvailableOpenedPullRequests, getTimeValue, isParentIssue } from "../../utils/issue";
import { HttpStatusCode, Result } from "../result-types";
import { hasUserBeenUnassigned } from "./check-assignments";
Expand Down Expand Up @@ -82,14 +83,26 @@ export async function start(
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 logger.error("No price label is set to calculate the duration", { issueNumber: issue.number });
}

// Checks if non-collaborators can be assigned to the issue
for (const label of labels) {
if (label.description?.includes("collaborator only")) {
for (const user of toAssign) {
if (!(await isUserCollaborator(context, user))) {
throw logger.error("Non-collaborators cannot be assigned to this issue", {
username: user,
});
}
}
}
}

const deadline = getDeadline(labels);
const toAssignIds = await fetchUserIds(context, toAssign);

Expand Down
9 changes: 9 additions & 0 deletions src/utils/get-user-association.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Context } from "../types";

export async function isUserCollaborator(context: Context, username: string): Promise<boolean> {
const { data } = await context.octokit.rest.orgs.getMembershipForUser({
org: context.payload.repository.owner.login,
username,
});
return ["collaborator", "member", "admin"].includes(data.role);
}

0 comments on commit 413c844

Please sign in to comment.