Skip to content

Commit

Permalink
Merge pull request #35 from sshivaditya2019/issuesim
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 authored Oct 15, 2024
2 parents ab3df04 + fb56335 commit ab694b1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/handlers/issue-deduplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,24 @@ export async function issueChecker(context: Context): Promise<boolean> {
state: "closed",
state_reason: "not_planned",
});
return true;
}

if (similarIssues.length > 0) {
logger.info(`Similar issue which matches more than ${context.config.warningThreshold} already exists`);
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,
issue_number: issue.number,
body: issueBody,
});
}
context.logger.info("No similar issues found");
return false;
}

Expand Down
32 changes: 32 additions & 0 deletions src/handlers/transfer-issue.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
3 changes: 3 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<K> : never;
Expand Down
1 change: 1 addition & 0 deletions src/types/payload.ts
Original file line number Diff line number Diff line change
@@ -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"];
9 changes: 7 additions & 2 deletions src/types/typeguards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}

0 comments on commit ab694b1

Please sign in to comment.