Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

2.2 KB
Raw Blame
import { UNKNOWN_ID } from "@/types/const";
import type { Issue } from "@/types/proto-es/v1/issue_service_pb";
import type { Plan, Plan_Spec } from "@/types/proto-es/v1/plan_service_pb";

export const shouldRedirectToIssueDetail = (plan: Plan, issue?: Issue) => {
  if (!issue?.name || issue.draft || !plan.specs?.length) {
    return false;
  }
  return plan.specs.every(
    (spec) => spec.config?.case === "createDatabaseConfig"
  );
};

export const sheetNameOfSpec = (spec: Plan_Spec): string => {
  if (spec.config?.case === "changeDatabaseConfig") {
    return spec.config.value.sheet ?? "";
  }
  return "";
};

export const targetsOfSpec = (spec: Plan_Spec): string[] => {
  if (spec.config?.case === "changeDatabaseConfig") {
    return spec.config.value.targets ?? [];
  }
  return [];
};

export const enablePriorBackupOfSpec = (spec: Plan_Spec): boolean => {
  return spec.config?.case === "changeDatabaseConfig"
    ? (spec.config.value.enablePriorBackup ?? false)
    : false;
};

export const extractPlanUID = (name: string) => {
  const pattern = /(?:^|\/)plans\/(\d+)(?:$|\/)/;
  const matches = name.match(pattern);
  return matches?.[1] ?? "";
};

export const isValidPlanName = (name: string | undefined) => {
  if (!name) {
    return false;
  }
  const planUID = extractPlanUID(name);
  return Boolean(planUID && planUID !== String(UNKNOWN_ID));
};

export const extractPlanCheckRunUID = (name: string) => {
  const pattern = /(?:^|\/)planCheckRuns\/(\d+)(?:$|\/)/;
  const matches = name.match(pattern);
  return matches?.[1] ?? "";
};

const extractSpecId = (name: string) => {
  const pattern = /(?:^|\/)specs\/([^/]+)(?:$|\/)/;
  const matches = name.match(pattern);
  return matches?.[1] ?? "";
};

/**
 * Get spec display info from spec resource name.
 * Returns specId and 1-based display index, or null if not found.
 */
export const getSpecDisplayInfo = (
  specs: Plan_Spec[],
  specResourceName: string
): { specId: string; displayIndex: number } | null => {
  const specId = extractSpecId(specResourceName);
  if (!specId) return null;

  const index = specs.findIndex((spec) => spec.id === specId);
  if (index >= 0) {
    return { specId, displayIndex: index + 1 };
  }
  return null;
};