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(api): gitlab mr now marked as draft

zaralXc5253b5011e3
diff --git a/apps/api/src/plugins/gitlab/utils/verify-token.ts b/apps/api/src/plugins/gitlab/utils/verify-token.ts
index 9e258c41e..b31b9bfd2 100644
--- a/apps/api/src/plugins/gitlab/utils/verify-token.ts
+++ b/apps/api/src/plugins/gitlab/utils/verify-token.ts
@@ -5,7 +5,7 @@ import { timingSafeEqual } from "node:crypto";
// comparing that token. GitLab 19.0 added a Standard Webhooks signing token in
// `webhook-signature`, but that secret is generated by GitLab and pasted into
// Kaneo, which is the opposite direction from the secret set up here.
-export function verifyGitlabToken(
+export function verifyWebhookToken(
secret: string,
tokenHeader: string | undefined,
): boolean {
diff --git a/apps/api/src/plugins/gitlab/webhook-handler.ts b/apps/api/src/plugins/gitlab/webhook-handler.ts
index 12f318b3e..9bd91b6bd 100644
--- a/apps/api/src/plugins/gitlab/webhook-handler.ts
+++ b/apps/api/src/plugins/gitlab/webhook-handler.ts
@@ -2,7 +2,7 @@ import { eq } from "drizzle-orm";
import db from "../../database";
import { integrationTable } from "../../database/schema";
import type { GitlabConfig } from "./config";
-import { verifyGitlabToken } from "./utils/verify-token";
+import { verifyWebhookToken } from "./utils/verify-token";
import { handleGitlabIssueClosed } from "./webhooks/issue-closed";
import { handleGitlabIssueOpened } from "./webhooks/issue-opened";
import { handleGitlabIssueReopened } from "./webhooks/issue-reopened";
@@ -69,13 +69,15 @@ function objectAction(payload: Record<string, unknown>): string | undefined {
return typeof attributes.action === "string" ? attributes.action : undefined;
}
-/** True when an "update" took the merge request out of draft. */
-function leftDraft(payload: Record<string, unknown>): boolean {
+/** Which way an "update" moved the draft flag, or null when it did not. */
+function draftChange(
+ payload: Record<string, unknown>,
+): "entered" | "left" | null {
const changes = payload.changes;
- if (!isRecord(changes)) return false;
+ if (!isRecord(changes)) return null;
const draft = changes.draft;
- if (!isRecord(draft)) return false;
- return draft.previous === true && draft.current === false;
+ if (!isRecord(draft) || draft.previous === draft.current) return null;
+ return draft.current === true ? "entered" : "left";
}
export async function handleGitlabWebhookRequest(
@@ -103,7 +105,7 @@ export async function handleGitlabWebhookRequest(
return { success: false, error: "Webhook secret not configured" };
}
- if (!verifyGitlabToken(secret, tokenHeader)) {
+ if (!verifyWebhookToken(secret, tokenHeader)) {
return { success: false, error: "Invalid webhook token" };
}
@@ -187,8 +189,13 @@ async function dispatchGitlabEvent(
await handleGitlabMergeRequestOpened(payload, integrationId);
return;
}
- if (action === "update" && leftDraft(payload)) {
- await handleGitlabMergeRequestOpened(payload, integrationId);
+ if (action === "update") {
+ const change = draftChange(payload);
+ if (change) {
+ await handleGitlabMergeRequestOpened(payload, integrationId, {
+ moveTask: change === "left",
+ });
+ }
return;
}
if (action === "merge" || action === "close") {
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 b76a43245..d6e36a58a 100644
--- a/apps/api/src/plugins/gitlab/webhooks/merge-request-opened.ts
+++ b/apps/api/src/plugins/gitlab/webhooks/merge-request-opened.ts
@@ -36,6 +36,7 @@ type MergeRequestOpenedPayload = {
export async function handleGitlabMergeRequestOpened(
payload: MergeRequestOpenedPayload,
integrationId?: string,
+ { moveTask = true }: { moveTask?: boolean } = {},
) {
const mergeRequest = payload.object_attributes;
const { project } = payload;
@@ -123,6 +124,10 @@ export async function handleGitlabMergeRequestOpened(
});
}
+ if (!moveTask) {
+ return;
+ }
+
// A reopen arrives with the link already recorded, and the task still needs
// to move back out of whatever state closing it left behind.
const targetStatus = await resolveTargetStatus(
diff --git a/tests/api/plugins/gitlab/utils/verify-token.test.ts b/tests/api/plugins/gitlab/utils/verify-token.test.ts
index ec33a5d7b..a85b39ae7 100644
--- a/tests/api/plugins/gitlab/utils/verify-token.test.ts
+++ b/tests/api/plugins/gitlab/utils/verify-token.test.ts
@@ -1,29 +1,29 @@
import { describe, expect, it } from "vitest";
-import { verifyGitlabToken } from "../../../../../apps/api/src/plugins/gitlab/utils/verify-token";
+import { verifyWebhookToken } from "../../../../../apps/api/src/plugins/gitlab/utils/verify-token";
-describe("verifyGitlabToken", () => {
+describe("verifyWebhookToken", () => {
const secret = "3f7a9c2e5b1d8046af32be7190c4d5e6";
it("accepts the exact token GitLab echoes back", () => {
- expect(verifyGitlabToken(secret, secret)).toBe(true);
+ expect(verifyWebhookToken(secret, secret)).toBe(true);
});
it("rejects a different token of the same length", () => {
const other = `${secret.slice(0, -1)}0`;
expect(other).toHaveLength(secret.length);
- expect(verifyGitlabToken(secret, other)).toBe(false);
+ expect(verifyWebhookToken(secret, other)).toBe(false);
});
it("rejects a token that only shares a prefix", () => {
- expect(verifyGitlabToken(secret, secret.slice(0, 8))).toBe(false);
+ expect(verifyWebhookToken(secret, secret.slice(0, 8))).toBe(false);
});
it("rejects a missing header", () => {
- expect(verifyGitlabToken(secret, undefined)).toBe(false);
- expect(verifyGitlabToken(secret, "")).toBe(false);
+ expect(verifyWebhookToken(secret, undefined)).toBe(false);
+ expect(verifyWebhookToken(secret, "")).toBe(false);
});
it("rejects every token when no secret is configured", () => {
- expect(verifyGitlabToken("", "anything")).toBe(false);
+ expect(verifyWebhookToken("", "anything")).toBe(false);
});
});
diff --git a/tests/api/plugins/gitlab/webhook-handler.test.ts b/tests/api/plugins/gitlab/webhook-handler.test.ts
new file mode 100644
index 000000000..28a309727
--- /dev/null
+++ b/tests/api/plugins/gitlab/webhook-handler.test.ts
@@ -0,0 +1,119 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { handleGitlabWebhookRequest } from "../../../../apps/api/src/plugins/gitlab/webhook-handler";
+
+const mocks = vi.hoisted(() => ({
+ integrationFindFirst: vi.fn(),
+ handleGitlabMergeRequestOpened: vi.fn(),
+ handleGitlabMergeRequestClosed: vi.fn(),
+}));
+
+vi.mock("../../../../apps/api/src/database", () => ({
+ default: {
+ query: {
+ integrationTable: {
+ findFirst: (...args: unknown[]) => mocks.integrationFindFirst(...args),
+ },
+ },
+ },
+}));
+
+vi.mock(
+ "../../../../apps/api/src/plugins/gitlab/webhooks/merge-request-opened",
+ () => ({
+ handleGitlabMergeRequestOpened: (...args: unknown[]) =>
+ mocks.handleGitlabMergeRequestOpened(...args),
+ }),
+);
+
+vi.mock(
+ "../../../../apps/api/src/plugins/gitlab/webhooks/merge-request-closed",
+ () => ({
+ handleGitlabMergeRequestClosed: (...args: unknown[]) =>
+ mocks.handleGitlabMergeRequestClosed(...args),
+ }),
+);
+
+const secret = "webhook-secret";
+
+function mergeRequestUpdate(changes: Record<string, unknown>) {
+ return JSON.stringify({
+ object_kind: "merge_request",
+ object_attributes: {
+ iid: 1,
+ title: "Fix the Safari login button",
+ description: null,
+ url: "https://gitlab.com/acme/web/-/merge_requests/1",
+ state: "opened",
+ action: "update",
+ source_branch: "kan-1",
+ },
+ changes,
+ project: {
+ name: "web",
+ web_url: "https://gitlab.com/acme/web",
+ path_with_namespace: "acme/web",
+ },
+ });
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.integrationFindFirst.mockResolvedValue({
+ id: "integration-1",
+ type: "gitlab",
+ config: JSON.stringify({ webhookSecret: secret }),
+ });
+});
+
+describe("handleGitlabWebhookRequest merge request updates", () => {
+ it("refreshes the link without moving the task when a merge request becomes a draft", async () => {
+ const result = await handleGitlabWebhookRequest(
+ "integration-1",
+ mergeRequestUpdate({ draft: { previous: false, current: true } }),
+ secret,
+ );
+
+ expect(result.success).toBe(true);
+ expect(mocks.handleGitlabMergeRequestOpened).toHaveBeenCalledWith(
+ expect.anything(),
+ "integration-1",
+ { moveTask: false },
+ );
+ });
+
+ it("moves the task when a merge request is taken out of draft", async () => {
+ await handleGitlabWebhookRequest(
+ "integration-1",
+ mergeRequestUpdate({ draft: { previous: true, current: false } }),
+ secret,
+ );
+
+ expect(mocks.handleGitlabMergeRequestOpened).toHaveBeenCalledWith(
+ expect.anything(),
+ "integration-1",
+ { moveTask: true },
+ );
+ });
+
+ it("ignores an update that does not touch the draft flag", async () => {
+ await handleGitlabWebhookRequest(
+ "integration-1",
+ mergeRequestUpdate({ title: { previous: "a", current: "b" } }),
+ secret,
+ );
+
+ expect(mocks.handleGitlabMergeRequestOpened).not.toHaveBeenCalled();
+ expect(mocks.handleGitlabMergeRequestClosed).not.toHaveBeenCalled();
+ });
+
+ it("rejects a delivery whose token does not match", async () => {
+ const result = await handleGitlabWebhookRequest(
+ "integration-1",
+ mergeRequestUpdate({ draft: { previous: false, current: true } }),
+ "wrong-secret",
+ );
+
+ expect(result).toEqual({ success: false, error: "Invalid webhook token" });
+ expect(mocks.handleGitlabMergeRequestOpened).not.toHaveBeenCalled();
+ });
+});