From dcadeb75d49a71a3937272dfc6a64d5eb7414fdd Mon Sep 17 00:00:00 2001 From: "Babak K. Shandiz" Date: Fri, 5 Sep 2025 11:17:13 +0100 Subject: [PATCH] feat(agent-task/capi): add `GetSession` method Signed-off-by: Babak K. Shandiz --- pkg/cmd/agent-task/capi/sessions.go | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pkg/cmd/agent-task/capi/sessions.go b/pkg/cmd/agent-task/capi/sessions.go index f53028941..e0252a8bb 100644 --- a/pkg/cmd/agent-task/capi/sessions.go +++ b/pkg/cmd/agent-task/capi/sessions.go @@ -5,6 +5,7 @@ import ( "context" "encoding/base64" "encoding/json" + "errors" "fmt" "net/http" "net/url" @@ -18,6 +19,8 @@ import ( var defaultSessionsPerPage = 50 +var ErrSessionNotFound = errors.New("not found") + // session is an in-flight agent task type session struct { ID string `json:"id"` @@ -197,6 +200,45 @@ func (c *CAPIClient) ListSessionsForRepo(ctx context.Context, owner string, repo return result, nil } +// GetSession retrieves a specific agent session by ID. +func (c *CAPIClient) GetSession(ctx context.Context, id string) (*Session, error) { + if id == "" { + return nil, fmt.Errorf("missing session ID") + } + + url := fmt.Sprintf("%s/agents/sessions/%s", baseCAPIURL, url.PathEscape(id)) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) + if err != nil { + return nil, err + } + + res, err := c.httpClient.Do(req) + if err != nil { + return nil, err + } + + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + if res.StatusCode == http.StatusNotFound { + return nil, ErrSessionNotFound + } + return nil, fmt.Errorf("failed to get session: %s", res.Status) + } + + var rawSession session + if err := json.NewDecoder(res.Body).Decode(&rawSession); err != nil { + return nil, fmt.Errorf("failed to decode session response: %w", err) + } + + sessions, err := c.hydrateSessionPullRequestsAndUsers([]session{rawSession}) + if err != nil { + return nil, fmt.Errorf("failed to fetch session resources: %w", err) + } + + return sessions[0], nil +} + // hydrateSessionPullRequestsAndUsers hydrates pull request and user information in sessions func (c *CAPIClient) hydrateSessionPullRequestsAndUsers(sessions []session) ([]*Session, error) { if len(sessions) == 0 {