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

Issuesim #35

Merged
merged 2 commits into from
Oct 15, 2024
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
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);
Comment on lines +21 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that it would be more efficient to just modify the existing entry?

} 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"
);
}