MCPcopy Create free account
hub / github.com/github/gh-aw / parseAndDisplayRunnerGuardOutput

Function parseAndDisplayRunnerGuardOutput

pkg/cli/runner_guard.go:145–269  ·  view source on GitHub ↗

parseAndDisplayRunnerGuardOutput parses runner-guard JSON output and displays findings. Returns the total number of findings found.

(stdout string, verbose bool, gitRoot string)

Source from the content-addressed store, hash-verified

143// parseAndDisplayRunnerGuardOutput parses runner-guard JSON output and displays findings.
144// Returns the total number of findings found.
145func parseAndDisplayRunnerGuardOutput(stdout string, verbose bool, gitRoot string) (int, error) {
146 if stdout == "" {
147 return 0, nil // No output means no findings
148 }
149
150 trimmed := strings.TrimSpace(stdout)
151 if !strings.HasPrefix(trimmed, "{") && !strings.HasPrefix(trimmed, "[") {
152 if len(trimmed) > 0 {
153 return 0, fmt.Errorf("unexpected runner-guard output format: %s", trimmed)
154 }
155 return 0, nil
156 }
157
158 var output runnerGuardOutput
159 if err := json.Unmarshal([]byte(stdout), &output); err != nil {
160 return 0, fmt.Errorf("failed to parse runner-guard JSON output: %w", err)
161 }
162
163 totalFindings := len(output.Findings)
164 if totalFindings == 0 {
165 return 0, nil
166 }
167
168 // Display score/grade if present
169 if output.Score > 0 || output.Grade != "" {
170 fmt.Fprintf(os.Stderr, "%s\n", console.FormatInfoMessage(
171 fmt.Sprintf("Runner-Guard Score: %d/100 (Grade: %s)", output.Score, output.Grade),
172 ))
173 }
174
175 // Group findings by file for better readability
176 findingsByFile := make(map[string][]runnerGuardFinding)
177 for _, finding := range output.Findings {
178 findingsByFile[finding.File] = append(findingsByFile[finding.File], finding)
179 }
180
181 // Display findings for each file
182 for filePath, findings := range findingsByFile {
183 // Validate and sanitize file path to prevent path traversal
184 cleanPath := filepath.Clean(filePath)
185
186 absPath := cleanPath
187 if !filepath.IsAbs(cleanPath) {
188 absPath = filepath.Join(gitRoot, cleanPath)
189 }
190
191 absGitRoot, err := filepath.Abs(gitRoot)
192 if err != nil {
193 runnerGuardLog.Printf("Failed to get absolute path for git root: %v", err)
194 continue
195 }
196
197 absPath, err = filepath.Abs(absPath)
198 if err != nil {
199 runnerGuardLog.Printf("Failed to get absolute path for %s: %v", filePath, err)
200 continue
201 }
202

Calls 5

FormatInfoMessageFunction · 0.92
FormatErrorFunction · 0.92
ToLowerMethod · 0.80
ErrorfMethod · 0.45
PrintfMethod · 0.45