Skip to content

Commit 8a5efb9

Browse files
committed
add sliding window for actions logs
1 parent 33a63a0 commit 8a5efb9

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

pkg/github/actions.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package github
22

33
import (
4+
"bufio"
45
"context"
56
"encoding/json"
67
"fmt"
7-
"io"
88
"net/http"
99
"strconv"
1010
"strings"
@@ -754,16 +754,36 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
754754
return "", 0, httpResp, fmt.Errorf("failed to download logs: HTTP %d", httpResp.StatusCode)
755755
}
756756

757-
content, err := io.ReadAll(httpResp.Body)
758-
if err != nil {
759-
return "", 0, httpResp, fmt.Errorf("failed to read log content: %w", err)
757+
// content, err := io.ReadAll(httpResp.Body)
758+
// if err != nil {
759+
// return "", 0, httpResp, fmt.Errorf("failed to read log content: %w", err)
760+
// }
761+
762+
if tailLines <= 0 {
763+
tailLines = 1000
760764
}
761765

762-
// Clean up and format the log content for better readability
763-
logContent := strings.TrimSpace(string(content))
766+
lines := make([]string, 0, tailLines)
767+
scanner := bufio.NewScanner(httpResp.Body)
768+
769+
buf := make([]byte, 0, 64*1024)
770+
scanner.Buffer(buf, 1024*1024)
771+
772+
for scanner.Scan() {
773+
line := scanner.Text()
774+
lines = append(lines, line)
775+
776+
if len(lines) > tailLines {
777+
lines = lines[len(lines)-tailLines:]
778+
}
779+
}
780+
781+
if err := scanner.Err(); err != nil {
782+
return "", 0, httpResp, fmt.Errorf("failed to read log content: %w", err)
783+
}
764784

765-
trimmedContent, lineCount := trimContent(logContent, tailLines)
766-
return trimmedContent, lineCount, httpResp, nil
785+
content := strings.Join(lines, "\n")
786+
return content, len(lines), httpResp, nil
767787
}
768788

769789
// trimContent trims the content to a maximum length and returns the trimmed content and an original length

0 commit comments

Comments
 (0)