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): stop disabled gitlab integration from syncing labels

zaralXc6848cd55cc2
diff --git a/tests/api/plugins/gitlab/utils/sync-label-to-gitlab.test.ts b/tests/api/plugins/gitlab/utils/sync-label-to-gitlab.test.ts
new file mode 100644
index 000000000..efc2479e0
--- /dev/null
+++ b/tests/api/plugins/gitlab/utils/sync-label-to-gitlab.test.ts
@@ -0,0 +1,79 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import {
+ removeLabelFromGitlab,
+ syncLabelToGitlab,
+} from "../../../../../apps/api/src/plugins/gitlab/utils/sync-label-to-gitlab";
+
+const mocks = vi.hoisted(() => ({
+ externalLinkFindMany: vi.fn(),
+ createGitlabClient: vi.fn(),
+ client: {
+ listLabels: vi.fn(),
+ createLabel: vi.fn(),
+ getIssue: vi.fn(),
+ updateIssue: vi.fn(),
+ },
+}));
+
+vi.mock("../../../../../apps/api/src/database", () => ({
+ default: {
+ query: {
+ externalLinkTable: {
+ findMany: (...args: unknown[]) => mocks.externalLinkFindMany(...args),
+ },
+ },
+ },
+}));
+
+vi.mock("../../../../../apps/api/src/plugins/gitlab/utils/gitlab-api", () => ({
+ createGitlabClient: (...args: unknown[]) => mocks.createGitlabClient(...args),
+}));
+
+function issueLink(isActive: boolean | null) {
+ return {
+ id: "link-1",
+ taskId: "task-1",
+ resourceType: "issue",
+ externalId: "3",
+ integration: {
+ id: "integration-1",
+ type: "gitlab",
+ isActive,
+ config: JSON.stringify({
+ baseUrl: "https://gitlab.com",
+ accessToken: "token",
+ projectPath: "acme/web",
+ }),
+ },
+ };
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.createGitlabClient.mockReturnValue(mocks.client);
+ mocks.client.listLabels.mockResolvedValue([{ id: 1, name: "bug" }]);
+ mocks.client.getIssue.mockResolvedValue({ iid: 3, labels: [] });
+ mocks.client.updateIssue.mockResolvedValue({ iid: 3 });
+});
+
+describe("GitLab label sync", () => {
+ it("adds a label to the issue of an active integration", async () => {
+ mocks.externalLinkFindMany.mockResolvedValue([issueLink(true)]);
+
+ await syncLabelToGitlab("task-1", "bug", "#EF4444");
+
+ expect(mocks.client.updateIssue).toHaveBeenCalledWith("acme/web", 3, {
+ add_labels: "bug",
+ });
+ });
+
+ it("leaves GitLab alone when the integration is disabled", async () => {
+ mocks.externalLinkFindMany.mockResolvedValue([issueLink(false)]);
+
+ await syncLabelToGitlab("task-1", "bug", "#EF4444");
+ await removeLabelFromGitlab("task-1", "bug");
+
+ expect(mocks.createGitlabClient).not.toHaveBeenCalled();
+ expect(mocks.client.updateIssue).not.toHaveBeenCalled();
+ });
+});
diff --git a/tests/api/plugins/gitlab/webhooks/note-created.test.ts b/tests/api/plugins/gitlab/webhooks/note-created.test.ts
new file mode 100644
index 000000000..57fc2dcc9
--- /dev/null
+++ b/tests/api/plugins/gitlab/webhooks/note-created.test.ts
@@ -0,0 +1,123 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { handleGitlabNoteCreated } from "../../../../../apps/api/src/plugins/gitlab/webhooks/note-created";
+
+const mocks = vi.hoisted(() => {
+ const insertedActivities: Array<Record<string, unknown>> = [];
+
+ return {
+ insertedActivities,
+ findAllIntegrationsByGitlabProject: vi.fn(),
+ findExternalLink: vi.fn(),
+ db: {
+ insert: () => ({
+ values: (values: Record<string, unknown>) => {
+ insertedActivities.push(values);
+ return { onConflictDoNothing: async () => undefined };
+ },
+ }),
+ },
+ };
+});
+
+vi.mock("../../../../../apps/api/src/database", () => ({ default: mocks.db }));
+
+vi.mock(
+ "../../../../../apps/api/src/plugins/github/services/link-manager",
+ () => ({
+ findExternalLink: (...args: unknown[]) => mocks.findExternalLink(...args),
+ }),
+);
+
+vi.mock(
+ "../../../../../apps/api/src/plugins/gitlab/services/integration-lookup",
+ () => ({
+ findAllIntegrationsByGitlabProject: (...args: unknown[]) =>
+ mocks.findAllIntegrationsByGitlabProject(...args),
+ }),
+);
+
+function notePayload(
+ overrides: {
+ id?: number;
+ internal?: boolean;
+ system?: boolean;
+ eventType?: string;
+ } = {},
+) {
+ return {
+ event_type: overrides.eventType ?? "note",
+ user: { username: "octocat" },
+ object_attributes: {
+ id: overrides.id ?? 10,
+ note: "Reproduced on Firefox too.",
+ noteable_type: "Issue",
+ url: "https://gitlab.com/acme/web/-/issues/3#note_10",
+ system: overrides.system ?? false,
+ internal: overrides.internal ?? false,
+ },
+ issue: { iid: 3 },
+ project: {
+ name: "web",
+ web_url: "https://gitlab.com/acme/web",
+ path_with_namespace: "acme/web",
+ },
+ };
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.insertedActivities.length = 0;
+ mocks.findAllIntegrationsByGitlabProject.mockResolvedValue([
+ { id: "integration-1" },
+ ]);
+ mocks.findExternalLink.mockResolvedValue({
+ id: "link-1",
+ taskId: "task-1",
+ metadata: JSON.stringify({ state: "opened" }),
+ });
+});
+
+describe("handleGitlabNoteCreated", () => {
+ it("copies a comment into the task activity", async () => {
+ await handleGitlabNoteCreated(notePayload());
+
+ expect(mocks.insertedActivities).toHaveLength(1);
+ expect(mocks.insertedActivities[0]).toMatchObject({
+ taskId: "task-1",
+ externalSource: "gitlab",
+ content: "Reproduced on Firefox too.",
+ });
+ });
+
+ it("keeps an internal note out of the workspace", async () => {
+ await handleGitlabNoteCreated(notePayload({ internal: true }));
+
+ expect(mocks.insertedActivities).toHaveLength(0);
+ });
+
+ it("keeps a comment on a confidential issue out of the workspace", async () => {
+ await handleGitlabNoteCreated(
+ notePayload({ eventType: "confidential_note" }),
+ );
+
+ expect(mocks.insertedActivities).toHaveLength(0);
+ });
+
+ it("skips system notes", async () => {
+ await handleGitlabNoteCreated(notePayload({ system: true }));
+
+ expect(mocks.insertedActivities).toHaveLength(0);
+ });
+
+ it("skips a note Kaneo posted itself", async () => {
+ mocks.findExternalLink.mockResolvedValue({
+ id: "link-1",
+ taskId: "task-1",
+ metadata: JSON.stringify({ syncedNoteIds: [10] }),
+ });
+
+ await handleGitlabNoteCreated(notePayload({ id: 10 }));
+
+ expect(mocks.insertedActivities).toHaveLength(0);
+ });
+});