Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: task-restriction #65

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("Only collaborators can 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);
}
5 changes: 3 additions & 2 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const SUCCESS_MESSAGE = "Task assigned successfully";

describe("User start/stop", () => {
beforeEach(async () => {
drop(db);
jest.clearAllMocks();
jest.resetModules();
await setupTests();
Expand Down Expand Up @@ -604,7 +605,7 @@ const maxConcurrentDefaults = {
contributor: 4,
};

function createContext(
export function createContext(
issue: Record<string, unknown>,
sender: Record<string, unknown> | undefined,
body = "/start",
Expand Down Expand Up @@ -641,7 +642,7 @@ function createContext(
};
}

function getSupabase(withData = true) {
export function getSupabase(withData = true) {
const mockedTable = {
select: jest.fn().mockReturnValue({
eq: jest.fn().mockReturnValue({
Expand Down
77 changes: 77 additions & 0 deletions tests/start.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, jest } from "@jest/globals";
import { drop } from "@mswjs/data";
import dotenv from "dotenv";
import { createAdapters } from "../src/adapters";
import { start } from "../src/handlers/shared/start";
import { Context } from "../src/types";
import { db } from "./__mocks__/db";
import issueTemplate from "./__mocks__/issue-template";
import { server } from "./__mocks__/node";
import { createContext, getSupabase } from "./main.test";

dotenv.config();

type Issue = Context<"issue_comment.created">["payload"]["issue"];
type PayloadSender = Context["payload"]["sender"];

beforeAll(() => {
server.listen();
});

afterEach(() => {
server.resetHandlers();
});

afterAll(() => server.close());

beforeEach(async () => {
jest.clearAllMocks();
jest.resetModules();
await setupTests();
});

async function setupTests() {
db.users.create({
id: 1,
login: "user1",
role: "contributor",
});
db.issue.create({
...issueTemplate,
labels: [{ name: "Priority: 1 (Normal)", description: "collaborator only" }, ...issueTemplate.labels],
});
db.repo.create({
id: 1,
html_url: "",
name: "test-repo",
owner: {
login: "ubiquity",
id: 1,
},
issues: [],
});
}

describe("Collaborator tests", () => {
beforeEach(async () => {
drop(db);
jest.clearAllMocks();
jest.resetModules();
await setupTests();
});

it("Should return error if user trying to assign is not a collaborator", async () => {
const issue = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue;
const sender = db.users.findFirst({ where: { id: { equals: 1 } } }) as unknown as PayloadSender;
const context = createContext(issue, sender, "/start");
context.adapters = createAdapters(getSupabase(), context);
await expect(start(context, issue, sender, [])).rejects.toMatchObject({
logMessage: {
diff: "```diff\n! Only collaborators can be assigned to this issue.\n```",
level: "error",
raw: "Only collaborators can be assigned to this issue.",
type: "error",
},
});
});
});