-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.ts
More file actions
262 lines (228 loc) · 8.28 KB
/
api.ts
File metadata and controls
262 lines (228 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { usePipelineStore } from "../store/usePipelineStore";
export const BASE =
import.meta.env.VITE_API_BASE ?? "http://localhost:3000/api";
// Derive the server base without any trailing "/api" for MCP calls
const SERVER_BASE = BASE.replace(/\/api$/, "");
async function request<T>(path: string, opts: RequestInit = {}): Promise<T> {
const res = await fetch(`${BASE}${path}`, {
headers: { "Content-Type": "application/json" },
credentials: "include",
...opts,
});
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error((data as any)?.error || res.statusText);
return data as T;
}
// Helper for MCP tool calls on the server at /mcp/v1/:tool_name
async function mcp<T>(tool: string, input: Record<string, any> = {}): Promise<T> {
const res = await fetch(`${SERVER_BASE}/mcp/v1/${encodeURIComponent(tool)}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include",
body: JSON.stringify(input),
});
const payload = await res.json().catch(() => ({}));
if (!res.ok || payload?.success === false) {
const msg = payload?.error || res.statusText || "MCP error";
throw new Error(msg);
}
return payload.data as T;
}
export const api = {
// ✅ method syntax (preferred)
async listRepos(): Promise<{ repos: string[] }> {
const outer = await mcp<{
success: boolean;
data: { provider: string; user: string; repositories: { full_name: string }[] };
}>("repo_reader", {});
const inner = outer?.data;
const repos = inner?.repositories?.map(r => r.full_name) ?? [];
return { repos };
},
async listBranches(repo: string): Promise<{ branches: string[] }> {
const outer = await mcp<{
success: boolean;
data: { provider: string; user: string; repositories: { full_name: string; branches?: string[] }[] };
}>("repo_reader", {});
const inner = outer?.data;
const match = inner?.repositories?.find(r => r.full_name === repo);
return { branches: match?.branches ?? [] };
},
async createPipeline(payload: any) {
const { repo, branch, template = "node_app", options } = payload || {};
const data = await mcp("pipeline_generator", {
repo,
branch,
provider: "aws",
template,
options: options || {},
});
return data;
},
async listAwsRoles(): Promise<{ roles: string[] }> {
const data = await mcp<{ roles?: { name: string; arn: string }[] }>(
"oidc_adapter",
{ provider: "aws" }
);
return { roles: (data.roles ?? []).map(r => r.arn) };
},
async openPr(_payload: any) {
throw new Error("openPr is not implemented on the server (no MCP tool)");
},
// ... keep the rest of your existing methods like getConnections, getSecretPresence, etc.
// --- Mocked config/secrets endpoints for Secrets/Preflight flow ---
async getConnections(_repo: string): Promise<{
githubAppInstalled: boolean;
githubRepoWriteOk: boolean;
awsOidc: { connected: boolean; roleArn?: string; accountId?: string; region?: string };
}> {
// Try to fetch roles to populate a default role ARN
let roleArn: string | undefined;
try {
const { roles } = await this.listAwsRoles();
roleArn = roles[0];
} catch {}
return {
githubAppInstalled: true,
githubRepoWriteOk: true,
awsOidc: { connected: !!roleArn, roleArn, accountId: "123456789012", region: "us-east-1" },
};
},
async getSecretPresence(repo: string, env: string): Promise<{ key: string; present: boolean }[]> {
const required = ["GITHUB_TOKEN", "AWS_ROLE_ARN"];
const store = readSecrets(repo, env);
return required.map((k) => ({ key: k, present: !!store[k] }));
},
async setSecret({ repo, env, key, value }: { repo: string; env: string; key: string; value: string }) {
const store = readSecrets(repo, env);
store[key] = value;
writeSecrets(repo, env, store);
return { ok: true } as const;
},
async runPreflight({
repo,
env,
aws,
}: {
repo: string;
env: string;
aws?: { roleArn?: string; region?: string };
}): Promise<{ results: { label: string; ok: boolean; info?: string }[] }> {
const connections = await this.getConnections(repo);
const secrets = await this.getSecretPresence(repo, env);
const hasGithubApp = connections.githubAppInstalled;
const hasRepoWrite = connections.githubRepoWriteOk;
const role = aws?.roleArn || connections.awsOidc.roleArn;
const hasAws = !!role;
const region = aws?.region || connections.awsOidc.region || "us-east-1";
const s = Object.fromEntries(secrets.map((x) => [x.key, x.present] as const));
const results = [
{ label: "GitHub App installed", ok: hasGithubApp },
{ label: "Repo write access", ok: hasRepoWrite },
{ label: "AWS OIDC configured", ok: hasAws, info: role },
{ label: "Secret: GITHUB_TOKEN", ok: !!s.GITHUB_TOKEN },
{ label: "Secret: AWS_ROLE_ARN", ok: !!s.AWS_ROLE_ARN, info: role },
{ label: "AWS Region selected", ok: !!region, info: region },
];
return { results };
},
// --- Deploy APIs for Dashboard ---
async startDeploy({
repoFullName: fromCallerRepo,
branch,
env,
yaml: fromCallerYaml,
provider,
path,
}: {
repoFullName?: string;
branch?: string;
env?: string;
yaml?: string;
provider?: string;
path?: string;
}) {
const pipelineStore = usePipelineStore.getState();
const repoFullName = fromCallerRepo || pipelineStore?.repoFullName || pipelineStore?.result?.repo;
const selectedBranch = branch || pipelineStore?.selectedBranch || "main";
const yaml = pipelineStore?.result?.generated_yaml;
const environment = env || pipelineStore?.environment || "dev";
const providerFinal = provider || pipelineStore?.provider || "aws";
const pathFinal = path || `.github/workflows/${environment}-deploy.yml`;
console.group("[Deploy Debug]");
console.log("repoFullName:", repoFullName);
console.log("selectedBranch:", selectedBranch);
console.log("environment:", environment);
console.log("provider:", providerFinal);
console.log("path:", pathFinal);
console.log("YAML length:", yaml ? yaml.length : 0);
console.groupEnd();
const payload = {
repoFullName,
branch: selectedBranch,
env: environment,
yaml,
provider: providerFinal,
path: pathFinal,
};
console.log("[Deploy] Final payload:", payload);
const res = await fetch(`${SERVER_BASE}/mcp/v1/pipeline_commit`, {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include",
body: JSON.stringify(payload),
});
const data = await res.json().catch(() => ({}));
console.group("[Deploy Response]");
console.log("Status:", res.status);
console.log("Data:", data);
console.groupEnd();
if (!res.ok) throw new Error(`Pipeline commit failed: ${res.statusText}`);
return data;
},
streamJob(_jobId: string, onEvent: (e: { ts: string; level: "info"; msg: string }) => void) {
const steps = [
"Connecting to GitHub...",
"Committing workflow file...",
"Verifying commit...",
"Done ✅"
];
let i = 0;
const timer = setInterval(() => {
if (i >= steps.length) return;
onEvent({ ts: new Date().toISOString(), level: "info", msg: steps[i++] });
if (i >= steps.length) clearInterval(timer);
}, 600);
return () => clearInterval(timer);
},
};
// Helper to start GitHub OAuth (server redirects back after callback)
export function startGitHubOAuth(
redirectTo: string = window.location.origin
) {
// Our server mounts OAuth at /auth/github/start and expects `redirect_to`
const serverBase = BASE.replace(/\/api$/, "");
const url = `${serverBase}/auth/github/start?redirect_to=${encodeURIComponent(
redirectTo
)}`;
window.location.href = url;
}
// --- Local storage helpers for mock secrets ---
function secKey(repo: string, env: string) {
return `secrets:${repo}:${env}`;
}
function readSecrets(repo: string, env: string): Record<string, string> {
try {
const raw = localStorage.getItem(secKey(repo, env));
return raw ? JSON.parse(raw) : {};
} catch {
return {};
}
}
function writeSecrets(repo: string, env: string, obj: Record<string, string>) {
try {
localStorage.setItem(secKey(repo, env), JSON.stringify(obj));
} catch {}
}
// in-memory job storage for mock deploys
const JOBS: Map<string, any> = new Map();