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.

merge: propagate committed integration notifications into echo handling

Tin7039659982a3
diff --cc apps/api/src/plugins/gitea/webhooks/issue-edited.ts
index f10cdd587,87c2d377b..5b9037c23
--- a/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
@@@ -1,10 -1,5 +1,11 @@@
+ import { publishEvent } from "../../../events";
import { withIntegrationLink } from "../../github/services/with-integration-link";
+import type { GiteaConfig } from "../config";
+import { createGiteaClient } from "../utils/gitea-api";
+import {
+ inboundEcho,
+ withEchoConfirmation,
+} from "../../github/utils/inbound-echo";
import { linkedTaskScope } from "../../github/services/integration-task-scope";
import { taskTable } from "../../../database/schema";
import {
@@@ -70,67 -64,45 +71,67 @@@ export async function handleGiteaIssueE
continue;
}
- await withIntegrationLink(
- externalLink,
- integration,
- async (db, afterCommit, externalLink) => {
- const task = await db.query.taskTable.findFirst({
- where: linkedTaskScope(externalLink.taskId, integration.projectId),
- });
-
- if (!task) {
- return;
- }
-
- const metadata = externalLink.metadata
- ? JSON.parse(externalLink.metadata)
- : {};
-
- const updateData: Record<string, unknown> = {};
- const updatedMetadata = { ...metadata };
-
- if (!updatedMetadata.lastSync) {
- updatedMetadata.lastSync = {};
- }
+ const readCurrent = async () => {
+ try {
+ const config = JSON.parse(integration.config) as GiteaConfig;
+ return await createGiteaClient(config).getIssue(
+ config.repositoryOwner,
+ config.repositoryName,
+ issue.number,
+ );
+ } catch {
+ return issue;
+ }
+ };
+ await withEchoConfirmation(readCurrent, (current) =>
+ withIntegrationLink(
+ externalLink,
+ integration,
- async (db, _afterCommit, externalLink) => {
++ async (db, afterCommit, externalLink) => {
+ const task = await db.query.taskTable.findFirst({
+ where: linkedTaskScope(externalLink.taskId, integration.projectId),
+ });
+
+ if (!task) {
+ return;
+ }
+ const metadata = externalLink.metadata
+ ? JSON.parse(externalLink.metadata)
+ : {};
- if (changes.title) {
- const lastTitleSync = metadata.lastSync?.title;
+ const updateData: Record<string, unknown> = {};
+ const updatedMetadata = { ...metadata };
- let shouldUpdateTitle = true;
+ if (!updatedMetadata.lastSync) {
+ updatedMetadata.lastSync = {};
+ }
- if (lastTitleSync) {
- if (
- lastTitleSync.value === issue.title &&
- lastTitleSync.source === "kaneo"
- ) {
- shouldUpdateTitle = false;
+ if (changes.title) {
+ const lastTitleSync = metadata.lastSync?.title;
+
+ let shouldUpdateTitle = true;
+
+ if (lastTitleSync) {
+ if (
+ inboundEcho(
+ lastTitleSync,
+ issue.title,
+ issue.updated_at,
+ current?.title,
+ )
+ ) {
+ shouldUpdateTitle = false;
+ }
}
- const timeSinceLastSync =
- Date.now() - new Date(lastTitleSync.timestamp).getTime();
- if (timeSinceLastSync < 2000 && shouldUpdateTitle) {
- shouldUpdateTitle = false;
+
+ if (shouldUpdateTitle) {
+ updateData.title = issue.title;
+ updatedMetadata.lastSync.title = {
+ outbound: metadata.lastSync?.title?.outbound,
+ timestamp: new Date().toISOString(),
+ source: "gitea",
+ value: issue.title,
+ };
}
}
@@@ -172,25 -138,40 +173,31 @@@
}
}
- if (shouldUpdateDescription) {
- updateData.description = formattedDescription;
- updatedMetadata.lastSync.description = {
- timestamp: new Date().toISOString(),
- source: "gitea",
- value: formattedDescription,
- };
+ if (Object.keys(updateData).length > 0) {
+ await db
+ .update(taskTable)
+ .set(updateData)
+ .where(linkedTaskScope(task.id, integration.projectId));
+
+ await updateExternalLink(
+ externalLink.id,
+ {
+ title: issue.title,
+ metadata: updatedMetadata,
+ },
+ db,
+ );
++ afterCommit(() =>
++ publishEvent("task.updated", {
++ projectId: integration.projectId,
++ taskId: externalLink.taskId,
++ }),
++ );
}
- }
-
- if (Object.keys(updateData).length > 0) {
- await db
- .update(taskTable)
- .set(updateData)
- .where(linkedTaskScope(task.id, integration.projectId));
-
- await updateExternalLink(
- externalLink.id,
- {
- title: issue.title,
- metadata: updatedMetadata,
- },
- db,
- );
- afterCommit(() =>
- publishEvent("task.updated", {
- projectId: integration.projectId,
- taskId: externalLink.taskId,
- }),
- );
- }
-
- return;
- },
+
+ return;
+ },
+ ),
);
}
}
diff --cc apps/api/src/plugins/github/webhooks/issue-edited.ts
index c0bc13e81,4d40cc242..0a1de9869
--- a/apps/api/src/plugins/github/webhooks/issue-edited.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-edited.ts
@@@ -1,7 -1,5 +1,8 @@@
+ import { publishEvent } from "../../../events";
import { withIntegrationLink } from "../services/with-integration-link";
+import type { GitHubConfig } from "../config";
+import { getVerifiedInstallationOctokit } from "../utils/github-app";
+import { inboundEcho, withEchoConfirmation } from "../utils/inbound-echo";
import { linkedTaskScope } from "../services/integration-task-scope";
import { taskTable } from "../../../database/schema";
import { findExternalLink, updateExternalLink } from "../services/link-manager";
@@@ -69,153 -70,142 +70,159 @@@ export async function handleIssueEdited
continue;
}
- await withIntegrationLink(
- externalLink,
- integration,
- async (db, afterCommit, externalLink) => {
- const task = await db.query.taskTable.findFirst({
- where: linkedTaskScope(externalLink.taskId, integration.projectId),
- });
+ const readCurrent = async () => {
+ try {
+ const config = JSON.parse(integration.config) as GitHubConfig;
+ const octokit = await getVerifiedInstallationOctokit(config);
+ return (
+ await octokit.rest.issues.get({
+ owner: config.repositoryOwner,
+ repo: config.repositoryName,
+ issue_number: issue.number,
+ })
+ ).data;
+ } catch {
+ return issue;
+ }
+ };
+ await withEchoConfirmation(readCurrent, (current) =>
+ withIntegrationLink(
+ externalLink,
+ integration,
- async (db, _afterCommit, externalLink) => {
++ async (db, afterCommit, externalLink) => {
+ const task = await db.query.taskTable.findFirst({
+ where: linkedTaskScope(externalLink.taskId, integration.projectId),
+ });
+
+ if (!task) {
+ console.error(`Task ${externalLink.taskId} not found`);
+ return;
+ }
+ const metadata = parseLinkMetadata<IssueEditedMetadata>(
+ externalLink.metadata,
+ {
+ externalLinkId: externalLink.id,
+ source: "issue_edited",
+ },
+ );
+
+ const updateData: Record<string, unknown> = {};
+ const updatedMetadata: IssueEditedMetadata = { ...metadata };
- if (!task) {
- console.error(`Task ${externalLink.taskId} not found`);
- return;
- }
-
- const metadata = parseLinkMetadata<IssueEditedMetadata>(
- externalLink.metadata,
- {
- externalLinkId: externalLink.id,
- source: "issue_edited",
- },
- );
-
- const updateData: Record<string, unknown> = {};
- const updatedMetadata: IssueEditedMetadata = { ...metadata };
-
- if (!updatedMetadata.lastSync) {
- updatedMetadata.lastSync = {};
- }
-
- if (changes.title) {
- const lastTitleSync = metadata.lastSync?.title;
-
- let shouldUpdateTitle = true;
-
- if (lastTitleSync) {
- if (
- lastTitleSync.value === issue.title &&
- lastTitleSync.source === "kaneo"
- ) {
- console.log("Skipping title update - already synced from Kaneo");
- shouldUpdateTitle = false;
+ if (!updatedMetadata.lastSync) {
+ updatedMetadata.lastSync = {};
+ }
+
+ if (changes.title) {
+ const lastTitleSync = metadata.lastSync?.title;
+
+ let shouldUpdateTitle = true;
+
+ if (lastTitleSync) {
+ if (
+ inboundEcho(
+ lastTitleSync,
+ issue.title,
+ issue.updated_at,
+ current?.title,
+ )
+ ) {
+ console.log(
+ "Skipping title update - already synced from Kaneo",
+ );
+ shouldUpdateTitle = false;
+ }
}
- const timeSinceLastSync =
- Date.now() - new Date(lastTitleSync.timestamp ?? 0).getTime();
- if (timeSinceLastSync < 2000 && shouldUpdateTitle) {
+
+ if (shouldUpdateTitle) {
+ updateData.title = issue.title;
+ updatedMetadata.lastSync.title = {
+ outbound: metadata.lastSync?.title?.outbound,
+ timestamp: new Date().toISOString(),
+ source: "github",
+ value: issue.title,
+ };
console.log(
- `Skipping title update - recent sync detected (${timeSinceLastSync}ms ago)`,
+ `Updating task title from GitHub: "${changes.title.from}" → "${issue.title}"`,
);
- shouldUpdateTitle = false;
}
}
- if (shouldUpdateTitle) {
- updateData.title = issue.title;
- updatedMetadata.lastSync.title = {
- timestamp: new Date().toISOString(),
- source: "github",
- value: issue.title,
- };
- console.log(
- `Updating task title from GitHub: "${changes.title.from}" → "${issue.title}"`,
+ if (changes.body) {
+ const lastDescSync = metadata.lastSync?.description;
+ const formattedDescription = formatTaskDescriptionFromIssue(
+ issue.body,
+ task.id,
);
- }
- }
-
- if (changes.body) {
- const lastDescSync = metadata.lastSync?.description;
- const formattedDescription = formatTaskDescriptionFromIssue(
- issue.body,
- task.id,
- );
- let shouldUpdateDescription = true;
-
- if (lastDescSync) {
- if (
- lastDescSync.value === formattedDescription &&
- lastDescSync.source === "kaneo"
- ) {
- console.log(
- "Skipping description update - already synced from Kaneo",
- );
- shouldUpdateDescription = false;
- }
- const timeSinceLastSync =
- Date.now() - new Date(lastDescSync.timestamp ?? 0).getTime();
- if (timeSinceLastSync < 2000 && shouldUpdateDescription) {
- console.log(
- `Skipping description update - recent sync detected (${timeSinceLastSync}ms ago)`,
- );
- shouldUpdateDescription = false;
+ let shouldUpdateDescription = true;
+
+ if (lastDescSync) {
+ if (
+ inboundEcho(
+ lastDescSync,
+ formattedDescription,
+ issue.updated_at,
+ current
+ ? formatTaskDescriptionFromIssue(
+ current.body ?? null,
+ task.id,
+ )
+ : undefined,
+ )
+ ) {
+ console.log(
+ "Skipping description update - already synced from Kaneo",
+ );
+ shouldUpdateDescription = false;
+ }
}
- }
- if (shouldUpdateDescription) {
- updateData.description = formattedDescription;
- updatedMetadata.lastSync.description = {
- timestamp: new Date().toISOString(),
- source: "github",
- value: formattedDescription,
- };
- console.log("Updating task description from GitHub");
+ if (shouldUpdateDescription) {
+ updateData.description = formattedDescription;
+ updatedMetadata.lastSync.description = {
+ outbound: metadata.lastSync?.description?.outbound,
+ timestamp: new Date().toISOString(),
+ source: "github",
+ value: formattedDescription,
+ };
+ console.log("Updating task description from GitHub");
+ }
}
- }
-
- if (Object.keys(updateData).length > 0) {
- await db
- .update(taskTable)
- .set(updateData)
- .where(linkedTaskScope(task.id, integration.projectId));
- await updateExternalLink(
- externalLink.id,
- {
- title: issue.title,
- metadata: updatedMetadata,
- },
- db,
- );
- afterCommit(() =>
- publishEvent("task.updated", {
- projectId: integration.projectId,
- taskId: externalLink.taskId,
- }),
- );
+ if (Object.keys(updateData).length > 0) {
+ await db
+ .update(taskTable)
+ .set(updateData)
+ .where(linkedTaskScope(task.id, integration.projectId));
+
+ await updateExternalLink(
+ externalLink.id,
+ {
+ title: issue.title,
+ metadata: updatedMetadata,
+ },
+ db,
+ );
++ afterCommit(() =>
++ publishEvent("task.updated", {
++ projectId: integration.projectId,
++ taskId: externalLink.taskId,
++ }),
++ );
- console.log(
- `Synced ${Object.keys(updateData).join(", ")} from GitHub issue #${issue.number} to task ${task.id}`,
- );
- } else {
- console.log(
- `No updates needed for task ${task.id} from issue #${issue.number}`,
- );
- }
+ console.log(
+ `Synced ${Object.keys(updateData).join(", ")} from GitHub issue #${issue.number} to task ${task.id}`,
+ );
+ } else {
+ console.log(
+ `No updates needed for task ${task.id} from issue #${issue.number}`,
+ );
+ }
- return;
- },
+ return;
+ },
+ ),
);
}
}