MCPcopy Create free account
hub / github.com/assetnote/newtowner / pollForWorkflowRun

Method pollForWorkflowRun

internal/providers/github/github.go:413–469  ·  view source on GitHub ↗

pollForWorkflowRun waits for a workflow run triggered with a specific input to complete. It identifies the correct run by looking for runIDTag in the workflow run's display name.

(ctx context.Context, runIDTag string)

Source from the content-addressed store, hash-verified

411// pollForWorkflowRun waits for a workflow run triggered with a specific input to complete.
412// It identifies the correct run by looking for runIDTag in the workflow run's display name.
413func (p *Provider) pollForWorkflowRun(ctx context.Context, runIDTag string) (*github.WorkflowRun, error) {
414 startTime := time.Now()
415 pollInterval := 5 * time.Second
416 maxWaitTime := 10 * time.Minute
417
418 if p.WorkflowID == 0 {
419 return nil, fmt.Errorf("WorkflowID is not set in Provider struct. Cannot poll for runs")
420 }
421
422 log.Printf("[GitHub Polling] Starting to poll for workflow runs for Workflow ID %d, expecting tag '%s' in run name", p.WorkflowID, runIDTag)
423
424 for time.Since(startTime) < maxWaitTime {
425 listOpts := &github.ListWorkflowRunsOptions{
426 Event: "workflow_dispatch",
427 ListOptions: github.ListOptions{PerPage: 15},
428 }
429
430 workflowRuns, _, err := p.client.Actions.ListWorkflowRunsByID(ctx, p.Owner, p.Repo, p.WorkflowID, listOpts)
431 if err != nil {
432 log.Printf("[GitHub Polling] Error listing workflow runs for Workflow ID %d: %v. Retrying in %s...", p.WorkflowID, err, pollInterval)
433 time.Sleep(pollInterval)
434 continue
435 }
436
437 if workflowRuns == nil || len(workflowRuns.WorkflowRuns) == 0 {
438 log.Printf("[GitHub Polling] No workflow runs found yet for Workflow ID %d with specified filters. Waiting %s...", p.WorkflowID, pollInterval)
439 time.Sleep(pollInterval)
440 continue
441 }
442
443 for _, run := range workflowRuns.WorkflowRuns {
444 if strings.Contains(run.GetName(), runIDTag) {
445 status := run.GetStatus()
446 conclusion := run.GetConclusion()
447
448 log.Printf("[GitHub Polling] Found candidate run ID %d with name '%s' (matching tag '%s'). Status: %s, Conclusion: %s",
449 run.GetID(), run.GetName(), runIDTag, status, conclusion)
450
451 if status == "completed" {
452 log.Printf("[GitHub Polling] Tagged run ID %d was already completed. Conclusion: '%s'. Returning it directly.", run.GetID(), conclusion)
453 return run, nil
454 } else if status == "queued" || status == "in_progress" || status == "waiting" {
455 log.Printf("[GitHub Polling] Tagged run ID %d is active (status: %s). Will monitor this one.", run.GetID(), status)
456 return p.monitorSpecificRun(ctx, run.GetID())
457 } else {
458 log.Printf("[GitHub Polling] Tagged run ID %d found in unexpected terminal state '%s', conclusion '%s'. Returning it.", run.GetID(), status, conclusion)
459 return run, nil
460 }
461 }
462 }
463
464 log.Printf("[GitHub Polling] No run found with tag '%s' in its name yet among the latest %d runs for Workflow ID %d. Waiting %s...", runIDTag, len(workflowRuns.WorkflowRuns), p.WorkflowID, pollInterval)
465 time.Sleep(pollInterval)
466 }
467
468 return nil, fmt.Errorf("timed out after %s waiting for workflow run with tag '%s' in its name to appear for Workflow ID %d", maxWaitTime, runIDTag, p.WorkflowID)
469}
470

Callers 1

CheckURLsMethod · 0.95

Calls 1

monitorSpecificRunMethod · 0.95

Tested by

no test coverage detected