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 settings when the connection is saves

zaralXe8bc4cce4933
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 ea0105797..c0cd4aa3b 100644
--- a/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.ts
+++ b/apps/api/src/gitlab-integration/controllers/create-gitlab-integration.ts
@@ -18,6 +18,23 @@ import {
verifyGitlabToken,
} from "../../plugins/gitlab/utils/gitlab-api";
+function pickSettings(config: Partial<GitlabConfig>): Partial<GitlabConfig> {
+ const settings: Partial<GitlabConfig> = {};
+ if (config.branchPattern !== undefined) {
+ settings.branchPattern = config.branchPattern;
+ }
+ if (config.customBranchRegex !== undefined) {
+ settings.customBranchRegex = config.customBranchRegex;
+ }
+ if (config.commentTaskLinkOnGitlabIssue !== undefined) {
+ settings.commentTaskLinkOnGitlabIssue = config.commentTaskLinkOnGitlabIssue;
+ }
+ if (config.statusTransitions !== undefined) {
+ settings.statusTransitions = config.statusTransitions;
+ }
+ return settings;
+}
+
async function createGitlabIntegration({
projectId,
baseUrl,
@@ -49,11 +66,10 @@ async function createGitlabIntegration({
),
});
- let resolvedToken = accessToken?.trim() ?? "";
- if (!resolvedToken && existingIntegration) {
+ let previousConfig: Partial<GitlabConfig> = {};
+ if (existingIntegration) {
try {
- const previous = JSON.parse(existingIntegration.config) as GitlabConfig;
- resolvedToken = previous.accessToken;
+ previousConfig = JSON.parse(existingIntegration.config) as GitlabConfig;
} catch (error) {
console.warn("Failed to parse existing GitLab integration config", {
integrationId: existingIntegration.id,
@@ -62,6 +78,8 @@ async function createGitlabIntegration({
}
}
+ const resolvedToken = accessToken?.trim() || previousConfig.accessToken || "";
+
if (!resolvedToken) {
throw new HTTPException(400, {
message: "Access token is required",
@@ -121,31 +139,19 @@ async function createGitlabIntegration({
}
}
- let webhookSecret = randomBytes(24).toString("hex");
- if (existingIntegration) {
- try {
- const previousConfig = JSON.parse(
- existingIntegration.config,
- ) as GitlabConfig;
- webhookSecret = previousConfig.webhookSecret ?? webhookSecret;
- } catch (error) {
- console.warn(
- "Failed to parse existing GitLab config for webhook secret",
- {
- integrationId: existingIntegration.id,
- error,
- },
- );
- }
- }
+ const webhookSecret =
+ previousConfig.webhookSecret ?? randomBytes(24).toString("hex");
- const config = getDefaultGitlabConfig(
- normalizedBase,
- resolvedToken,
- tokenType,
- normalizedPath,
- webhookSecret,
- );
+ const config: GitlabConfig = {
+ ...getDefaultGitlabConfig(
+ normalizedBase,
+ resolvedToken,
+ tokenType,
+ normalizedPath,
+ webhookSecret,
+ ),
+ ...pickSettings(previousConfig),
+ };
const validation = await validateGitlabConfig(config);
if (!validation.valid) {
diff --git a/tests/api/gitlab-integration/create-gitlab-integration.test.ts b/tests/api/gitlab-integration/create-gitlab-integration.test.ts
new file mode 100644
index 000000000..60051d0f5
--- /dev/null
+++ b/tests/api/gitlab-integration/create-gitlab-integration.test.ts
@@ -0,0 +1,120 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ existingIntegration: vi.fn(),
+ updateSet: vi.fn(),
+}));
+
+vi.mock("../../../apps/api/src/database", () => ({
+ default: {
+ query: {
+ projectTable: {
+ findFirst: async () => ({ id: "project-1" }),
+ },
+ integrationTable: {
+ findFirst: () => mocks.existingIntegration(),
+ findMany: async () => [],
+ },
+ },
+ update: () => ({
+ set: (values: Record<string, unknown>) => {
+ mocks.updateSet(values);
+ return {
+ where: () => ({
+ returning: async () => [
+ {
+ id: "integration-1",
+ projectId: "project-1",
+ isActive: true,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ },
+ ],
+ }),
+ };
+ },
+ }),
+ },
+}));
+
+vi.mock("../../../apps/api/src/plugins/gitlab/utils/gitlab-api", () => ({
+ GitlabApiError: class GitlabApiError extends Error {},
+ verifyGitlabToken: async () => ({ id: 1 }),
+ createGitlabClient: () => ({ getProject: async () => ({ id: 1 }) }),
+}));
+
+const { default: createGitlabIntegration } = await import(
+ "../../../apps/api/src/gitlab-integration/controllers/create-gitlab-integration"
+);
+
+function savedConfig() {
+ const values = mocks.updateSet.mock.calls[0]?.[0] as { config: string };
+ return JSON.parse(values.config);
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+});
+
+describe("createGitlabIntegration on an existing integration", () => {
+ it("keeps the settings saved for the integration", async () => {
+ mocks.existingIntegration.mockResolvedValue({
+ id: "integration-1",
+ config: JSON.stringify({
+ baseUrl: "https://gitlab.com",
+ accessToken: "old-token",
+ tokenType: "private",
+ projectPath: "acme/web",
+ webhookSecret: "existing-secret",
+ branchPattern: "custom",
+ customBranchRegex: "^feature/(\\d+)",
+ commentTaskLinkOnGitlabIssue: false,
+ statusTransitions: { onPROpen: "review", onPRMerge: "shipped" },
+ }),
+ });
+
+ await createGitlabIntegration({
+ projectId: "project-1",
+ baseUrl: "https://gitlab.com",
+ accessToken: "new-token",
+ tokenType: "private",
+ projectPath: "acme/web",
+ });
+
+ expect(savedConfig()).toMatchObject({
+ accessToken: "new-token",
+ webhookSecret: "existing-secret",
+ branchPattern: "custom",
+ customBranchRegex: "^feature/(\\d+)",
+ commentTaskLinkOnGitlabIssue: false,
+ statusTransitions: { onPROpen: "review", onPRMerge: "shipped" },
+ });
+ });
+
+ it("reuses the stored token and fills defaults for settings never saved", async () => {
+ mocks.existingIntegration.mockResolvedValue({
+ id: "integration-1",
+ config: JSON.stringify({
+ baseUrl: "https://gitlab.com",
+ accessToken: "old-token",
+ projectPath: "acme/web",
+ webhookSecret: "existing-secret",
+ }),
+ });
+
+ await createGitlabIntegration({
+ projectId: "project-1",
+ baseUrl: "https://gitlab.com",
+ accessToken: undefined,
+ tokenType: "private",
+ projectPath: "acme/web",
+ });
+
+ expect(savedConfig()).toMatchObject({
+ accessToken: "old-token",
+ branchPattern: "{slug}-{number}",
+ commentTaskLinkOnGitlabIssue: true,
+ statusTransitions: { onPROpen: "in-review" },
+ });
+ });
+});