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.

2.1 KB
Raw Blame
import type { ImportConnection } from "@kaneo/importers";
import { HTTPException } from "hono/http-exception";
import type { ImportConnectionInput } from "../schema";
import { trelloApiKeyFromEnv } from "./trello-api-key";

export type ResolvedImportConnection = {
  connection: ImportConnection;
  secrets: string[];
  credentialHint: string;
};

export function resolveImportConnection(
  input: ImportConnectionInput,
  env: NodeJS.ProcessEnv = process.env,
): ResolvedImportConnection {
  switch (input.source) {
    case "planka": {
      const { source, url, apiKey, username, password } = input;
      const usesPassword = !apiKey;
      return {
        connection: {
          source,
          url,
          ...(apiKey ? { apiKey } : { username, password }),
        },
        secrets: [apiKey, password].filter(isPresent),
        credentialHint: usesPassword
          ? "Check the username and password."
          : "Check the API key.",
      };
    }
    case "trello": {
      const apiKey = input.apiKey ?? trelloApiKeyFromEnv(env);
      if (!apiKey) {
        throw new HTTPException(400, { message: "Add a Trello API key." });
      }
      return {
        connection: { source: "trello", apiKey, token: input.token },
        secrets: [input.token, apiKey],
        credentialHint: "Check the API key and token.",
      };
    }
    case "plane":
      return {
        connection: input,
        secrets: [input.apiKey],
        credentialHint: "Check the API key.",
      };
    case "linear":
      return {
        connection: input,
        secrets: [input.apiKey],
        credentialHint: "Check the API key.",
      };
    case "jira":
      return {
        connection: input,
        secrets: [input.apiToken],
        credentialHint:
          "Check the email and API token, or the personal access token for Jira Data Center.",
      };
    case "kaneo":
      return {
        connection: input,
        secrets: [input.apiKey],
        credentialHint: "Check the API key.",
      };
  }
}

function isPresent(value: string | undefined): value is string {
  return Boolean(value);
}