diff --git a/src/handlers/issue-deduplication.ts b/src/handlers/issue-deduplication.ts index 0cca940..7ca35b3 100644 --- a/src/handlers/issue-deduplication.ts +++ b/src/handlers/issue-deduplication.ts @@ -51,6 +51,7 @@ export async function issueChecker(context: Context): Promise { state: "closed", state_reason: "not_planned", }); + return true; } if (similarIssues.length > 0) { @@ -58,11 +59,8 @@ export async function issueChecker(context: Context): Promise { await handleSimilarIssuesComment(context, payload, issueBody, issue.number, similarIssues); return true; } - } - context.logger.info("No similar issues found"); - - //Use the IssueBody (Without footnotes) to update the issue - if (issueBody !== issue.body) { + } else { + //Use the IssueBody (Without footnotes) to update the issue when no similar issues are found await octokit.issues.update({ owner: payload.repository.owner.login, repo: payload.repository.name, @@ -70,6 +68,7 @@ export async function issueChecker(context: Context): Promise { body: issueBody, }); } + context.logger.info("No similar issues found"); return false; } diff --git a/src/handlers/transfer-issue.ts b/src/handlers/transfer-issue.ts new file mode 100644 index 0000000..bf92d47 --- /dev/null +++ b/src/handlers/transfer-issue.ts @@ -0,0 +1,32 @@ +import { Context } from "../types"; +import { IssueTransferPayload } from "../types/payload"; + +export async function issueTransfer(context: Context) { + const { + logger, + adapters: { supabase }, + } = context; + const { changes, issue } = (context as { payload: IssueTransferPayload }).payload; + const nodeId = issue.node_id; + const { new_issue, new_repository } = changes; + //Fetch the new details of the issue + const newIssueNodeId = new_issue.node_id; + const markdown = new_issue.body + " " + new_issue.title || null; + const authorId = new_issue.user?.id || -1; + const isPrivate = new_repository.private; + + //Delete the issue from the old repository + //Create the new issue in the new repository + try { + await supabase.issue.deleteIssue(nodeId); + await supabase.issue.createIssue(newIssueNodeId, new_issue, isPrivate, markdown, authorId); + } catch (error) { + if (error instanceof Error) { + logger.error(`Error transferring issue:`, { error: error, stack: error.stack }); + throw error; + } else { + logger.error(`Error transferring issue:`, { err: error, error: new Error() }); + throw error; + } + } +} diff --git a/src/plugin.ts b/src/plugin.ts index f382409..5cdf193 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -14,6 +14,7 @@ import { updateIssue } from "./handlers/update-issue"; import { Context, Env, PluginInputs } from "./types"; import { Database } from "./types/database"; import { isIssueCommentEvent, isIssueEvent } from "./types/typeguards"; +import { issueTransfer } from "./handlers/transfer-issue"; /** * The main plugin function. Split for easier testing. @@ -41,6 +42,8 @@ export async function runPlugin(context: Context) { return await issueMatching(context); case "issues.deleted": return await deleteIssues(context); + case "issues.transferred": + return await issueTransfer(context); } } else if (eventName == "issues.labeled") { return await issueMatching(context); diff --git a/src/types/context.ts b/src/types/context.ts index b38d836..46f2e67 100644 --- a/src/types/context.ts +++ b/src/types/context.ts @@ -17,7 +17,8 @@ export type SupportedEventsU = | "issues.opened" | "issues.edited" | "issues.deleted" - | "issues.labeled"; + | "issues.labeled" + | "issues.transferred"; export type SupportedEvents = { [K in SupportedEventsU]: K extends WebhookEventName ? WebhookEvent : never; diff --git a/src/types/payload.ts b/src/types/payload.ts index 395fa09..d1f3d64 100644 --- a/src/types/payload.ts +++ b/src/types/payload.ts @@ -1,3 +1,4 @@ import { EmitterWebhookEvent as WebhookEvent } from "@octokit/webhooks"; export type CommentPayload = WebhookEvent<"issue_comment">["payload"]; export type IssuePayload = WebhookEvent<"issues">["payload"]; +export type IssueTransferPayload = WebhookEvent<"issues.transferred">["payload"]; diff --git a/src/types/typeguards.ts b/src/types/typeguards.ts index 01a6c26..aae7236 100644 --- a/src/types/typeguards.ts +++ b/src/types/typeguards.ts @@ -20,6 +20,11 @@ export function isIssueCommentEvent(context: Context): context is Context<"issue * * @param context The context object. */ -export function isIssueEvent(context: Context): context is Context<"issues.opened" | "issues.edited" | "issues.deleted"> { - return context.eventName === "issues.opened" || context.eventName === "issues.edited" || context.eventName === "issues.deleted"; +export function isIssueEvent(context: Context): context is Context<"issues.opened" | "issues.edited" | "issues.deleted" | "issues.transferred"> { + return ( + context.eventName === "issues.opened" || + context.eventName === "issues.edited" || + context.eventName === "issues.deleted" || + context.eventName === "issues.transferred" + ); }