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): keep gitlab private content out + stop footer repeating

zaralX07fd36549e6a
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 30a90688b..89b39e5d6 100644
--- a/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
+++ b/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
@@ -18,7 +18,6 @@ import {
extractIssuePriority,
extractIssueStatus,
} from "../../plugins/github/utils/extract-priority";
-import { formatTaskDescriptionFromIssue } from "../../plugins/github/utils/format";
import type { GitlabConfig } from "../../plugins/gitlab/config";
import { extractTaskNumberGitlab } from "../../plugins/gitlab/utils/branch-matcher";
import {
@@ -26,6 +25,7 @@ import {
type GitlabIssue,
type GitlabMergeRequest,
} from "../../plugins/gitlab/utils/gitlab-api";
+import { taskDescriptionFromIssue } from "../../plugins/gitlab/utils/issue-description";
import { isSystemLabelName } from "../../plugins/gitlab/utils/system-labels";
import { claimTaskNumber } from "../../task/controllers/claim-task-numbers";
@@ -105,6 +105,13 @@ export async function importGitlabIssues(
}
for (const issue of allIssues) {
+ // Confidential issues stay out of the workspace, the same as they do when
+ // one arrives through the webhook.
+ if (issue.confidential) {
+ skipped++;
+ continue;
+ }
+
try {
const result = await importSingleIssue(
issue,
@@ -183,7 +190,7 @@ async function importSingleIssue(
if (existingLink) {
const updateData: Record<string, unknown> = {
title: issue.title,
- description: formatTaskDescriptionFromIssue(issue.description),
+ description: taskDescriptionFromIssue(issue.description),
};
if (priority) updateData.priority = priority;
@@ -210,7 +217,7 @@ async function importSingleIssue(
projectId,
userId: null,
title: issue.title,
- description: formatTaskDescriptionFromIssue(issue.description),
+ description: taskDescriptionFromIssue(issue.description),
status: status || "to-do",
priority: priority ?? "low",
number,
@@ -321,8 +328,9 @@ async function importNotesForTask(
if (notes.length === 0) break;
for (const note of notes) {
- // GitLab records label, milestone and state changes as system notes.
- if (note.system) {
+ // GitLab records label, milestone and state changes as system notes, and
+ // an internal note is visible there only to project members.
+ if (note.system || note.internal) {
continue;
}
diff --git a/apps/api/src/plugins/gitlab/utils/gitlab-api.ts b/apps/api/src/plugins/gitlab/utils/gitlab-api.ts
index 9cb703069..52c6c7d0f 100644
--- a/apps/api/src/plugins/gitlab/utils/gitlab-api.ts
+++ b/apps/api/src/plugins/gitlab/utils/gitlab-api.ts
@@ -26,12 +26,14 @@ export type GitlabIssue = {
labels?: string[];
author?: GitlabUser | null;
updated_at?: string;
+ confidential?: boolean;
};
export type GitlabNote = {
id: number;
body: string;
system: boolean;
+ internal?: boolean;
author?: GitlabUser | null;
created_at: string;
};
diff --git a/apps/api/src/plugins/gitlab/utils/issue-description.ts b/apps/api/src/plugins/gitlab/utils/issue-description.ts
new file mode 100644
index 000000000..65f125814
--- /dev/null
+++ b/apps/api/src/plugins/gitlab/utils/issue-description.ts
@@ -0,0 +1,17 @@
+// formatIssueBody appends a `Task: <id>` footer to every description Kaneo
+// writes to GitLab. Reading that body back into the task unchanged would keep
+// the footer there, and the next edit in Kaneo would append a second one.
+const FOOTER = /(?:\r?\n)*-{3}\r?\n<sub>Task: [^<\r\n]+<\/sub>\s*$/;
+const FOOTER_ONLY = /^\s*<sub>Task: [^<\r\n]+<\/sub>\s*$/;
+
+export function taskDescriptionFromIssue(body: string | null): string {
+ if (!body) {
+ return "";
+ }
+
+ if (FOOTER_ONLY.test(body)) {
+ return "";
+ }
+
+ return body.replace(FOOTER, "");
+}
diff --git a/apps/api/src/plugins/gitlab/utils/sync-label-to-gitlab.ts b/apps/api/src/plugins/gitlab/utils/sync-label-to-gitlab.ts
index 5a3ca29a5..55d685e1f 100644
--- a/apps/api/src/plugins/gitlab/utils/sync-label-to-gitlab.ts
+++ b/apps/api/src/plugins/gitlab/utils/sync-label-to-gitlab.ts
@@ -52,9 +52,14 @@ async function getGitlabIssueContext(taskId: string) {
},
});
+ // Label changes reach here straight from the label controllers rather than
+ // through the plugin registry, so the registry's active-integration filter
+ // does not apply and a disabled integration has to be skipped here.
const externalLink = externalLinks.find(
(link) =>
- link.resourceType === "issue" && link.integration?.type === "gitlab",
+ link.resourceType === "issue" &&
+ link.integration?.type === "gitlab" &&
+ link.integration.isActive === true,
);
if (!externalLink?.integration) {
diff --git a/apps/api/src/plugins/gitlab/webhooks/issue-opened.ts b/apps/api/src/plugins/gitlab/webhooks/issue-opened.ts
index 5db7d6fa1..ef5ac9418 100644
--- a/apps/api/src/plugins/gitlab/webhooks/issue-opened.ts
+++ b/apps/api/src/plugins/gitlab/webhooks/issue-opened.ts
@@ -12,10 +12,10 @@ import {
extractIssuePriority,
extractIssueStatus,
} from "../../github/utils/extract-priority";
-import { formatTaskDescriptionFromIssue } from "../../github/utils/format";
import type { GitlabConfig } from "../config";
import { findAllIntegrationsByGitlabProject } from "../services/integration-lookup";
import { createGitlabClient } from "../utils/gitlab-api";
+import { taskDescriptionFromIssue } from "../utils/issue-description";
import { addLabelsToIssueGitlab } from "../utils/labels";
import type {
GitlabWebhookLabel,
@@ -35,6 +35,7 @@ type IssueOpenedPayload = {
description: string | null;
url: string;
action?: string;
+ confidential?: boolean;
};
labels?: GitlabWebhookLabel[];
project: GitlabWebhookProject;
@@ -47,6 +48,12 @@ export async function handleGitlabIssueOpened(
const issue = payload.object_attributes;
const { project } = payload;
+ // A confidential issue is limited to project members in GitLab; turning it
+ // into a task would publish its title and description to the whole workspace.
+ if (issue.confidential) {
+ return;
+ }
+
const baseUrl = baseUrlFromProjectWebUrl(
project.web_url,
project.path_with_namespace,
@@ -113,7 +120,7 @@ export async function handleGitlabIssueOpened(
projectId,
userId: null,
title: issue.title,
- description: formatTaskDescriptionFromIssue(issue.description),
+ description: taskDescriptionFromIssue(issue.description),
status: resolvedStatus,
columnId: targetColumn?.id ?? null,
priority: priority ?? "low",
diff --git a/apps/api/src/plugins/gitlab/webhooks/issue-updated.ts b/apps/api/src/plugins/gitlab/webhooks/issue-updated.ts
index 8e24334a7..ad634b283 100644
--- a/apps/api/src/plugins/gitlab/webhooks/issue-updated.ts
+++ b/apps/api/src/plugins/gitlab/webhooks/issue-updated.ts
@@ -11,8 +11,8 @@ import {
extractIssuePriority,
extractIssueStatus,
} from "../../github/utils/extract-priority";
-import { formatTaskDescriptionFromIssue } from "../../github/utils/format";
import { findAllIntegrationsByGitlabProject } from "../services/integration-lookup";
+import { taskDescriptionFromIssue } from "../utils/issue-description";
import {
isEchoOf,
type LinkMetadata,
@@ -189,8 +189,11 @@ export async function handleGitlabIssueUpdated(
}
if (changes?.description) {
- const description = formatTaskDescriptionFromIssue(issue.description);
- if (!isEchoOf(metadata.lastSync?.description, "kaneo", description)) {
+ // Kaneo recorded the full body it wrote, footer included, so the echo
+ // is recognised against the raw body; the task keeps it without one.
+ const issueBody = issue.description ?? "";
+ if (!isEchoOf(metadata.lastSync?.description, "kaneo", issueBody)) {
+ const description = taskDescriptionFromIssue(issue.description);
updateData.description = description;
lastSync.description = {
timestamp: now,
diff --git a/apps/api/src/plugins/gitlab/webhooks/note-created.ts b/apps/api/src/plugins/gitlab/webhooks/note-created.ts
index 3e3778327..557188769 100644
--- a/apps/api/src/plugins/gitlab/webhooks/note-created.ts
+++ b/apps/api/src/plugins/gitlab/webhooks/note-created.ts
@@ -7,6 +7,7 @@ import { syncedNoteIds } from "../utils/synced-notes";
import { baseUrlFromProjectWebUrl } from "../utils/webhook-project";
type NoteCreatedPayload = {
+ event_type?: string;
user?: GitlabWebhookUser | null;
object_attributes: {
id: number;
@@ -14,6 +15,7 @@ type NoteCreatedPayload = {
noteable_type: string;
url: string;
system?: boolean;
+ internal?: boolean;
};
issue?: { iid: number };
project: GitlabWebhookProject;
@@ -35,6 +37,13 @@ export async function handleGitlabNoteCreated(
return;
}
+ // An internal note, or any note on a confidential issue, is visible in GitLab
+ // only to project members with Reporter access. Copying it into the task
+ // activity would show it to everyone in the Kaneo workspace.
+ if (note.internal || payload.event_type === "confidential_note") {
+ return;
+ }
+
const baseUrl = baseUrlFromProjectWebUrl(
payload.project.web_url,
payload.project.path_with_namespace,
diff --git a/tests/api/plugins/gitlab/utils/issue-description.test.ts b/tests/api/plugins/gitlab/utils/issue-description.test.ts
new file mode 100644
index 000000000..15b6e0951
--- /dev/null
+++ b/tests/api/plugins/gitlab/utils/issue-description.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it } from "vitest";
+import { formatIssueBody } from "../../../../../apps/api/src/plugins/github/utils/format";
+import { taskDescriptionFromIssue } from "../../../../../apps/api/src/plugins/gitlab/utils/issue-description";
+
+const taskId = "uhapfwllr062kqj36fd7k1eq";
+
+describe("taskDescriptionFromIssue", () => {
+ it("removes the footer Kaneo appended to a description", () => {
+ expect(
+ taskDescriptionFromIssue(formatIssueBody("Ten attempts per IP.", taskId)),
+ ).toBe("Ten attempts per IP.");
+ });
+
+ it("returns an empty description when the body is only the footer", () => {
+ expect(taskDescriptionFromIssue(formatIssueBody("", taskId))).toBe("");
+ });
+
+ it("keeps a multi-line description intact", () => {
+ const description = "First line.\n\n- a list item\n- another";
+ expect(taskDescriptionFromIssue(formatIssueBody(description, taskId))).toBe(
+ description,
+ );
+ });
+
+ it("leaves a description written in GitLab untouched", () => {
+ expect(
+ taskDescriptionFromIssue("Written by hand.\n\n---\nNo footer."),
+ ).toBe("Written by hand.\n\n---\nNo footer.");
+ });
+
+ it("returns an empty string for a missing body", () => {
+ expect(taskDescriptionFromIssue(null)).toBe("");
+ });
+
+ it("keeps a single footer across a GitLab edit followed by a Kaneo edit", () => {
+ const written = formatIssueBody("Original.", taskId);
+ const editedInGitlab = written.replace("Original.", "Edited in GitLab.");
+
+ const taskDescription = taskDescriptionFromIssue(editedInGitlab);
+ const writtenAgain = formatIssueBody(`${taskDescription} More.`, taskId);
+
+ expect(writtenAgain.match(/<sub>Task:/g)).toHaveLength(1);
+ });
+});
diff --git a/tests/api/plugins/gitlab/webhooks/issue-opened.test.ts b/tests/api/plugins/gitlab/webhooks/issue-opened.test.ts
index d061f2171..194d19839 100644
--- a/tests/api/plugins/gitlab/webhooks/issue-opened.test.ts
+++ b/tests/api/plugins/gitlab/webhooks/issue-opened.test.ts
@@ -160,6 +160,17 @@ describe("handleGitlabIssueOpened", () => {
);
});
+ it("keeps a confidential issue out of the workspace", async () => {
+ const payload = issueOpenedPayload([]);
+ await handleGitlabIssueOpened({
+ ...payload,
+ object_attributes: { ...payload.object_attributes, confidential: true },
+ });
+
+ expect(mocks.insertedValues).toHaveLength(0);
+ expect(mocks.findAllIntegrationsByGitlabProject).not.toHaveBeenCalled();
+ });
+
it("skips an issue that already has a task", async () => {
mocks.findExternalLink.mockResolvedValue({
id: "link-1",