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): preserve rules and canonical export labels

Tincdb7a6a59d35
diff --git a/apps/api/src/gitea-integration/controllers/create-gitea-integration.ts b/apps/api/src/gitea-integration/controllers/create-gitea-integration.ts
index 519f13968..ab5acb211 100644
--- a/apps/api/src/gitea-integration/controllers/create-gitea-integration.ts
+++ b/apps/api/src/gitea-integration/controllers/create-gitea-integration.ts
@@ -152,15 +152,15 @@ async function createGiteaIntegration({
})
.where(
and(
- eq(integrationTable.projectId, projectId),
- eq(integrationTable.type, "gitea"),
+ eq(integrationTable.id, existingIntegration.id),
+ eq(integrationTable.config, existingIntegration.config),
),
)
.returning();
if (!updated) {
- throw new HTTPException(500, {
- message: "Failed to update Gitea integration",
+ throw new HTTPException(409, {
+ message: "Gitea integration changed; refresh before reconnecting",
});
}
diff --git a/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.ts b/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.ts
index 56f1a32ec..a55407ea0 100644
--- a/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.ts
+++ b/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.ts
@@ -190,15 +190,15 @@ async function createGitlabIntegration({
})
.where(
and(
- eq(integrationTable.projectId, projectId),
- eq(integrationTable.type, "gitlab"),
+ eq(integrationTable.id, existingIntegration.id),
+ eq(integrationTable.config, existingIntegration.config),
),
)
.returning();
if (!updated) {
- throw new HTTPException(500, {
- message: "Failed to update GitLab integration",
+ throw new HTTPException(409, {
+ message: "GitLab integration changed; refresh before reconnecting",
});
}
diff --git a/apps/api/src/plugins/sync/issue-labels.ts b/apps/api/src/plugins/sync/issue-labels.ts
index c3676de2f..0cd374129 100644
--- a/apps/api/src/plugins/sync/issue-labels.ts
+++ b/apps/api/src/plugins/sync/issue-labels.ts
@@ -17,7 +17,12 @@ export async function taskIssueLabels(
return [
...new Set([
...getLabelsForIssue(priority, status),
- ...assigned.map((label) => label.name),
+ ...assigned
+ .map((label) => label.name)
+ .filter(
+ (name) =>
+ !name.startsWith("status:") && !name.startsWith("priority:"),
+ ),
]),
];
}
diff --git a/tests/api-integration/integration-sync-providers.test.ts b/tests/api-integration/integration-sync-providers.test.ts
index d89502c5e..3057a8a17 100644
--- a/tests/api-integration/integration-sync-providers.test.ts
+++ b/tests/api-integration/integration-sync-providers.test.ts
@@ -194,12 +194,23 @@ describe.each(["github", "gitea", "gitlab"] as const)(
await reconcileProjectSync(f.project.id, f.integration.id);
expect(create).not.toHaveBeenCalled();
await f.assign();
+ await db.insert(schema.labelTable).values(
+ ["status:done", "priority:high"].map((name) => ({
+ taskId: f.task.id,
+ workspaceId: f.workspace.id,
+ name,
+ color: "#123456",
+ })),
+ );
await Promise.all([
reconcileTaskSync(f.project.id, f.task.id),
reconcileProjectSync(f.project.id, f.integration.id),
]);
expect(create).toHaveBeenCalledOnce();
expect(mocks.labels.mock.calls[0]!.at(-1)).toContain("export");
+ expect(mocks.labels.mock.calls[0]!.at(-1)).toContain("status:to-do");
+ expect(mocks.labels.mock.calls[0]!.at(-1)).not.toContain("status:done");
+ expect(mocks.labels.mock.calls[0]!.at(-1)).not.toContain("priority:high");
expect(
await db.query.externalLinkTable.findMany({
where: eq(schema.externalLinkTable.taskId, f.task.id),
diff --git a/tests/api-integration/integration-sync-reconnect.test.ts b/tests/api-integration/integration-sync-reconnect.test.ts
new file mode 100644
index 000000000..f0e63b48a
--- /dev/null
+++ b/tests/api-integration/integration-sync-reconnect.test.ts
@@ -0,0 +1,119 @@
+import { eq } from "drizzle-orm";
+import { beforeEach, expect, it, vi } from "vite-plus/test";
+import db, { schema } from "../../apps/api/src/database";
+import * as events from "../../apps/api/src/events";
+import createGiteaIntegration from "../../apps/api/src/gitea-integration/controllers/create-gitea-integration";
+import createGitlabIntegration from "../../apps/api/src/gitlab-integration/controllers/create-gitlab-integration";
+import { getSyncIntegration } from "../../apps/api/src/integration-sync/controllers/get-integration";
+import { previewSyncRules } from "../../apps/api/src/integration-sync/controllers/preview-rules";
+import { saveSyncRules } from "../../apps/api/src/integration-sync/controllers/save-rules";
+import {
+ defaultSyncRules,
+ type SyncRules,
+} from "../../apps/api/src/plugins/sync/rules";
+import { resetTestDatabase } from "./helpers/database";
+import {
+ createProjectFixture,
+ createWorkspaceMember,
+} from "./helpers/fixtures";
+
+const verify = vi.hoisted(() => vi.fn());
+vi.mock("../../apps/api/src/plugins/gitea/utils/gitea-api", () => ({
+ GiteaApiError: class extends Error {},
+ verifyGiteaToken: verify,
+ createGiteaClient: () => ({ getRepo: async () => ({}) }),
+}));
+vi.mock("../../apps/api/src/plugins/gitlab/utils/gitlab-api", () => ({
+ GitlabApiError: class extends Error {},
+ verifyGitlabToken: verify,
+ createGitlabClient: () => ({ getProject: async () => ({}) }),
+}));
+beforeEach(async () => {
+ await resetTestDatabase();
+ verify.mockReset().mockResolvedValue({ id: 1 });
+ vi.spyOn(events, "publishEvent").mockResolvedValue(undefined);
+});
+
+it.each(["gitea", "gitlab"] as const)(
+ "%s reconnect cannot overwrite rules saved during verification",
+ async (type) => {
+ const { workspace } = await createWorkspaceMember();
+ const { project } = await createProjectFixture({
+ workspaceId: workspace.id,
+ });
+ const base = {
+ baseUrl: "https://git.example",
+ accessToken: "test-only",
+ webhookSecret: "test-hook",
+ };
+ const [integration] = await db
+ .insert(schema.integrationTable)
+ .values({
+ projectId: project.id,
+ type,
+ isActive: true,
+ config: JSON.stringify({
+ ...base,
+ repositoryOwner: "team",
+ repositoryName: "repo",
+ projectPath: "team/repo",
+ syncRules: defaultSyncRules,
+ }),
+ })
+ .returning();
+ const reconnect = () =>
+ type === "gitea"
+ ? createGiteaIntegration({
+ ...base,
+ projectId: project.id,
+ repositoryOwner: "team",
+ repositoryName: "repo",
+ })
+ : createGitlabIntegration({
+ ...base,
+ projectId: project.id,
+ tokenType: "private",
+ projectPath: "team/repo",
+ });
+ let release!: () => void;
+ const gate = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ verify.mockImplementationOnce(async () => {
+ await gate;
+ return { id: 1 };
+ });
+ const pending = reconnect();
+ const conflict = expect(pending).rejects.toMatchObject({ status: 409 });
+ const rules: SyncRules = {
+ ...defaultSyncRules,
+ incoming: { mode: "labels", match: "all", labels: ["ready"] },
+ };
+ try {
+ await vi.waitFor(() => expect(verify).toHaveBeenCalledOnce());
+ const preview = await previewSyncRules(
+ await getSyncIntegration(project.id, type),
+ rules,
+ );
+ await saveSyncRules(project.id, type, rules, preview.previewToken);
+ } finally {
+ release();
+ await conflict;
+ }
+ expect(
+ JSON.parse(
+ (await db.query.integrationTable.findFirst({
+ where: eq(schema.integrationTable.id, integration!.id),
+ }))!.config,
+ ).syncRules,
+ ).toEqual(rules);
+ await reconnect();
+ expect(
+ JSON.parse(
+ (await db.query.integrationTable.findFirst({
+ where: eq(schema.integrationTable.id, integration!.id),
+ }))!.config,
+ ).syncRules,
+ ).toEqual(rules);
+ },
+);