@@ -744,7 +744,7 @@ func getJobLogData(ctx context.Context, client *github.Client, owner, repo strin
744
744
745
745
// downloadLogContent downloads the actual log content from a GitHub logs URL
746
746
func downloadLogContent (logURL string , tailLines int ) (string , int , * http.Response , error ) {
747
- httpResp , err := http .Get (logURL ) //nolint:gosec // URLs are provided by GitHub API and are safe
747
+ httpResp , err := http .Get (logURL ) //nolint:gosec
748
748
if err != nil {
749
749
return "" , 0 , httpResp , fmt .Errorf ("failed to download logs: %w" , err )
750
750
}
@@ -758,27 +758,35 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
758
758
tailLines = 1000
759
759
}
760
760
761
- lines := make ([]string , 0 , tailLines )
762
- scanner := bufio .NewScanner (httpResp .Body )
761
+ const maxMemoryBytes = 1024 * 1024
762
+ var lines []string
763
+ var currentMemoryUsage int
764
+ totalLines := 0
763
765
764
- buf := make ([] byte , 0 , 64 * 1024 )
765
- scanner .Buffer (buf , 1024 * 1024 )
766
+ scanner := bufio . NewScanner ( httpResp . Body )
767
+ scanner .Buffer (make ([] byte , 0 , 64 * 1024 ) , 1024 * 1024 )
766
768
767
769
for scanner .Scan () {
768
770
line := scanner .Text ()
769
- lines = append (lines , line )
771
+ totalLines ++
772
+ lineSize := len (line ) + 1
770
773
771
- if len (lines ) > tailLines {
774
+ // Remove old lines if we exceed memory limit or line count limit
775
+ for (currentMemoryUsage + lineSize > maxMemoryBytes || len (lines ) >= tailLines ) && len (lines ) > 0 {
776
+ removedLineSize := len (lines [0 ]) + 1
777
+ currentMemoryUsage -= removedLineSize
772
778
lines = lines [1 :]
773
779
}
780
+
781
+ lines = append (lines , line )
782
+ currentMemoryUsage += lineSize
774
783
}
775
784
776
785
if err := scanner .Err (); err != nil {
777
786
return "" , 0 , httpResp , fmt .Errorf ("failed to read log content: %w" , err )
778
787
}
779
788
780
- content := strings .Join (lines , "\n " )
781
- return content , len (lines ), httpResp , nil
789
+ return strings .Join (lines , "\n " ), totalLines , httpResp , nil
782
790
}
783
791
784
792
// RerunWorkflowRun creates a tool to re-run an entire workflow run
0 commit comments