Canvas: Generate Github nodes out of the dodo-app config

Change-Id: Ifc5b09deb39352a3025f7ea66ce39b421daac94d
diff --git a/apps/canvas/config/src/config.ts b/apps/canvas/config/src/config.ts
index 53fd64c..dc3b122 100644
--- a/apps/canvas/config/src/config.ts
+++ b/apps/canvas/config/src/config.ts
@@ -4,6 +4,7 @@
 	Env,
 	GatewayHttpsNode,
 	GatewayTCPNode,
+	GithubNode,
 	MongoDBNode,
 	Network,
 	NetworkNode,
@@ -15,6 +16,7 @@
 import { Edge } from "@xyflow/react";
 import { v4 as uuidv4 } from "uuid";
 import { ConfigWithInput, Ingress, Service, Volume, PostgreSQL, MongoDB, Config, PortDomain } from "./types.js";
+import { GithubRepository } from "./github.js";
 
 export function generateDodoConfig(appId: string | undefined, nodes: AppNode[], env: Env): ConfigWithInput | null {
 	try {
@@ -170,7 +172,7 @@
 	edges: Edge[];
 };
 
-export function configToGraph(config: Config, networks: Network[], current?: Graph): Graph {
+export function configToGraph(config: Config, networks: Network[], repos: GithubRepository[], current?: Graph): Graph {
 	if (current == null) {
 		current = { nodes: [], edges: [] };
 	}
@@ -181,6 +183,39 @@
 	if (networks.length === 0) {
 		return ret;
 	}
+	const repoNodes = (config.service || [])
+		.filter((s) => s.source.repository != null)
+		.map((s): GithubNode | null => {
+			const existing = current.nodes.find(
+				(n) => n.type === "github" && n.data.repository?.sshURL === s.source.repository,
+			);
+			const repo = repos.find((r) => r.ssh_url === s.source.repository);
+			if (repo == null) {
+				return null;
+			}
+			return {
+				id: existing != null ? existing.id : uuidv4(),
+				type: "github",
+				data: {
+					label: repo.full_name,
+					repository: {
+						id: repo.id,
+						sshURL: repo.ssh_url,
+						fullName: repo.full_name,
+					},
+					envVars: [],
+					ports: [],
+				},
+				position:
+					existing != null
+						? existing.position
+						: {
+								x: 0,
+								y: 0,
+							},
+			};
+		})
+		.filter((n) => n != null);
 	const networkNodes = networks.map((n): NetworkNode => {
 		let existing: NetworkNode | undefined = undefined;
 		existing = current.nodes
@@ -210,6 +245,12 @@
 				label: s.name,
 				type: s.type,
 				env: [],
+				repository: {
+					id: repoNodes.find((r) => r.data.repository?.sshURL === s.source.repository)!.data.repository!.id,
+					repoNodeId: repoNodes.find((r) => r.data.repository?.sshURL === s.source.repository)!.id,
+					branch: s.source.branch,
+					rootDir: s.source.rootDir,
+				},
 				ports: (s.ports || []).map(
 					(p): Port => ({
 						id: uuidv4(),
@@ -494,7 +535,7 @@
 		});
 	ret.nodes = [
 		...networkNodes,
-		...ret.nodes,
+		...repoNodes,
 		...(services || []),
 		...(serviceGateways || []),
 		...(volumes || []),
@@ -593,6 +634,15 @@
 			},
 		];
 	});
-	ret.edges = [...envVarEdges, ...exposureEdges, ...ingressEdges];
+	const repoEdges = (services || []).map((s): Edge => {
+		return {
+			id: uuidv4(),
+			source: s.data.repository!.repoNodeId!,
+			sourceHandle: "repository",
+			target: s.id,
+			targetHandle: "repository",
+		};
+	});
+	ret.edges = [...repoEdges, ...envVarEdges, ...exposureEdges, ...ingressEdges];
 	return ret;
 }