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: close PRs on unassign #46

Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Start | Stop",
"description": "Assign or un-assign yourself from an issue.",
"ubiquity:listeners": ["issue_comment.created", "issues.assigned", "pull_request.opened"],
"ubiquity:listeners": ["issue_comment.created", "issues.assigned", "issues.unassigned", "pull_request.opened"],
"commands": {
"start": {
"ubiquity:example": "/start",
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/shared/stop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Assignee, Context, Sender } from "../../types";
import { addCommentToIssue, closePullRequestForAnIssue } from "../../utils/issue";
import { closePullRequestForAnIssue } from "../../utils/issue";
import { HttpStatusCode, Result } from "../result-types";

export async function stop(
Expand Down
13 changes: 12 additions & 1 deletion src/handlers/user-start-stop.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Repository } from "@octokit/graphql-schema";
import { Context, isContextCommentCreated, Label } from "../types";
import { QUERY_CLOSING_ISSUE_REFERENCES } from "../utils/get-closing-issue-references";
import { addCommentToIssue, getOwnerRepoFromHtmlUrl } from "../utils/issue";
import { addCommentToIssue, closePullRequestForAnIssue, getOwnerRepoFromHtmlUrl } from "../utils/issue";
import { HttpStatusCode, Result } from "./result-types";
import { getDeadline } from "./shared/generate-assignment-comment";
import { start } from "./shared/start";
Expand Down Expand Up @@ -91,3 +91,14 @@ export async function userPullRequest(context: Context<"pull_request.opened">):
}
return { status: HttpStatusCode.NOT_MODIFIED };
}

export async function userUnassigned(context: Context): Promise<Result> {
if (!isContextCommentCreated(context)) {
context.logger.debug("Payload does not contain an issue, skipping issues.unassigned event.");
return { status: HttpStatusCode.NOT_MODIFIED };
}
const { payload } = context;
const { issue, sender, repository } = payload;
await closePullRequestForAnIssue(context, issue.number, repository, sender.login);
return { status: HttpStatusCode.OK, content: "Linked pull-requests closed." };
}
4 changes: 3 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Octokit } from "@octokit/rest";
import { createClient } from "@supabase/supabase-js";
import { LogReturn, Logs } from "@ubiquity-dao/ubiquibot-logger";
import { createAdapters } from "./adapters";
import { userPullRequest, userSelfAssign, userStartStop } from "./handlers/user-start-stop";
import { userPullRequest, userSelfAssign, userStartStop, userUnassigned } from "./handlers/user-start-stop";
import { Context, Env, PluginInputs } from "./types";
import { addCommentToIssue } from "./utils/issue";

Expand Down Expand Up @@ -32,6 +32,8 @@ export async function startStopTask(inputs: PluginInputs, env: Env) {
return await userSelfAssign(context as Context<"issues.assigned">);
case "pull_request.opened":
return await userPullRequest(context as Context<"pull_request.opened">);
case "issues.unassigned":
return await userUnassigned(context);
default:
context.logger.error(`Unsupported event: ${context.eventName}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { createAdapters } from "../adapters";
import { Env } from "./env";
import { Logs } from "@ubiquity-dao/ubiquibot-logger";

export type SupportedEventsU = "issue_comment.created" | "issues.assigned" | "pull_request.opened";
export type SupportedEventsU = "issue_comment.created" | "issues.assigned" | "pull_request.opened" | "issues.unassigned";

export type SupportedEvents = {
[K in SupportedEventsU]: K extends WebhookEventName ? WebhookEvent<K> : never;
};

export function isContextCommentCreated(context: Context): context is Context<"issue_comment.created"> {
gentlementlegen marked this conversation as resolved.
Show resolved Hide resolved
return "comment" in context.payload;
return "issue" in context.payload;
}

export interface Context<T extends SupportedEventsU = SupportedEventsU, TU extends SupportedEvents[T] = SupportedEvents[T]> {
Expand Down
22 changes: 21 additions & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { db } from "./__mocks__/db";
import { server } from "./__mocks__/node";
import usersGet from "./__mocks__/users-get.json";
import { expect, describe, beforeAll, beforeEach, afterAll, afterEach } from "@jest/globals";
import { userStartStop } from "../src/handlers/user-start-stop";
import { userStartStop, userUnassigned } from "../src/handlers/user-start-stop";
import issueTemplate from "./__mocks__/issue-template";
import { createAdapters } from "../src/adapters";
import { createClient } from "@supabase/supabase-js";
Expand Down Expand Up @@ -112,6 +112,26 @@ describe("User start/stop", () => {
);
});

test("Author's manual unassign should close linked issue", async () => {
const infoSpy = jest.spyOn(console, "info").mockImplementation(() => {});
const issue = db.issue.findFirst({ where: { id: { equals: 2 } } }) as unknown as Issue;
const sender = db.users.findFirst({ where: { id: { equals: 2 } } }) as unknown as PayloadSender;
const context = createContext(issue, sender, "") as Context<"issues.unassigned">;

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

const { content } = await userUnassigned(context);

expect(content).toEqual("Linked pull-requests closed.");
const logs = infoSpy.mock.calls.flat();
expect(logs[0]).toMatch(/Opened prs/);
expect(cleanLogString(logs[3])).toMatch(
cleanLogString(
" › ```diff# These linked pull requests are closed: http://github.com/ubiquity/test-repo/pull/2 http://github.com/ubiquity/test-repo/pull/3"
gentlementlegen marked this conversation as resolved.
Show resolved Hide resolved
)
);
});

test("User can't stop an issue they're not assigned to", async () => {
const issue = db.issue.findFirst({ where: { id: { equals: 2 } } }) as unknown as Issue;
const sender = db.users.findFirst({ where: { id: { equals: 1 } } }) as unknown as PayloadSender;
Expand Down