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.

1.4 KB
Raw Blame
import { and, eq } from "drizzle-orm";
import db from "../../../database";
import { integrationTable } from "../../../database/schema";
import type { GitlabConfig } from "../config";
import { normalizeGitlabBaseUrl, normalizeProjectPath } from "../config";
import { activeIntegrationCondition } from "../../active-integration-condition";

export async function findAllIntegrationsByGitlabProject(
  baseUrl: string,
  projectPath: string,
  integrationId?: string,
) {
  const normalizedBase = normalizeGitlabBaseUrl(baseUrl);
  const normalizedPath = normalizeProjectPath(projectPath);
  const conditions = [
    eq(integrationTable.type, "gitlab"),
    activeIntegrationCondition(),
  ];
  if (integrationId) {
    conditions.push(eq(integrationTable.id, integrationId));
  }

  const integrations = await db.query.integrationTable.findMany({
    where: and(...conditions),
    with: {
      project: true,
    },
  });

  return integrations.filter((integration) => {
    try {
      const config = JSON.parse(integration.config) as GitlabConfig;
      const matches =
        normalizeGitlabBaseUrl(config.baseUrl) === normalizedBase &&
        normalizeProjectPath(config.projectPath) === normalizedPath;
      if (integrationId && !matches) {
        console.warn("[GitLab Webhook] Signed integration project mismatch", {
          integrationId,
        });
      }
      return matches;
    } catch {
      return false;
    }
  });
}