Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/gitprovider/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/google/go-github/v69/github"
Expand Down Expand Up @@ -49,3 +50,17 @@ func TestGitHubProviderFindPRByCommit(t *testing.T) {
t.Fatalf("PR = %+v, want merged PR #56 for feature/deleted-remote -> main", pr)
}
}

func TestFetchLogTailReadsPastFirst64KiB(t *testing.T) {
body := strings.Repeat("setup success\n", 6000) + "actual failure\nexit status 1\n"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(body))
}))
t.Cleanup(server.Close)

got := fetchLogTail(server.URL, 2)
want := "actual failure\nexit status 1"
if got != want {
t.Fatalf("fetchLogTail() = %q, want %q", got, want)
}
}
39 changes: 29 additions & 10 deletions internal/gitprovider/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gitprovider

import (
"bufio"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -86,14 +87,7 @@ func isFailedStatus(s string) bool {
}

func tailString(s string, n int) string {
if n <= 0 {
return ""
}
lines := strings.Split(s, "\n")
if len(lines) > n {
lines = lines[len(lines)-n:]
}
return strings.Join(lines, "\n")
return tailReader(strings.NewReader(s), n)
}

func fetchLogTail(url string, lines int) string {
Expand All @@ -102,6 +96,31 @@ func fetchLogTail(url string, lines int) string {
return ""
}
defer resp.Body.Close()
data, _ := io.ReadAll(io.LimitReader(resp.Body, 64*1024))
return tailString(string(data), lines)
return tailReader(resp.Body, lines)
}

func tailReader(r io.Reader, maxLines int) string {
if maxLines <= 0 {
return ""
}

recent := make([]string, 0, maxLines)
reader := bufio.NewReader(r)
for {
line, err := reader.ReadString('\n')
if line != "" {
line = strings.TrimRight(line, "\r\n")
if len(recent) == maxLines {
copy(recent, recent[1:])
recent[len(recent)-1] = line
} else {
recent = append(recent, line)
}
}
if err != nil {
break
}
}

return strings.Join(recent, "\n")
}
Loading