runPlanChecks triggers plan checks and polls for results within the poll budget.
(ctx context.Context, planName string)
| 347 | |
| 348 | // runPlanChecks triggers plan checks and polls for results within the poll budget. |
| 349 | func (s *Server) runPlanChecks(ctx context.Context, planName string) *PlanCheckInfo { |
| 350 | checkRunName := planName + "/planCheckRun" |
| 351 | |
| 352 | // Trigger plan checks. If this fails, still poll — CreatePlan already |
| 353 | // initializes plan checks server-side, so they may be running regardless. |
| 354 | _, _ = s.apiRequest(ctx, "/bytebase.v1.PlanService/RunPlanChecks", map[string]any{ |
| 355 | "name": planName, |
| 356 | }) |
| 357 | |
| 358 | pendingResult := &PlanCheckInfo{ |
| 359 | Status: planCheckRunning, |
| 360 | PlanCheckRun: checkRunName, |
| 361 | } |
| 362 | |
| 363 | // Poll for results. Transient errors (404 before row visible, brief 500) |
| 364 | // are retried within the budget rather than bailing immediately. |
| 365 | deadline := time.Now().Add(s.planCheckBudget()) |
| 366 | for time.Now().Before(deadline) { |
| 367 | if ctx.Err() != nil { |
| 368 | return pendingResult |
| 369 | } |
| 370 | |
| 371 | pollResp, err := s.apiRequest(ctx, "/bytebase.v1.PlanService/GetPlanCheckRun", map[string]any{ |
| 372 | "name": checkRunName, |
| 373 | }) |
| 374 | if err != nil || pollResp.Status >= 400 { |
| 375 | time.Sleep(planCheckPollInterval) |
| 376 | continue |
| 377 | } |
| 378 | |
| 379 | var checkRun planCheckRunResponse |
| 380 | if err := json.Unmarshal(pollResp.Body, &checkRun); err != nil { |
| 381 | time.Sleep(planCheckPollInterval) |
| 382 | continue |
| 383 | } |
| 384 | |
| 385 | switch checkRun.Status { |
| 386 | case "DONE": |
| 387 | return buildPlanCheckInfo(checkRun) |
| 388 | case "FAILED", "CANCELED": |
| 389 | return &PlanCheckInfo{Status: planCheckFailed} |
| 390 | default: |
| 391 | // RUNNING or unknown — keep polling. |
| 392 | } |
| 393 | |
| 394 | time.Sleep(planCheckPollInterval) |
| 395 | } |
| 396 | |
| 397 | // Budget exhausted. |
| 398 | return pendingResult |
| 399 | } |
| 400 | |
| 401 | // planCheckRunResponse mirrors the PlanCheckRun proto response. |
| 402 | type planCheckRunResponse struct { |
no test coverage detected