Skip to content

Commit

Permalink
clean up merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-ae committed Sep 2, 2024
1 parent 579e7b4 commit 0097358
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
15 changes: 3 additions & 12 deletions src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function start(context: Context, issue: Context["payload"]["issue"]
const toAssign = [];
// check max assigned issues
for (const user of teammates) {
if (await handleTaskLimitChecks(user, context, maxConcurrentTasks, logger, sender.login)) {
if (await handleTaskLimitChecks(user, context, maxTask.limit, logger, sender.login)) {
toAssign.push(user);
}
}
Expand Down Expand Up @@ -136,8 +136,6 @@ export async function start(context: Context, issue: Context["payload"]["issue"]
return { output: "Task assigned successfully" };
}

async function handleTaskLimitChecks(username: string, context: Context, maxConcurrentTasks: { [role: string]: number }, logger: Context["logger"], sender: string) {

async function fetchUserIds(context: Context, username: string[]) {
const ids = [];

Expand All @@ -157,22 +155,16 @@ async function fetchUserIds(context: Context, username: string[]) {
}

async function handleTaskLimitChecks(username: string, context: Context, maxConcurrentTasks: number, logger: Context["logger"], sender: string) {

const openedPullRequests = await getAvailableOpenedPullRequests(context, username);
const assignedIssues = await getAssignedIssues(context, username);

// check for max and enforce max
const maxTask = await getUserRoleAndTaskLimit(context, sender);
const maxTasksForRole = maxConcurrentTasks[maxTask.limit] ?? 0;

if (assignedIssues.length - openedPullRequests.length >= maxTasksForRole) {
throw logger.error(username === sender ? "You have reached your max task limit" : `${username} has reached their max task limit`, {

if (Math.abs(assignedIssues.length - openedPullRequests.length) >= maxConcurrentTasks) {
const log = logger.error(username === sender ? "You have reached your max task limit" : `${username} has reached their max task limit`, {
assignedIssues: assignedIssues.length,
openedPullRequests: openedPullRequests.length,
maxConcurrentTasks: maxTasksForRole,
maxConcurrentTasks,
});
await addCommentToIssue(context, log?.logMessage.diff as string);
return false;
Expand All @@ -183,5 +175,4 @@ async function handleTaskLimitChecks(username: string, context: Context, maxConc
}

return true;
}

}
1 change: 0 additions & 1 deletion tests/__mocks__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export const db = factory({
type: String,
url: String,
}),
pull_request: Object,
assignees: Array,
requested_reviewers: Array,
requested_teams: Array,
Expand Down
1 change: 0 additions & 1 deletion tests/__mocks__/users-get.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@
"id": 5,
"login": "user5",
"role": "member"
"login": "user2"
}
]
17 changes: 9 additions & 8 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe("User start/stop", () => {

context.adapters = createAdapters(getSupabase(), context);

expect(output).toEqual({ output: "You are not assigned to this task" });
await expect(userStartStop(context as unknown as Context)).rejects.toThrow("```diff\n! You are not assigned to this task\n```");
});

test("User can't stop an issue without assignees", async () => {
Expand Down Expand Up @@ -178,9 +178,9 @@ describe("User start/stop", () => {
const issue = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue;
const sender = db.users.findFirst({ where: { id: { equals: 4 } } }) as unknown as Sender;

// test("User can't start another issue if they have reached the max limit", async () => {
// const issue = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue;
// const sender = db.users.findFirst({ where: { id: { equals: 2 } } }) as unknown as PayloadSender;
// test("User can't start another issue if they have reached the max limit", async () => {
// const issue = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue;
// const sender = db.users.findFirst({ where: { id: { equals: 2 } } }) as unknown as PayloadSender;

const contributorLimit = maxConcurrentDefaults.contributor;
createIssuesForMaxAssignment(contributorLimit, sender.id);
Expand Down Expand Up @@ -224,8 +224,9 @@ describe("User start/stop", () => {
await userStartStop(context);
} catch (error) {
if (error instanceof Error) {
expect(error.message).toEqual("Too many assigned issues, you have reached your max limit of 2 issues.");
context.config.maxConcurrentTasks = 1;
expect(error.message).toEqual("Too many assigned issues, you have reached your max limit of 2 issues.")
}
}

context.adapters = createAdapters(getSupabase(), context);

Expand Down Expand Up @@ -633,15 +634,15 @@ const maxConcurrentDefaults = {
member: 4,
contributor: 2,
};

function createContext(
issue: Record<string, unknown>,
sender: Record<string, unknown>,
body = "/start",
appId: string | null = "1",
startRequiresWallet = false
): Context {

return {
adapters: {} as ReturnType<typeof createAdapters>,
payload: {
Expand Down

0 comments on commit 0097358

Please sign in to comment.