Accept consistent overlaps in CI pipeline pagination
This commit is contained in:
@@ -349,6 +349,12 @@ human and JSON reports. Keep secret-redaction assertions.
|
||||
|
||||
## 8. Pagination assumes a stable snapshot of live CI history
|
||||
|
||||
**Completed:** Pagination deduplicates consistent immutable identities while
|
||||
retaining first-seen order and updated status observations. Partial and whole-page
|
||||
overlap continue to later pages; conflicting identities fail, and a non-advancing
|
||||
feed remains cancelable without an arbitrary duplicate-page limit. Woodpecker
|
||||
and rehearsal suites pass.
|
||||
|
||||
**Priority:** P2. **Stages:** 10, 12, 13.
|
||||
|
||||
**Relevant code:**
|
||||
|
||||
@@ -150,7 +150,12 @@ func (c Client) MatchingPipelines(ctx context.Context, tag, commit string) ([]Pi
|
||||
return nil, errors.New("exact tag and full commit are required")
|
||||
}
|
||||
var matches []Pipeline
|
||||
seen := map[int64]bool{}
|
||||
type pipelineIdentity struct {
|
||||
number int64
|
||||
ref, commit, event string
|
||||
}
|
||||
seen := map[int64]pipelineIdentity{}
|
||||
indices := map[int64]int{}
|
||||
for page := 1; ; page++ {
|
||||
query := url.Values{"page": {strconv.Itoa(page)}, "perPage": {"50"}, "event": {"tag"}, "ref": {"refs/tags/" + tag}}
|
||||
var items []Pipeline
|
||||
@@ -167,11 +172,19 @@ func (c Client) MatchingPipelines(ctx context.Context, tag, commit string) ([]Pi
|
||||
if item.ID <= 0 || item.Number <= 0 || item.Ref == "" || item.Commit == "" || item.Event == "" || item.Status == "" {
|
||||
return nil, &Error{Operation: "list pipelines", Cause: errors.New("pipeline response omits required identity")}
|
||||
}
|
||||
if seen[item.ID] {
|
||||
return nil, &Error{Operation: "list pipelines", Cause: errors.New("pagination repeated a pipeline")}
|
||||
identity := pipelineIdentity{item.Number, item.Ref, strings.ToLower(item.Commit), item.Event}
|
||||
if previous, exists := seen[item.ID]; exists {
|
||||
if previous != identity {
|
||||
return nil, &Error{Operation: "list pipelines", Cause: errors.New("pagination returned conflicting pipeline identity")}
|
||||
}
|
||||
if index, matched := indices[item.ID]; matched {
|
||||
matches[index] = item
|
||||
}
|
||||
continue
|
||||
}
|
||||
seen[item.ID] = true
|
||||
seen[item.ID] = identity
|
||||
if item.Event == "tag" && item.Ref == "refs/tags/"+tag && git.EqualObjectID(item.Commit, commit) {
|
||||
indices[item.ID] = len(matches)
|
||||
matches = append(matches, item)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -112,3 +113,71 @@ func TestJobsPreserveMultipleMatchesAndUnknownStates(t *testing.T) {
|
||||
t.Fatal("accepted missing step")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPipelinePaginationAcceptsOverlapAndStatusProgress(t *testing.T) {
|
||||
commit := strings.Repeat("a", 40)
|
||||
pipeline := func(id int64) Pipeline {
|
||||
return Pipeline{ID: id, Number: id, Event: "tag", Ref: "refs/tags/target", Commit: commit, Status: "running"}
|
||||
}
|
||||
first, second := []Pipeline{}, []Pipeline{}
|
||||
for id := int64(1); id <= 50; id++ {
|
||||
first = append(first, pipeline(id))
|
||||
}
|
||||
for id := int64(50); id <= 99; id++ {
|
||||
second = append(second, pipeline(id))
|
||||
}
|
||||
second[0].Status = "success"
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
pages [][]Pipeline
|
||||
want int
|
||||
}{
|
||||
{"overlapping page", [][]Pipeline{first, second, {pipeline(100)}}, 100},
|
||||
{"whole page overlap", [][]Pipeline{first, first, {pipeline(51)}}, 51},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
||||
if page > len(test.pages) {
|
||||
fmt.Fprint(w, `[]`)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(test.pages[page-1])
|
||||
}))
|
||||
defer server.Close()
|
||||
matches, err := (Client{BaseURL: server.URL, RepositoryID: 1, Token: "token"}).MatchingPipelines(t.Context(), "target", commit)
|
||||
if err != nil || len(matches) != test.want {
|
||||
t.Fatalf("count=%d want=%d error=%v", len(matches), test.want, err)
|
||||
}
|
||||
if test.name == "overlapping page" && matches[49].Status != "success" {
|
||||
t.Fatalf("status progression lost: %+v", matches[49])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPipelinePaginationRejectsConflictsAndCancelsNonProgress(t *testing.T) {
|
||||
commit := strings.Repeat("a", 40)
|
||||
for _, conflict := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("conflict=%t", conflict), func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
||||
item := Pipeline{ID: 1, Number: 1, Event: "tag", Ref: "refs/tags/target", Commit: commit, Status: "running"}
|
||||
if page > 1 && conflict {
|
||||
item.Number = 2
|
||||
}
|
||||
if page == 3 {
|
||||
cancel()
|
||||
}
|
||||
json.NewEncoder(w).Encode([]Pipeline{item})
|
||||
}))
|
||||
defer server.Close()
|
||||
_, err := (Client{BaseURL: server.URL, RepositoryID: 1, Token: "token"}).MatchingPipelines(ctx, "target", commit)
|
||||
if err == nil || !conflict && !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("error=%v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user