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): announce committed PR links and reimported resources

Tin53c75ea88aad
diff --git a/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts b/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
index 007abb156..0d438466d 100644
--- a/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
+++ b/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
@@ -223,7 +223,7 @@ async function importSingleIssue(
const result = await withIntegrationTask(
existingLink.taskId,
{ id: integrationId, projectId, project: { workspaceId } },
- async (database) => {
+ async (database, afterCommit) => {
const [linked] = await database
.select({ id: externalLinkTable.id })
.from(externalLinkTable)
@@ -259,6 +259,17 @@ async function importSingleIssue(
await importCommentsForTask(comments, existingLink.taskId, database);
+ afterCommit(async () => {
+ for (const type of [
+ "task.updated",
+ "task.labels_updated",
+ "comment.updated",
+ ])
+ await publishEvent(type, {
+ projectId,
+ taskId: existingLink.taskId,
+ });
+ });
return "updated" as const;
},
);
@@ -520,4 +531,5 @@ async function linkPullRequestToTask(
author: pr.user?.login ?? pr.user?.username,
},
});
+ await publishEvent("task.updated", { projectId, taskId: task.id });
}
diff --git a/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts b/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
index eef47d7f3..c8db74d5b 100644
--- a/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
+++ b/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
@@ -199,7 +199,7 @@ async function importSingleIssue(
const result = await withIntegrationTask(
existingLink.taskId,
{ id: integrationId, projectId, project: { workspaceId } },
- async (database) => {
+ async (database, afterCommit) => {
const [linked] = await database
.select({ id: externalLinkTable.id })
.from(externalLinkTable)
@@ -234,6 +234,17 @@ async function importSingleIssue(
);
await importNotesForTask(issue, notes, existingLink.taskId, database);
+ afterCommit(async () => {
+ for (const type of [
+ "task.updated",
+ "task.labels_updated",
+ "comment.updated",
+ ])
+ await publishEvent(type, {
+ projectId,
+ taskId: existingLink.taskId,
+ });
+ });
return "updated" as const;
},
);
@@ -457,4 +468,5 @@ async function linkMergeRequestToTask(
author: mergeRequest.author?.username ?? mergeRequest.author?.name,
},
});
+ await publishEvent("task.updated", { projectId, taskId: task.id });
}
diff --git a/apps/api/src/plugins/gitea/webhooks/pull-request-closed.ts b/apps/api/src/plugins/gitea/webhooks/pull-request-closed.ts
index 04a2c52ce..4b9790f60 100644
--- a/apps/api/src/plugins/gitea/webhooks/pull-request-closed.ts
+++ b/apps/api/src/plugins/gitea/webhooks/pull-request-closed.ts
@@ -95,6 +95,13 @@ export async function handleGiteaPullRequestClosed(
database,
);
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: task.id,
+ }),
+ );
+
if (pull_request.merged) {
const allTaskPRs = await database.query.externalLinkTable.findMany({
where: and(
diff --git a/apps/api/src/plugins/gitea/webhooks/pull-request-opened.ts b/apps/api/src/plugins/gitea/webhooks/pull-request-opened.ts
index f07a5f066..4edff4679 100644
--- a/apps/api/src/plugins/gitea/webhooks/pull-request-opened.ts
+++ b/apps/api/src/plugins/gitea/webhooks/pull-request-opened.ts
@@ -135,6 +135,13 @@ export async function handleGiteaPullRequestOpened(
database,
);
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: task.id,
+ }),
+ );
+
const targetStatus = await resolveTargetStatus(
integration.projectId,
"pr_opened",
diff --git a/apps/api/src/plugins/github/webhooks/pull-request-closed.ts b/apps/api/src/plugins/github/webhooks/pull-request-closed.ts
index 97c974dc1..e059d1612 100644
--- a/apps/api/src/plugins/github/webhooks/pull-request-closed.ts
+++ b/apps/api/src/plugins/github/webhooks/pull-request-closed.ts
@@ -82,6 +82,13 @@ export async function handlePullRequestClosed(payload: PRClosedPayload) {
database,
);
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: task.id,
+ }),
+ );
+
if (pull_request.merged) {
const allTaskPRs = await database.query.externalLinkTable.findMany({
where: and(
diff --git a/apps/api/src/plugins/github/webhooks/pull-request-opened.ts b/apps/api/src/plugins/github/webhooks/pull-request-opened.ts
index c6654140f..ce3e362ad 100644
--- a/apps/api/src/plugins/github/webhooks/pull-request-opened.ts
+++ b/apps/api/src/plugins/github/webhooks/pull-request-opened.ts
@@ -109,6 +109,13 @@ export async function handlePullRequestOpened(payload: PROpenedPayload) {
database,
);
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: task.id,
+ }),
+ );
+
const targetStatus = await resolveTargetStatus(
integration.projectId,
"pr_opened",
diff --git a/apps/api/src/plugins/gitlab/webhooks/merge-request-closed.ts b/apps/api/src/plugins/gitlab/webhooks/merge-request-closed.ts
index 43804be54..db9bdb451 100644
--- a/apps/api/src/plugins/gitlab/webhooks/merge-request-closed.ts
+++ b/apps/api/src/plugins/gitlab/webhooks/merge-request-closed.ts
@@ -109,6 +109,13 @@ export async function handleGitlabMergeRequestClosed(
database,
);
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: task.id,
+ }),
+ );
+
if (!merged) {
return;
}
diff --git a/apps/api/src/plugins/gitlab/webhooks/merge-request-opened.ts b/apps/api/src/plugins/gitlab/webhooks/merge-request-opened.ts
index 89b538eaa..e616ae046 100644
--- a/apps/api/src/plugins/gitlab/webhooks/merge-request-opened.ts
+++ b/apps/api/src/plugins/gitlab/webhooks/merge-request-opened.ts
@@ -162,6 +162,13 @@ export async function handleGitlabMergeRequestOpened(
);
}
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: task.id,
+ }),
+ );
+
if (!moveTask) {
return;
}
diff --git a/tests/api-integration/integration-pr-task-ownership.test.ts b/tests/api-integration/integration-pr-task-ownership.test.ts
index 14be38458..bf6b5bab5 100644
--- a/tests/api-integration/integration-pr-task-ownership.test.ts
+++ b/tests/api-integration/integration-pr-task-ownership.test.ts
@@ -1,5 +1,6 @@
import { eq } from "drizzle-orm";
import { beforeEach, expect, it, vi } from "vite-plus/test";
+import { publishEvent } from "../../apps/api/src/events";
import db, { getDatabase, schema } from "../../apps/api/src/database";
import { handlePullRequestOpened } from "../../apps/api/src/plugins/github/webhooks/pull-request-opened";
import { handlePullRequestClosed } from "../../apps/api/src/plugins/github/webhooks/pull-request-closed";
@@ -17,19 +18,33 @@ import {
vi.mock("../../apps/api/src/events", () => ({
publishEvent: vi.fn(async () => undefined),
}));
-beforeEach(resetTestDatabase);
+beforeEach(async () => {
+ await resetTestDatabase();
+ vi.mocked(publishEvent).mockReset();
+});
const cases = ["github", "gitea", "gitlab"].flatMap((provider) =>
["opened", "merged"].flatMap((action) =>
["ordinary", "moved", "returned"].map((race) => ({
provider,
action,
race,
+ sameStatus: false,
+ })),
+ ),
+);
+cases.push(
+ ...["github", "gitea", "gitlab"].flatMap((provider) =>
+ ["opened", "merged", "closed"].map((action) => ({
+ provider,
+ action,
+ race: "ordinary",
+ sameStatus: action !== "closed",
})),
),
);
it.each(cases)(
"$provider $action webhook respects task ownership ($race)",
- async ({ provider, action, race }) => {
+ async ({ provider, action, race, sameStatus }) => {
const { user, workspace } = await createWorkspaceMember();
const { project } = await createProjectFixture({
workspaceId: workspace.id,
@@ -48,7 +63,11 @@ it.each(cases)(
projectId: project.id,
number: 1,
title: "card",
- status: "to-do",
+ status: sameStatus
+ ? action === "opened"
+ ? "in-progress"
+ : "done"
+ : "to-do",
position: 0,
})
.returning();
@@ -72,7 +91,7 @@ it.each(cases)(
}),
})
.returning();
- if (action === "merged")
+ if (action !== "opened")
await db.insert(schema.externalLinkTable).values({
taskId: task.id,
integrationId: integration.id,
@@ -117,12 +136,38 @@ it.each(cases)(
title: "change",
description: null,
url: "https://git.example/owner/repo/-/merge_requests/1",
- state: action === "opened" ? "opened" : "merged",
- action: action === "opened" ? "open" : "merge",
+ state:
+ action === "opened"
+ ? "opened"
+ : action === "merged"
+ ? "merged"
+ : "closed",
+ action:
+ action === "opened"
+ ? "open"
+ : action === "merged"
+ ? "merge"
+ : "close",
source_branch: "KAN-1",
draft: false,
},
};
+ vi.mocked(publishEvent).mockImplementation(async (type) => {
+ if (type !== "task.updated") return;
+ const committed = await db.query.externalLinkTable.findFirst({
+ where: eq(schema.externalLinkTable.taskId, task.id),
+ });
+ expect(committed).toBeDefined();
+ expect(JSON.parse(committed!.metadata!).state).toBe(
+ action === "opened"
+ ? provider === "gitlab"
+ ? "opened"
+ : "open"
+ : action === "merged" && provider === "gitlab"
+ ? "merged"
+ : "closed",
+ );
+ });
const transaction = getDatabase().transaction.bind(getDatabase());
const intercepted =
race === "ordinary"
@@ -169,7 +214,9 @@ it.each(cases)(
race === "ordinary"
? action === "opened"
? "in-progress"
- : "done"
+ : action === "merged"
+ ? "done"
+ : "to-do"
: "to-do",
);
expect(saved?.projectId).toBe(
@@ -179,6 +226,15 @@ it.each(cases)(
where: eq(schema.externalLinkTable.taskId, task.id),
});
expect(links).toHaveLength(race === "ordinary" ? 1 : 0);
+ expect(
+ vi
+ .mocked(publishEvent)
+ .mock.calls.filter(([type]) => type === "task.updated"),
+ ).toEqual(
+ race === "ordinary"
+ ? [["task.updated", { projectId: project.id, taskId: task.id }]]
+ : [],
+ );
} finally {
intercepted?.mockRestore();
}
diff --git a/tests/api-integration/integration-task-ownership.test.ts b/tests/api-integration/integration-task-ownership.test.ts
index 5b515b38f..0ce685d84 100644
--- a/tests/api-integration/integration-task-ownership.test.ts
+++ b/tests/api-integration/integration-task-ownership.test.ts
@@ -23,6 +23,7 @@ import {
} from "./helpers/fixtures";
const m = vi.hoisted(() => ({
+ publish: vi.fn(async (_type: string, _data: unknown) => undefined),
listIssues: vi.fn(),
listIssueComments: vi.fn(async () => []),
listIssueNotes: vi.fn(async () => []),
@@ -47,6 +48,10 @@ vi.mock(
createGitlabClient: () => m,
}),
);
+vi.mock("../../apps/api/src/events", async (original) => ({
+ ...(await original<typeof import("../../apps/api/src/events")>()),
+ publishEvent: m.publish,
+}));
const remoteIssue = {
number: 1,
iid: 1,
@@ -66,6 +71,7 @@ const repository = {
beforeEach(async () => {
await resetTestDatabase();
vi.clearAllMocks();
+ m.publish.mockReset().mockResolvedValue(undefined);
m.listIssues.mockResolvedValue([remoteIssue]);
m.listIssueComments.mockResolvedValue([]);
m.listIssueNotes.mockResolvedValue([]);
@@ -483,3 +489,39 @@ it.each(["edit", "labels", "comment"])(
}
},
);
+
+it.each(["gitea", "gitlab"])(
+ "announces committed %s reimport updates to task and resource caches",
+ async (provider) => {
+ const fixture = await setup(provider);
+ m.publish.mockImplementation(async (type, data) => {
+ if (
+ !["task.updated", "task.labels_updated", "comment.updated"].includes(
+ type,
+ )
+ )
+ return;
+ expect(data).toEqual({
+ projectId: fixture.project.id,
+ taskId: fixture.task.id,
+ });
+ expect(
+ await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, fixture.task.id),
+ }),
+ ).toMatchObject({
+ title: "Remote title",
+ description: "Remote description",
+ });
+ });
+ const result = await (
+ provider === "gitea" ? importGiteaIssues : importGitlabIssues
+ )(fixture.project.id);
+ expect(result).toMatchObject({ imported: 0, updated: 1 });
+ expect(m.publish.mock.calls.map(([type]) => type)).toEqual([
+ "task.updated",
+ "task.labels_updated",
+ "comment.updated",
+ ]);
+ },
+);