MCPcopy Create free account
hub / github.com/cli/cli / ListSessionsByResourceID

Method ListSessionsByResourceID

pkg/cmd/agent-task/capi/sessions.go:368–423  ·  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

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

Calls 6

JoinPathWithHostPrefixFunction · 0.92
ErrorfMethod · 0.65
StringMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65