Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat(github): move the task back when an issue is reopened (#1518)

Luan Taraschif9593984e693
diff --git a/apps/api/src/plugins/github/webhook-handler.ts b/apps/api/src/plugins/github/webhook-handler.ts
index fc33be45c..c977436f1 100644
--- a/apps/api/src/plugins/github/webhook-handler.ts
+++ b/apps/api/src/plugins/github/webhook-handler.ts
@@ -4,6 +4,7 @@ import { handleIssueCommentCreated } from "./webhooks/issue-comment-created";
import { handleIssueEdited } from "./webhooks/issue-edited";
import { handleIssueLabeled } from "./webhooks/issue-labeled";
import { handleIssueOpened } from "./webhooks/issue-opened";
+import { handleIssueReopened } from "./webhooks/issue-reopened";
import { handleLabelCreated } from "./webhooks/label-created";
import { handlePullRequestClosed } from "./webhooks/pull-request-closed";
import { handlePullRequestOpened } from "./webhooks/pull-request-opened";
@@ -86,6 +87,19 @@ export function setupWebhookHandlers() {
}
});
+ githubApp.webhooks.on("issues.reopened", async ({ payload }) => {
+ console.log("[GitHub Webhook] Handling issues.reopened");
+ try {
+ await handleIssueReopened(
+ payload as Parameters<typeof handleIssueReopened>[0],
+ );
+ console.log("[GitHub Webhook] issues.reopened handled successfully");
+ } catch (error) {
+ console.error("[GitHub Webhook] issues.reopened handler error:", error);
+ throw error;
+ }
+ });
+
githubApp.webhooks.on("issues.labeled", async ({ payload }) => {
console.log("[GitHub Webhook] Handling issues.labeled");
try {
diff --git a/apps/api/src/plugins/github/webhooks/issue-reopened.ts b/apps/api/src/plugins/github/webhooks/issue-reopened.ts
new file mode 100644
index 000000000..7f838deda
--- /dev/null
+++ b/apps/api/src/plugins/github/webhooks/issue-reopened.ts
@@ -0,0 +1,106 @@
+import { and, eq } from "drizzle-orm";
+import db from "../../../database";
+import { externalLinkTable, taskTable } from "../../../database/schema";
+import { publishEvent } from "../../../events";
+import { updateExternalLink } from "../services/link-manager";
+import {
+ findAllIntegrationsByRepo,
+ updateTaskStatus,
+} from "../services/task-service";
+import { resolveTargetStatus } from "../utils/resolve-column";
+
+type IssueReopenedPayload = {
+ action: string;
+ issue: {
+ number: number;
+ title: string;
+ html_url: string;
+ state: string;
+ };
+ repository: {
+ owner: { login: string };
+ name: string;
+ full_name: string;
+ };
+};
+
+export async function handleIssueReopened(payload: IssueReopenedPayload) {
+ const { issue, repository } = payload;
+
+ const integrations = await findAllIntegrationsByRepo(
+ repository.owner.login,
+ repository.name,
+ );
+
+ for (const integration of integrations) {
+ const externalLink = await db.query.externalLinkTable.findFirst({
+ where: and(
+ eq(externalLinkTable.integrationId, integration.id),
+ eq(externalLinkTable.resourceType, "issue"),
+ eq(externalLinkTable.externalId, issue.number.toString()),
+ ),
+ });
+
+ if (!externalLink) {
+ continue;
+ }
+
+ const task = await db.query.taskTable.findFirst({
+ where: eq(taskTable.id, externalLink.taskId),
+ });
+
+ if (!task) {
+ continue;
+ }
+
+ let existingMetadata: Record<string, unknown> = {};
+ if (externalLink.metadata) {
+ try {
+ existingMetadata = JSON.parse(externalLink.metadata) as Record<
+ string,
+ unknown
+ >;
+ } catch (error) {
+ console.warn("Failed to parse GitHub issue metadata for reopen sync", {
+ externalLinkId: externalLink.id,
+ metadata: externalLink.metadata,
+ error,
+ });
+ }
+ }
+
+ if (existingMetadata.createdFrom === "kaneo") {
+ continue;
+ }
+
+ const targetStatus = await resolveTargetStatus(
+ task.projectId,
+ "issue_reopened",
+ "to-do",
+ );
+
+ const statusResult = await updateTaskStatus(task.id, targetStatus);
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ await publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ });
+ }
+
+ await updateExternalLink(externalLink.id, {
+ metadata: {
+ ...existingMetadata,
+ state: "open",
+ },
+ });
+ }
+}