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.

fix(integrations): publish live task updates after committed provider writes

Tinca6e4f7e15ff
diff --git a/apps/api/src/github-integration/controllers/import-issues.ts b/apps/api/src/github-integration/controllers/import-issues.ts
index 5461791b7..1b1fb9dca 100644
--- a/apps/api/src/github-integration/controllers/import-issues.ts
+++ b/apps/api/src/github-integration/controllers/import-issues.ts
@@ -2,6 +2,7 @@ import { createId } from "@paralleldrive/cuid2";
import { and, asc, eq, isNull } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
+import { publishEvent } from "../../events";
import {
activityTable,
columnTable,
@@ -212,6 +213,9 @@ export async function importIssues(projectId: string, runId?: string) {
if (!saved) throw conflict();
return saved;
});
+ // Each bounded page is durable before other clients refresh, including
+ // continuation pages that change labels, comments or linked resources.
+ await publishEvent("project.updated", { projectId });
}
return importProgress(run.runId, run.state);
});
diff --git a/apps/api/src/plugins/gitea/webhooks/issue-edited.ts b/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
index 687c70e48..87c2d377b 100644
--- a/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
@@ -1,3 +1,4 @@
+import { publishEvent } from "../../../events";
import { withIntegrationLink } from "../../github/services/with-integration-link";
import { linkedTaskScope } from "../../github/services/integration-task-scope";
import { taskTable } from "../../../database/schema";
@@ -66,7 +67,7 @@ export async function handleGiteaIssueEdited(
await withIntegrationLink(
externalLink,
integration,
- async (db, _afterCommit, externalLink) => {
+ async (db, afterCommit, externalLink) => {
const task = await db.query.taskTable.findFirst({
where: linkedTaskScope(externalLink.taskId, integration.projectId),
});
@@ -161,6 +162,12 @@ export async function handleGiteaIssueEdited(
},
db,
);
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: externalLink.taskId,
+ }),
+ );
}
return;
diff --git a/apps/api/src/plugins/gitea/webhooks/issue-labeled.ts b/apps/api/src/plugins/gitea/webhooks/issue-labeled.ts
index 843da0fff..db17fdf43 100644
--- a/apps/api/src/plugins/gitea/webhooks/issue-labeled.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-labeled.ts
@@ -208,15 +208,35 @@ export async function handleGiteaIssueLabeled(
giteaLabelsForSync(issue.labels),
db,
);
+ afterCommit(() =>
+ publishEvent("task.labels_updated", {
+ projectId: integration.projectId,
+ taskId: existingLink.taskId,
+ }),
+ );
}
return;
}
if (!addedLabel) {
+ if (priority)
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: existingLink.taskId,
+ }),
+ );
return;
}
if (isSystemLabelName(addedLabel.name)) {
+ if (priority)
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: existingLink.taskId,
+ }),
+ );
return;
}
@@ -273,6 +293,12 @@ export async function handleGiteaIssueLabeled(
await db.delete(labelTable).where(eq(labelTable.id, label.id));
}
}
+ afterCommit(() =>
+ publishEvent("task.labels_updated", {
+ projectId: integration.projectId,
+ taskId: existingLink.taskId,
+ }),
+ );
},
);
} catch (error) {
diff --git a/apps/api/src/plugins/github/webhooks/issue-edited.ts b/apps/api/src/plugins/github/webhooks/issue-edited.ts
index 8ce77232f..4d40cc242 100644
--- a/apps/api/src/plugins/github/webhooks/issue-edited.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-edited.ts
@@ -1,3 +1,4 @@
+import { publishEvent } from "../../../events";
import { withIntegrationLink } from "../services/with-integration-link";
import { linkedTaskScope } from "../services/integration-task-scope";
import { taskTable } from "../../../database/schema";
@@ -72,7 +73,7 @@ export async function handleIssueEdited(payload: IssueEditedPayload) {
await withIntegrationLink(
externalLink,
integration,
- async (db, _afterCommit, externalLink) => {
+ async (db, afterCommit, externalLink) => {
const task = await db.query.taskTable.findFirst({
where: linkedTaskScope(externalLink.taskId, integration.projectId),
});
@@ -187,6 +188,12 @@ export async function handleIssueEdited(payload: IssueEditedPayload) {
},
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}`,
diff --git a/apps/api/src/plugins/github/webhooks/issue-labeled.ts b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
index 14a7fbe9f..c8a664550 100644
--- a/apps/api/src/plugins/github/webhooks/issue-labeled.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
@@ -87,6 +87,13 @@ export async function handleIssueLabeled(payload: IssueLabeledPayload) {
}
if (!addedLabel) {
+ if (priority)
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: existingLink.taskId,
+ }),
+ );
return;
}
@@ -95,6 +102,13 @@ export async function handleIssueLabeled(payload: IssueLabeledPayload) {
addedLabel.name.startsWith("status:");
if (isSystemLabel) {
+ if (priority)
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: existingLink.taskId,
+ }),
+ );
return;
}
@@ -149,6 +163,12 @@ export async function handleIssueLabeled(payload: IssueLabeledPayload) {
}
}
+ afterCommit(() =>
+ publishEvent("task.labels_updated", {
+ projectId: integration.projectId,
+ taskId: existingLink.taskId,
+ }),
+ );
return;
},
);
diff --git a/apps/api/src/plugins/github/webhooks/issue-opened.ts b/apps/api/src/plugins/github/webhooks/issue-opened.ts
index 02029446d..07f4c1a5d 100644
--- a/apps/api/src/plugins/github/webhooks/issue-opened.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-opened.ts
@@ -1,5 +1,6 @@
import { and, eq } from "drizzle-orm";
import db from "../../../database";
+import { publishEvent } from "../../../events";
import {
columnTable,
integrationTable,
@@ -130,6 +131,7 @@ export async function handleIssueOpened(payload: IssueOpenedPayload) {
return task;
});
if (!createdTask) continue;
+ await publishEvent("task.created", { projectId, taskId: createdTask.id });
const project = await db.query.projectTable.findFirst({
where: eq(projectTable.id, projectId),
diff --git a/apps/api/src/plugins/gitlab/webhooks/issue-updated.ts b/apps/api/src/plugins/gitlab/webhooks/issue-updated.ts
index 61860cd6d..3f1c22072 100644
--- a/apps/api/src/plugins/gitlab/webhooks/issue-updated.ts
+++ b/apps/api/src/plugins/gitlab/webhooks/issue-updated.ts
@@ -231,6 +231,12 @@ export async function handleGitlabIssueUpdated(
},
db,
);
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: task.id,
+ }),
+ );
}
}
@@ -284,6 +290,12 @@ export async function handleGitlabIssueUpdated(
changes?.labels?.previous,
db,
);
+ afterCommit(() =>
+ publishEvent("task.labels_updated", {
+ projectId: integration.projectId,
+ taskId: task.id,
+ }),
+ );
}
},
);
diff --git a/apps/api/src/ws/index.ts b/apps/api/src/ws/index.ts
index 414db106c..3f599b36a 100644
--- a/apps/api/src/ws/index.ts
+++ b/apps/api/src/ws/index.ts
@@ -381,6 +381,7 @@ const taskUpdateEvents = [
"task.label_assigned",
"task.label_unassigned",
"task.label_created",
+ "task.labels_updated",
"task.label_deleted",
"task-relation.created",
"task-relation.deleted",
@@ -500,6 +501,7 @@ for (const eventName of taskUpdateEvents) {
case "task.label_assigned":
case "task.label_unassigned":
case "task.label_created":
+ case "task.labels_updated":
case "task.label_deleted":
type = "TASK_LABEL_UPDATED";
break;
diff --git a/tests/api-integration/github-import-bounds.test.ts b/tests/api-integration/github-import-bounds.test.ts
index 12a107da5..4acd1b6e3 100644
--- a/tests/api-integration/github-import-bounds.test.ts
+++ b/tests/api-integration/github-import-bounds.test.ts
@@ -18,6 +18,10 @@ const mocks = vi.hoisted(() => ({
graphql: vi.fn(),
verify: vi.fn(),
comment: vi.fn(),
+ publish: vi.fn(
+ async (_name: string, _payload: { projectId: string; taskId?: string }) =>
+ undefined,
+ ),
}));
vi.mock("../../apps/api/src/plugins/github/utils/github-app", () => ({
getVerifiedInstallationOctokit: mocks.verify,
@@ -27,6 +31,10 @@ vi.mock("../../apps/api/src/plugins/github/utils/github-app", () => ({
}),
}),
}));
+vi.mock("../../apps/api/src/events", async (original) => ({
+ ...(await original<typeof import("../../apps/api/src/events")>()),
+ publishEvent: mocks.publish,
+}));
const old = "2020-01-01T00:00:00Z";
function connection<T>(
nodes: T[],
@@ -107,6 +115,7 @@ beforeEach(async () => {
mocks.graphql.mockReset();
mocks.verify.mockReset().mockResolvedValue({ graphql: mocks.graphql });
mocks.comment.mockReset();
+ mocks.publish.mockReset().mockResolvedValue(undefined);
});
async function setup() {
const member = await createWorkspaceMember({ role: "admin" });
@@ -152,6 +161,12 @@ describe("bounded resumable GitHub import", () => {
it("bounds each HTTP step, persists resumable state, imports every issue and retries completion without restarting", async () => {
const { request, app, project } = await setup();
serveIssues(9);
+ const observedCounts: number[] = [];
+ mocks.publish.mockImplementation(async (name, payload) => {
+ expect(name).toBe("project.updated");
+ expect(payload).toEqual({ projectId: project.id });
+ observedCounts.push((await db.query.taskTable.findMany()).length);
+ });
const first = await request();
expect(first.status).toBe(202);
const progress = await first.json();
@@ -161,6 +176,7 @@ describe("bounded resumable GitHub import", () => {
updated: 0,
});
expect(mocks.graphql).toHaveBeenCalledTimes(IMPORT_PAGES_PER_REQUEST);
+ expect(observedCounts).toEqual([1, 2, 3, 4]);
const info = await app.request(
`/api/github-integration/project/${project.id}`,
);
@@ -593,6 +609,40 @@ describe("bounded resumable GitHub import", () => {
expect(mocks.comment).not.toHaveBeenCalled();
});
+ it("announces a webhook task only after its task and link commit", async () => {
+ const { project } = await setup();
+ mocks.publish.mockImplementation(async (name, payload) => {
+ expect(name).toBe("task.created");
+ expect(payload.projectId).toBe(project.id);
+ const task = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, payload.taskId!),
+ });
+ const link = await db.query.externalLinkTable.findFirst({
+ where: eq(schema.externalLinkTable.taskId, payload.taskId!),
+ });
+ expect(task?.title).toBe("Live issue");
+ expect(link?.externalId).toBe("77");
+ });
+ await handleIssueOpened({
+ action: "opened",
+ installation: { id: 1 },
+ repository: {
+ id: 2,
+ owner: { login: "example" },
+ name: "repo",
+ full_name: "example/repo",
+ },
+ issue: {
+ number: 77,
+ title: "Live issue",
+ body: "Description",
+ html_url: "https://github.com/example/repo/issues/77",
+ user: null,
+ },
+ });
+ expect(mocks.publish).toHaveBeenCalledTimes(1);
+ });
+
it("continues all pull request pages and links only matching tasks in this project", async () => {
const { project, integration } = await setup();
mocks.graphql.mockImplementation(
diff --git a/tests/api-integration/provider-board-live-updates.test.ts b/tests/api-integration/provider-board-live-updates.test.ts
new file mode 100644
index 000000000..053d620ed
--- /dev/null
+++ b/tests/api-integration/provider-board-live-updates.test.ts
@@ -0,0 +1,220 @@
+import { beforeEach, expect, it, vi } from "vite-plus/test";
+import { eq } from "drizzle-orm";
+import db, { schema } from "../../apps/api/src/database";
+import { handleIssueEdited } from "../../apps/api/src/plugins/github/webhooks/issue-edited";
+import { handleIssueLabeled } from "../../apps/api/src/plugins/github/webhooks/issue-labeled";
+import { handleGiteaIssueEdited } from "../../apps/api/src/plugins/gitea/webhooks/issue-edited";
+import { handleGiteaIssueLabeled } from "../../apps/api/src/plugins/gitea/webhooks/issue-labeled";
+import { handleGitlabIssueUpdated } from "../../apps/api/src/plugins/gitlab/webhooks/issue-updated";
+import { resetTestDatabase } from "./helpers/database";
+import {
+ createWorkspaceMember,
+ createProjectFixture,
+} from "./helpers/fixtures";
+const m = vi.hoisted(() => ({
+ integrations: [] as unknown[],
+ publish: vi.fn(
+ async (
+ _name: string,
+ _payload: { projectId: string; taskId: string },
+ ) => {},
+ ),
+}));
+vi.mock("../../apps/api/src/events", () => ({ publishEvent: m.publish }));
+vi.mock(
+ "../../apps/api/src/plugins/github/services/task-service",
+ async (original) => ({
+ ...(await original<
+ typeof import("../../apps/api/src/plugins/github/services/task-service")
+ >()),
+ findAllIntegrationsByRepo: async () => m.integrations,
+ }),
+);
+vi.mock(
+ "../../apps/api/src/plugins/gitea/services/integration-lookup",
+ async (original) => ({
+ ...(await original<
+ typeof import("../../apps/api/src/plugins/gitea/services/integration-lookup")
+ >()),
+ findAllIntegrationsByGiteaRepo: async () => m.integrations,
+ }),
+);
+vi.mock(
+ "../../apps/api/src/plugins/gitlab/services/integration-lookup",
+ async (original) => ({
+ ...(await original<
+ typeof import("../../apps/api/src/plugins/gitlab/services/integration-lookup")
+ >()),
+ findAllIntegrationsByGitlabProject: async () => m.integrations,
+ }),
+);
+beforeEach(async () => {
+ await resetTestDatabase();
+ m.publish.mockReset();
+});
+it.each([
+ ["github", "text"],
+ ["gitea", "text"],
+ ["gitlab", "text"],
+ ["github", "priority"],
+ ["gitea", "priority"],
+ ["gitlab", "priority"],
+ ["github", "label"],
+ ["gitea", "label"],
+ ["gitlab", "label"],
+ ["github", "label-delete"],
+ ["gitea", "label-delete"],
+ ["gitlab", "label-delete"],
+])(
+ "announces committed %s %s edits to observing boards",
+ async (provider, kind) => {
+ const { workspace } = await createWorkspaceMember();
+ const { project } = await createProjectFixture({
+ workspaceId: workspace.id,
+ });
+ const [task] = await db
+ .insert(schema.taskTable)
+ .values({
+ projectId: project.id,
+ number: 1,
+ title: "Before",
+ description: "Before body",
+ priority: "low",
+ })
+ .returning();
+ const [integration] = await db
+ .insert(schema.integrationTable)
+ .values({
+ projectId: project.id,
+ type: provider,
+ config: "{}",
+ isActive: true,
+ })
+ .returning();
+ m.integrations = [integration];
+ await db.insert(schema.externalLinkTable).values({
+ taskId: task.id,
+ integrationId: integration.id,
+ resourceType: "issue",
+ externalId: "1",
+ url: "https://provider.example/issues/1",
+ metadata: "{}",
+ });
+ if (kind === "label-delete")
+ await db.insert(schema.labelTable).values({
+ taskId: task.id,
+ workspaceId: workspace.id,
+ name: "bug",
+ color: "#ff0000",
+ });
+ m.publish.mockImplementation(async () => {
+ const committed = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ if (kind === "text") {
+ expect(committed?.title).toBe("After");
+ expect(committed?.description).toBe("After body");
+ } else if (kind === "priority") expect(committed?.priority).toBe("high");
+ else {
+ const labels = await db.query.labelTable.findMany({
+ where: eq(schema.labelTable.taskId, task.id),
+ });
+ expect(labels.map((label) => label.name)).toEqual(
+ kind === "label-delete" ? [] : ["bug"],
+ );
+ }
+ });
+ const issue = {
+ number: 1,
+ title: "After",
+ body: "After body",
+ html_url: "https://gitea.example/owner/repo/issues/1",
+ };
+ const repository = {
+ id: 2,
+ name: "repo",
+ full_name: "owner/repo",
+ owner: { login: "owner" },
+ html_url: "https://gitea.example/owner/repo",
+ };
+ if (provider === "github" || provider === "gitea") {
+ if (kind === "text") {
+ const payload = {
+ action: "edited",
+ issue,
+ repository,
+ changes: { title: { from: "Before" }, body: { from: "Before body" } },
+ };
+ if (provider === "github") await handleIssueEdited(payload);
+ else await handleGiteaIssueEdited(payload);
+ } else {
+ const label = {
+ name: kind === "priority" ? "priority:high" : "bug",
+ color: "ff0000",
+ };
+ const payload = {
+ action: kind === "label-delete" ? "unlabeled" : "labeled",
+ issue: { ...issue, labels: kind === "label-delete" ? [] : [label] },
+ label,
+ repository,
+ };
+ if (provider === "github") await handleIssueLabeled(payload);
+ else await handleGiteaIssueLabeled(payload);
+ }
+ } else {
+ const label = {
+ title: kind === "priority" ? "priority:high" : "bug",
+ color: "#ff0000",
+ };
+ await handleGitlabIssueUpdated({
+ object_attributes: {
+ iid: 1,
+ title: "After",
+ description: "After body",
+ url: "https://gitlab.example/owner/repo/-/issues/1",
+ action: "update",
+ },
+ changes:
+ kind === "text"
+ ? {
+ title: { previous: "Before", current: "After" },
+ description: { previous: "Before body", current: "After body" },
+ }
+ : {
+ labels: {
+ previous: kind === "label-delete" ? [label] : [],
+ current: kind === "label-delete" ? [] : [label],
+ },
+ },
+ project: {
+ name: "repo",
+ web_url: "https://gitlab.example/owner/repo",
+ path_with_namespace: "owner/repo",
+ },
+ });
+ }
+ const saved = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ if (kind === "text") {
+ expect(saved?.title).toBe("After");
+ expect(saved?.description).toBe("After body");
+ } else if (kind === "priority") expect(saved?.priority).toBe("high");
+ else {
+ const labels = await db.query.labelTable.findMany({
+ where: eq(schema.labelTable.taskId, task.id),
+ });
+ expect(labels.map((label) => label.name)).toEqual(
+ kind === "label-delete" ? [] : ["bug"],
+ );
+ }
+ expect(m.publish.mock.calls).toEqual([
+ [
+ kind === "text" || (kind === "priority" && provider !== "gitlab")
+ ? "task.updated"
+ : "task.labels_updated",
+ { projectId: project.id, taskId: task.id },
+ ],
+ ]);
+ },
+);