refactor(agent-task/capi): improve pagination
Signed-off-by: Babak K. Shandiz <babakks@github.com>
This commit is contained in:
parent
f3c3797d5c
commit
0138bf3dab
1 changed files with 25 additions and 17 deletions
|
|
@ -16,7 +16,7 @@ import (
|
|||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
const defaultSessionsPerPage = 50
|
||||
var defaultSessionsPerPage = 50
|
||||
|
||||
// session is an in-flight agent task
|
||||
type session struct {
|
||||
|
|
@ -65,20 +65,23 @@ type Session struct {
|
|||
// ListSessionsForViewer lists all agent sessions for the
|
||||
// authenticated user up to limit.
|
||||
func (c *CAPIClient) ListSessionsForViewer(ctx context.Context, limit int) ([]*Session, error) {
|
||||
if limit == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
url := baseCAPIURL + "/agents/sessions"
|
||||
pageSize := defaultSessionsPerPage
|
||||
|
||||
var sessions []session
|
||||
page := 1
|
||||
perPage := defaultSessionsPerPage
|
||||
sessions := make([]session, 0, limit+pageSize)
|
||||
|
||||
for {
|
||||
for page := 1; ; page++ {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Set("page_size", strconv.Itoa(perPage))
|
||||
q.Set("page_size", strconv.Itoa(pageSize))
|
||||
q.Set("page_number", strconv.Itoa(page))
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
|
|
@ -96,11 +99,11 @@ func (c *CAPIClient) ListSessionsForViewer(ctx context.Context, limit int) ([]*S
|
|||
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode sessions response: %w", err)
|
||||
}
|
||||
if len(response.Sessions) == 0 || len(sessions) >= limit {
|
||||
|
||||
sessions = append(sessions, response.Sessions...)
|
||||
if len(response.Sessions) < pageSize || len(sessions) >= limit {
|
||||
break
|
||||
}
|
||||
sessions = append(sessions, response.Sessions...)
|
||||
page++
|
||||
}
|
||||
|
||||
// Drop any above the limit
|
||||
|
|
@ -123,20 +126,23 @@ func (c *CAPIClient) ListSessionsForRepo(ctx context.Context, owner string, repo
|
|||
return nil, fmt.Errorf("owner and repo are required")
|
||||
}
|
||||
|
||||
if limit == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/agents/sessions/nwo/%s/%s", baseCAPIURL, url.PathEscape(owner), url.PathEscape(repo))
|
||||
pageSize := defaultSessionsPerPage
|
||||
|
||||
var sessions []session
|
||||
page := 1
|
||||
perPage := defaultSessionsPerPage
|
||||
sessions := make([]session, 0, limit+pageSize)
|
||||
|
||||
for {
|
||||
for page := 1; ; page++ {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Set("page_size", strconv.Itoa(perPage))
|
||||
q.Set("page_size", strconv.Itoa(pageSize))
|
||||
q.Set("page_number", strconv.Itoa(page))
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
|
|
@ -154,11 +160,11 @@ func (c *CAPIClient) ListSessionsForRepo(ctx context.Context, owner string, repo
|
|||
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode sessions response: %w", err)
|
||||
}
|
||||
if len(response.Sessions) == 0 || len(sessions) >= limit {
|
||||
|
||||
sessions = append(sessions, response.Sessions...)
|
||||
if len(response.Sessions) < pageSize || len(sessions) >= limit {
|
||||
break
|
||||
}
|
||||
sessions = append(sessions, response.Sessions...)
|
||||
page++
|
||||
}
|
||||
|
||||
// Drop any above the limit
|
||||
|
|
@ -194,10 +200,12 @@ func (c *CAPIClient) hydrateSessionPullRequests(sessions []session) ([]*Session,
|
|||
|
||||
var resp struct {
|
||||
Nodes []struct {
|
||||
TypeName string `graphql:"__typename"`
|
||||
PullRequest sessionPullRequest `graphql:"... on PullRequest"`
|
||||
} `graphql:"nodes(ids: $ids)"`
|
||||
}
|
||||
|
||||
// TODO handle pagination
|
||||
host, _ := c.authCfg.DefaultHost()
|
||||
err := apiClient.Query(host, "FetchPRsForAgentTaskSessions", &resp, map[string]any{
|
||||
"ids": prNodeIds,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue