fetchGitHubWorkflows fetches workflow information from GitHub
(repoOverride string, verbose bool)
| 68 | |
| 69 | // fetchGitHubWorkflows fetches workflow information from GitHub |
| 70 | func fetchGitHubWorkflows(repoOverride string, verbose bool) (map[string]*GitHubWorkflow, error) { |
| 71 | workflowsLog.Printf("Fetching GitHub workflows: repoOverride=%s", repoOverride) |
| 72 | |
| 73 | // Start spinner for network operation (only if not in verbose mode) |
| 74 | spinner := console.NewSpinner("Fetching GitHub workflow status...") |
| 75 | if !verbose { |
| 76 | spinner.Start() |
| 77 | } |
| 78 | |
| 79 | args := []string{"workflow", "list", "--all", "--json", "id,name,path,state"} |
| 80 | if repoOverride != "" { |
| 81 | args = append(args, "--repo", repoOverride) |
| 82 | } |
| 83 | cmd := workflow.ExecGH(args...) |
| 84 | output, err := cmd.Output() |
| 85 | |
| 86 | if err != nil { |
| 87 | // Stop spinner on error |
| 88 | if !verbose { |
| 89 | spinner.Stop() |
| 90 | } |
| 91 | |
| 92 | // Extract detailed error information including exit code and stderr |
| 93 | var exitCode int |
| 94 | var stderr string |
| 95 | var exitErr *exec.ExitError |
| 96 | if errors.As(err, &exitErr) { |
| 97 | exitCode = exitErr.ExitCode() |
| 98 | stderr = string(exitErr.Stderr) |
| 99 | workflowsLog.Printf("gh workflow list command failed with exit code %d. Command: gh %v", exitCode, args) |
| 100 | workflowsLog.Printf("stderr output: %s", stderr) |
| 101 | |
| 102 | return nil, fmt.Errorf("failed to execute gh workflow list command (exit code %d): %w. stderr: %s", exitCode, err, stderr) |
| 103 | } |
| 104 | |
| 105 | // If not an ExitError, log what we can |
| 106 | workflowsLog.Printf("gh workflow list command failed with error (not ExitError): %v. Command: gh %v", err, args) |
| 107 | return nil, fmt.Errorf("failed to execute gh workflow list command: %w", err) |
| 108 | } |
| 109 | |
| 110 | // Check if output is empty |
| 111 | if len(output) == 0 { |
| 112 | if !verbose { |
| 113 | spinner.Stop() |
| 114 | } |
| 115 | return nil, errors.New("gh workflow list returned empty output - check if repository has workflows and gh CLI is authenticated") |
| 116 | } |
| 117 | |
| 118 | // Validate JSON before unmarshaling |
| 119 | if !json.Valid(output) { |
| 120 | if !verbose { |
| 121 | spinner.Stop() |
| 122 | } |
| 123 | return nil, errors.New("gh workflow list returned invalid JSON - this may be due to network issues or authentication problems") |
| 124 | } |
| 125 | |
| 126 | var workflows []GitHubWorkflow |
| 127 | if err := json.Unmarshal(output, &workflows); err != nil { |
no test coverage detected