MCPcopy
hub / github.com/cli/cli / ListSessionsByResourceID

Method ListSessionsByResourceID

pkg/cmd/agent-task/capi/sessions.go:365–417  ·  view source on GitHub ↗

ListSessionsByResourceID retrieves sessions associated with the given resource type and ID.

(ctx context.Context, resourceType string, resourceID int64, limit int)

Source from the content-addressed store, hash-verified

363
364// ListSessionsByResourceID retrieves sessions associated with the given resource type and ID.
365func (c *CAPIClient) ListSessionsByResourceID(ctx context.Context, resourceType string, resourceID int64, limit int) ([]*Session, error) {
366 if resourceType == "" || resourceID == 0 {
367 return nil, fmt.Errorf("missing resource type/ID")
368 }
369
370 if limit == 0 {
371 return nil, nil
372 }
373
374 url := fmt.Sprintf("%s/agents/resource/%s/%d", c.capiBaseURL, url.PathEscape(resourceType), resourceID)
375
376 req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
377 if err != nil {
378 return nil, err
379 }
380
381 res, err := c.httpClient.Do(req)
382 if err != nil {
383 return nil, err
384 }
385 defer res.Body.Close()
386 if res.StatusCode != http.StatusOK {
387 return nil, fmt.Errorf("failed to list sessions: %s", res.Status)
388 }
389
390 var response resource
391 if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
392 return nil, fmt.Errorf("failed to decode sessions response: %w", err)
393 }
394
395 sessions := make([]session, 0, len(response.Sessions))
396 for _, s := range response.Sessions {
397 session := session{
398 ID: s.SessionID,
399 Name: s.Name,
400 UserID: int64(response.UserID),
401 ResourceType: response.ResourceType,
402 ResourceID: response.ResourceID,
403 ResourceGlobalID: response.ResourceGlobalID,
404 State: s.SessionState,
405 }
406 if s.SessionLastUpdatedAt != 0 {
407 session.LastUpdatedAt = time.Unix(s.SessionLastUpdatedAt, 0).UTC()
408 }
409 sessions = append(sessions, session)
410 }
411
412 result, err := c.hydrateSessionPullRequestsAndUsers(sessions)
413 if err != nil {
414 return nil, fmt.Errorf("failed to fetch session resources: %w", err)
415 }
416 return result, nil
417}
418
419// hydrateSessionPullRequestsAndUsers hydrates pull request and user information in sessions
420func (c *CAPIClient) hydrateSessionPullRequestsAndUsers(sessions []session) ([]*Session, error) {

Calls 4

ErrorfMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65