fix(agent-task/capi): remove fallback to viewer when user id is zero
Signed-off-by: Babak K. Shandiz <babakks@github.com>
This commit is contained in:
parent
eba31343a8
commit
597cdaf081
2 changed files with 5 additions and 123 deletions
|
|
@ -330,10 +330,6 @@ func (c *CAPIClient) hydrateSessionPullRequestsAndUsers(sessions []session) ([]*
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
// When the session is fetched via the resources endpoint, the session user
|
||||
// ID can be zero, which means it's the viewer user.
|
||||
var includeViewer bool
|
||||
|
||||
prNodeIds := make([]string, 0, len(sessions))
|
||||
userNodeIds := make([]string, 0, len(sessions))
|
||||
for _, session := range sessions {
|
||||
|
|
@ -349,13 +345,9 @@ func (c *CAPIClient) hydrateSessionPullRequestsAndUsers(sessions []session) ([]*
|
|||
}
|
||||
}
|
||||
|
||||
if session.UserID != 0 {
|
||||
userNodeId := generateUserNodeID(session.UserID)
|
||||
if !slices.Contains(userNodeIds, userNodeId) {
|
||||
userNodeIds = append(userNodeIds, userNodeId)
|
||||
}
|
||||
} else {
|
||||
includeViewer = true
|
||||
userNodeId := generateUserNodeID(session.UserID)
|
||||
if !slices.Contains(userNodeIds, userNodeId) {
|
||||
userNodeIds = append(userNodeIds, userNodeId)
|
||||
}
|
||||
}
|
||||
apiClient := api.NewClientFromHTTP(c.httpClient)
|
||||
|
|
@ -366,7 +358,6 @@ func (c *CAPIClient) hydrateSessionPullRequestsAndUsers(sessions []session) ([]*
|
|||
PullRequest sessionPullRequest `graphql:"... on PullRequest"`
|
||||
User api.GitHubUser `graphql:"... on User"`
|
||||
} `graphql:"nodes(ids: $ids)"`
|
||||
Viewer api.GitHubUser `graphql:"viewer @include(if: $includeViewer)"`
|
||||
}
|
||||
|
||||
ids := make([]string, 0, len(prNodeIds)+len(userNodeIds))
|
||||
|
|
@ -376,8 +367,7 @@ func (c *CAPIClient) hydrateSessionPullRequestsAndUsers(sessions []session) ([]*
|
|||
// TODO handle pagination
|
||||
host, _ := c.authCfg.DefaultHost()
|
||||
err := apiClient.Query(host, "FetchPRsAndUsersForAgentTaskSessions", &resp, map[string]any{
|
||||
"ids": ids,
|
||||
"includeViewer": includeViewer,
|
||||
"ids": ids,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -413,12 +403,7 @@ func (c *CAPIClient) hydrateSessionPullRequestsAndUsers(sessions []session) ([]*
|
|||
for _, s := range sessions {
|
||||
newSession := fromAPISession(s)
|
||||
newSession.PullRequest = prMap[strconv.FormatInt(s.ResourceID, 10)]
|
||||
if s.UserID != 0 {
|
||||
newSession.User = userMap[s.UserID]
|
||||
} else {
|
||||
newSession.UserID = resp.Viewer.DatabaseID
|
||||
newSession.User = &resp.Viewer
|
||||
}
|
||||
newSession.User = userMap[s.UserID]
|
||||
newSessions = append(newSessions, newSession)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1300,108 +1300,6 @@ func TestListSessionsByResourceID(t *testing.T) {
|
|||
sampleDateString,
|
||||
), func(q string, vars map[string]interface{}) {
|
||||
assert.Equal(t, []interface{}{"PR_kwDNA-jNB9A", "U_kgAB"}, vars["ids"])
|
||||
assert.Equal(t, false, vars["includeViewer"])
|
||||
}),
|
||||
)
|
||||
},
|
||||
wantOut: []*Session{
|
||||
{
|
||||
ID: "sess1",
|
||||
CreatedAt: time.Time{},
|
||||
LastUpdatedAt: sampleDate,
|
||||
Name: "Build artifacts",
|
||||
UserID: 1,
|
||||
State: "completed",
|
||||
ResourceType: "pull",
|
||||
ResourceID: 2000,
|
||||
PullRequest: &api.PullRequest{
|
||||
ID: "PR_node",
|
||||
FullDatabaseID: "2000",
|
||||
Number: 42,
|
||||
Title: "Improve docs",
|
||||
State: "OPEN",
|
||||
IsDraft: true,
|
||||
URL: "https://github.com/OWNER/REPO/pull/42",
|
||||
Body: "",
|
||||
CreatedAt: sampleDate,
|
||||
UpdatedAt: sampleDate,
|
||||
Repository: &api.PRRepository{
|
||||
NameWithOwner: "OWNER/REPO",
|
||||
},
|
||||
},
|
||||
User: &api.GitHubUser{
|
||||
Login: "octocat",
|
||||
Name: "Octocat",
|
||||
DatabaseID: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "single session with zero user ID",
|
||||
limit: 10,
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.WithHost(httpmock.REST("GET", "agents/resource/pull/999"), "api.githubcopilot.com"),
|
||||
httpmock.StringResponse(heredoc.Docf(`
|
||||
{
|
||||
"id": "resource:pull:2000",
|
||||
"user_id": 0,
|
||||
"resource_global_id": "PR_kwDNA-jNB9A",
|
||||
"resource_type": "pull",
|
||||
"resource_id": 2000,
|
||||
"session_count": 1,
|
||||
"last_updated_at": %[1]d,
|
||||
"state": "completed",
|
||||
"resource_state": "draft",
|
||||
"sessions": [
|
||||
{
|
||||
"id": "sess1",
|
||||
"name": "Build artifacts",
|
||||
"state": "completed",
|
||||
"last_updated_at": %[1]d
|
||||
}
|
||||
]
|
||||
}`,
|
||||
sampleDateTimestamp,
|
||||
)),
|
||||
)
|
||||
// GraphQL hydration
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`query FetchPRsAndUsersForAgentTaskSessions\b`),
|
||||
httpmock.GraphQLQuery(heredoc.Docf(`
|
||||
{
|
||||
"data": {
|
||||
"nodes": [
|
||||
{
|
||||
"__typename": "PullRequest",
|
||||
"id": "PR_node",
|
||||
"fullDatabaseId": "2000",
|
||||
"number": 42,
|
||||
"title": "Improve docs",
|
||||
"state": "OPEN",
|
||||
"isDraft": true,
|
||||
"url": "https://github.com/OWNER/REPO/pull/42",
|
||||
"body": "",
|
||||
"createdAt": "%[1]s",
|
||||
"updatedAt": "%[1]s",
|
||||
"repository": {
|
||||
"nameWithOwner": "OWNER/REPO"
|
||||
}
|
||||
}
|
||||
],
|
||||
"viewer": {
|
||||
"login": "octocat",
|
||||
"name": "Octocat",
|
||||
"databaseId": 1
|
||||
}
|
||||
}
|
||||
}`,
|
||||
sampleDateString,
|
||||
), func(q string, vars map[string]interface{}) {
|
||||
// Should not fetch user by ID since it's zero
|
||||
assert.Equal(t, []interface{}{"PR_kwDNA-jNB9A"}, vars["ids"])
|
||||
assert.Equal(t, true, vars["includeViewer"])
|
||||
}),
|
||||
)
|
||||
},
|
||||
|
|
@ -1525,7 +1423,6 @@ func TestListSessionsByResourceID(t *testing.T) {
|
|||
sampleDateString,
|
||||
), func(q string, vars map[string]interface{}) {
|
||||
assert.Equal(t, []interface{}{"PR_kwDNA-jNB9A", "U_kgAB"}, vars["ids"])
|
||||
assert.Equal(t, false, vars["includeViewer"])
|
||||
}),
|
||||
)
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue