|
| 1 | +import { usePipelineStore } from "../store/usePipelineStore"; |
| 2 | + |
1 | 3 | export const BASE = |
2 | 4 | import.meta.env.VITE_API_BASE ?? "http://localhost:3000/api"; |
3 | 5 |
|
@@ -139,35 +141,82 @@ export const api = { |
139 | 141 | return { results }; |
140 | 142 | }, |
141 | 143 |
|
142 | | - // --- Mock deploy APIs for Dashboard --- |
143 | | - async startDeploy({ repo, env }: { repo: string; env: string }) { |
144 | | - const jobId = `job_${Math.random().toString(36).slice(2)}`; |
145 | | - // Stash minimal job info in memory for the stream to reference |
146 | | - JOBS.set(jobId, { repo, env, startedAt: Date.now() }); |
147 | | - return { jobId } as const; |
148 | | - }, |
| 144 | + // --- Deploy APIs for Dashboard --- |
| 145 | +async startDeploy({ |
| 146 | + repoFullName: fromCallerRepo, |
| 147 | + branch, |
| 148 | + env, |
| 149 | + yaml: fromCallerYaml, |
| 150 | + provider, |
| 151 | + path, |
| 152 | +}: { |
| 153 | + repoFullName?: string; |
| 154 | + branch?: string; |
| 155 | + env?: string; |
| 156 | + yaml?: string; |
| 157 | + provider?: string; |
| 158 | + path?: string; |
| 159 | +}) { |
| 160 | + const pipelineStore = usePipelineStore.getState(); |
| 161 | + const repoFullName = fromCallerRepo || pipelineStore?.repoFullName || pipelineStore?.result?.repo; |
| 162 | + const selectedBranch = branch || pipelineStore?.selectedBranch || "main"; |
| 163 | + const yaml = pipelineStore?.result?.generated_yaml; |
| 164 | + const environment = env || pipelineStore?.environment || "dev"; |
| 165 | + |
| 166 | + const providerFinal = provider || pipelineStore?.provider || "aws"; |
| 167 | + const pathFinal = path || `.github/workflows/${environment}-deploy.yml`; |
| 168 | + |
| 169 | + console.group("[Deploy Debug]"); |
| 170 | + console.log("repoFullName:", repoFullName); |
| 171 | + console.log("selectedBranch:", selectedBranch); |
| 172 | + console.log("environment:", environment); |
| 173 | + console.log("provider:", providerFinal); |
| 174 | + console.log("path:", pathFinal); |
| 175 | + console.log("YAML length:", yaml ? yaml.length : 0); |
| 176 | + console.groupEnd(); |
| 177 | + |
| 178 | + const payload = { |
| 179 | + repoFullName, |
| 180 | + branch: selectedBranch, |
| 181 | + env: environment, |
| 182 | + yaml, |
| 183 | + provider: providerFinal, |
| 184 | + path: pathFinal, |
| 185 | + }; |
| 186 | + |
| 187 | + console.log("[Deploy] Final payload:", payload); |
| 188 | + |
| 189 | + const res = await fetch(`${SERVER_BASE}/mcp/v1/pipeline_commit`, { |
| 190 | + method: "POST", |
| 191 | + headers: { "Content-Type": "application/json" }, |
| 192 | + credentials: "include", |
| 193 | + body: JSON.stringify(payload), |
| 194 | + }); |
| 195 | + |
| 196 | + const data = await res.json().catch(() => ({})); |
| 197 | + |
| 198 | + console.group("[Deploy Response]"); |
| 199 | + console.log("Status:", res.status); |
| 200 | + console.log("Data:", data); |
| 201 | + console.groupEnd(); |
| 202 | + |
| 203 | + if (!res.ok) throw new Error(`Pipeline commit failed: ${res.statusText}`); |
| 204 | + return data; |
| 205 | +}, |
149 | 206 |
|
150 | | - streamJob( |
151 | | - jobId: string, |
152 | | - onEvent: (e: { ts: string; level: "info" | "warn" | "error"; msg: string }) => void |
153 | | - ) { |
154 | | - const meta = JOBS.get(jobId) || { repo: "?", env: "dev" }; |
| 207 | + streamJob(_jobId: string, onEvent: (e: { ts: string; level: "info"; msg: string }) => void) { |
155 | 208 | const steps = [ |
156 | | - `Authenticating to AWS (${meta.env})`, |
157 | | - `Assuming role`, |
158 | | - `Validating permissions`, |
159 | | - `Building artifacts`, |
160 | | - `Deploying ${meta.repo}`, |
161 | | - `Verifying rollout`, |
162 | | - `Done` |
| 209 | + "Connecting to GitHub...", |
| 210 | + "Committing workflow file...", |
| 211 | + "Verifying commit...", |
| 212 | + "Done ✅" |
163 | 213 | ]; |
164 | 214 | let i = 0; |
165 | 215 | const timer = setInterval(() => { |
166 | 216 | if (i >= steps.length) return; |
167 | | - const level = i === steps.length - 1 ? "info" : "info"; |
168 | | - onEvent({ ts: new Date().toISOString(), level, msg: steps[i++] }); |
| 217 | + onEvent({ ts: new Date().toISOString(), level: "info", msg: steps[i++] }); |
169 | 218 | if (i >= steps.length) clearInterval(timer); |
170 | | - }, 800); |
| 219 | + }, 600); |
171 | 220 | return () => clearInterval(timer); |
172 | 221 | }, |
173 | 222 | }; |
|
0 commit comments