fix(agent-task/capi): add Error field to Session

Signed-off-by: Babak K. Shandiz <babakks@github.com>
This commit is contained in:
Babak K. Shandiz 2025-09-18 15:04:06 +01:00
parent 211632771e
commit 3ed9034ff8
No known key found for this signature in database
GPG key ID: 9472CAEFF56C742E
2 changed files with 130 additions and 1 deletions

View file

@ -43,6 +43,10 @@ type session struct {
EventURL string `json:"event_url"`
EventType string `json:"event_type"`
PremiumRequests float64 `json:"premium_requests"`
Error *struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error,omitempty"`
}
// A shim of a full pull request because looking up by node ID
@ -83,11 +87,17 @@ type Session struct {
EventURL string
EventType string
PremiumRequests float64
Error *SessionError
PullRequest *api.PullRequest
User *api.GitHubUser
}
type SessionError struct {
Code string
Message string
}
// ListLatestSessionsForViewer lists all agent sessions for the
// authenticated user up to limit.
func (c *CAPIClient) ListLatestSessionsForViewer(ctx context.Context, limit int) ([]*Session, error) {
@ -442,7 +452,7 @@ func generateUserNodeID(userID int64) string {
}
func fromAPISession(s session) *Session {
return &Session{
result := Session{
ID: s.ID,
Name: s.Name,
UserID: s.UserID,
@ -460,4 +470,11 @@ func fromAPISession(s session) *Session {
EventType: s.EventType,
PremiumRequests: s.PremiumRequests,
}
if s.Error != nil {
result.Error = &SessionError{
Code: s.Error.Code,
Message: s.Error.Message,
}
}
return &result
}

View file

@ -875,6 +875,118 @@ func TestListLatestSessionsForViewer(t *testing.T) {
},
},
},
{
name: "session error is included",
limit: 10,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(
httpmock.QueryMatcher("GET", "agents/sessions", url.Values{
"page_number": {"1"},
"page_size": {"50"},
"sort": {"last_updated_at,desc"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(heredoc.Docf(`
{
"sessions": [
{
"id": "sessA",
"name": "Build artifacts",
"user_id": 1,
"agent_id": 2,
"logs": "",
"state": "failed",
"owner_id": 10,
"repo_id": 1000,
"resource_type": "pull",
"resource_id": 3000,
"created_at": "%[1]s",
"error": {
"code": "some-error-code",
"message": "some-error-message"
}
}
]
}`,
sampleDateString,
)),
)
// GraphQL hydration
reg.Register(
httpmock.GraphQL(`query FetchPRsAndUsersForAgentTaskSessions\b`),
httpmock.GraphQLQuery(heredoc.Docf(`
{
"data": {
"nodes": [
{
"__typename": "PullRequest",
"id": "PR_node3000",
"fullDatabaseId": "3000",
"number": 100,
"title": "Improve docs",
"state": "OPEN",
"isDraft": true,
"url": "https://github.com/OWNER/REPO/pull/100",
"body": "",
"createdAt": "%[1]s",
"updatedAt": "%[1]s",
"repository": {"nameWithOwner": "OWNER/REPO"}
},
{
"__typename": "User",
"login": "octocat",
"name": "Octocat",
"databaseId": 1
}
]
}
}`,
sampleDateString,
), func(q string, vars map[string]interface{}) {
// Expected encoded node IDs for resource IDs 3000 and user octocat
assert.Equal(t, []interface{}{"PR_kwDNA-jNC7g", "U_kgAB"}, vars["ids"])
}),
)
},
wantOut: []*Session{
{
ID: "sessA",
Name: "Build artifacts",
UserID: 1,
AgentID: 2,
Logs: "",
State: "failed",
OwnerID: 10,
RepoID: 1000,
ResourceType: "pull",
ResourceID: 3000,
CreatedAt: sampleDate,
Error: &SessionError{
Code: "some-error-code",
Message: "some-error-message",
},
PullRequest: &api.PullRequest{
ID: "PR_node3000",
FullDatabaseID: "3000",
Number: 100,
Title: "Improve docs",
State: "OPEN",
IsDraft: true,
URL: "https://github.com/OWNER/REPO/pull/100",
Body: "",
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
Repository: &api.PRRepository{
NameWithOwner: "OWNER/REPO",
},
},
User: &api.GitHubUser{Login: "octocat", Name: "Octocat", DatabaseID: 1},
},
},
},
{
name: "API error",
limit: 10,