Skip to content

Commit 4e43327

Browse files
committed
extract ring buffer logic to new package
1 parent 9d273b9 commit 4e43327

File tree

2 files changed

+56
-43
lines changed

2 files changed

+56
-43
lines changed

pkg/buffer/buffer.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package buffer
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
)
9+
10+
func ProcessAsRingBufferToEnd(httpResp *http.Response, maxJobLogLines int) (string, int, *http.Response, error) {
11+
lines := make([]string, maxJobLogLines)
12+
validLines := make([]bool, maxJobLogLines)
13+
totalLines := 0
14+
writeIndex := 0
15+
16+
scanner := bufio.NewScanner(httpResp.Body)
17+
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
18+
19+
for scanner.Scan() {
20+
line := scanner.Text()
21+
totalLines++
22+
23+
lines[writeIndex] = line
24+
validLines[writeIndex] = true
25+
writeIndex = (writeIndex + 1) % maxJobLogLines
26+
}
27+
28+
if err := scanner.Err(); err != nil {
29+
return "", 0, httpResp, fmt.Errorf("failed to read log content: %w", err)
30+
}
31+
32+
var result []string
33+
linesInBuffer := totalLines
34+
if linesInBuffer > maxJobLogLines {
35+
linesInBuffer = maxJobLogLines
36+
}
37+
38+
startIndex := 0
39+
if totalLines > maxJobLogLines {
40+
startIndex = writeIndex
41+
}
42+
43+
for i := 0; i < linesInBuffer; i++ {
44+
idx := (startIndex + i) % maxJobLogLines
45+
if validLines[idx] {
46+
result = append(result, lines[idx])
47+
}
48+
}
49+
50+
return strings.Join(result, "\n"), totalLines, httpResp, nil
51+
}

pkg/github/actions.go

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package github
22

33
import (
4-
"bufio"
54
"context"
65
"encoding/json"
76
"fmt"
87
"net/http"
98
"strconv"
10-
"strings"
119

10+
buffer "github.com/github/github-mcp-server/pkg/buffer"
1211
ghErrors "github.com/github/github-mcp-server/pkg/errors"
1312
"github.com/github/github-mcp-server/pkg/translations"
1413
"github.com/google/go-github/v74/github"
@@ -758,50 +757,13 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
758757
tailLines = 1000
759758
}
760759

761-
lines := make([]string, maxJobLogLines)
762-
validLines := make([]bool, maxJobLogLines)
763-
totalLines := 0
764-
writeIndex := 0
760+
processedInput, totalLines, httpResp, err := buffer.ProcessAsRingBufferToEnd(httpResp, tailLines)
765761

766-
scanner := bufio.NewScanner(httpResp.Body)
767-
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
768-
769-
for scanner.Scan() {
770-
line := scanner.Text()
771-
totalLines++
772-
773-
lines[writeIndex] = line
774-
validLines[writeIndex] = true
775-
writeIndex = (writeIndex + 1) % maxJobLogLines
776-
}
777-
778-
if err := scanner.Err(); err != nil {
779-
return "", 0, httpResp, fmt.Errorf("failed to read log content: %w", err)
780-
}
781-
782-
var result []string
783-
linesInBuffer := totalLines
784-
if linesInBuffer > maxJobLogLines {
785-
linesInBuffer = maxJobLogLines
786-
}
787-
788-
startIndex := 0
789-
if totalLines > maxJobLogLines {
790-
startIndex = writeIndex
791-
}
792-
793-
for i := 0; i < linesInBuffer; i++ {
794-
idx := (startIndex + i) % maxJobLogLines
795-
if validLines[idx] {
796-
result = append(result, lines[idx])
797-
}
798-
}
799-
800-
if len(result) > tailLines {
801-
result = result[len(result)-tailLines:]
762+
if len(processedInput) > tailLines {
763+
processedInput = processedInput[len(processedInput)-tailLines:]
802764
}
803765

804-
finalResult := strings.Join(result, "\n")
766+
finalResult := processedInput
805767

806768
return finalResult, totalLines, httpResp, nil
807769
}

0 commit comments

Comments
 (0)