extractRelevantCode reads code files referenced in stack traces.
(localPath string, relevantPaths []string, stackTraces []StackTrace)
| 621 | |
| 622 | // extractRelevantCode reads code files referenced in stack traces. |
| 623 | func extractRelevantCode(localPath string, relevantPaths []string, stackTraces []StackTrace) []CodeSnippet { |
| 624 | var snippets []CodeSnippet |
| 625 | |
| 626 | for _, trace := range stackTraces { |
| 627 | for _, frame := range trace.Frames { |
| 628 | filePath, lineNum := parseStackFrame(frame, trace.Language) |
| 629 | if filePath == "" || lineNum == 0 { |
| 630 | continue |
| 631 | } |
| 632 | |
| 633 | // Try to find the file in the repo |
| 634 | fullPath := findFileInRepo(localPath, filePath, relevantPaths) |
| 635 | if fullPath == "" { |
| 636 | continue |
| 637 | } |
| 638 | |
| 639 | // Read the file and extract context around the line |
| 640 | content, err := readLinesAround(localPath, fullPath, lineNum, 5) |
| 641 | if err != nil { |
| 642 | continue |
| 643 | } |
| 644 | |
| 645 | rel, _ := filepath.Rel(localPath, fullPath) |
| 646 | snippets = append(snippets, CodeSnippet{ |
| 647 | FilePath: rel, |
| 648 | Language: trace.Language, |
| 649 | StartLine: lineNum - 5, |
| 650 | EndLine: lineNum + 5, |
| 651 | Content: content, |
| 652 | Reason: fmt.Sprintf("Referenced in %s stack trace: %s", trace.Language, trace.ExceptionType), |
| 653 | }) |
| 654 | |
| 655 | if len(snippets) >= 5 { // Limit to 5 snippets |
| 656 | return snippets |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return snippets |
| 662 | } |
| 663 | |
| 664 | // parseStackFrame extracts file path and line number from a stack frame string. |
| 665 | func parseStackFrame(frame, language string) (string, int) { |