Merge pull request #11668 from cli/babakks/refactor-agent-task-tests

Refactor `agent-task` tests
This commit is contained in:
Kynan Ware 2025-09-05 09:44:24 -06:00 committed by GitHub
commit a1dbafebef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 2145 additions and 881 deletions

View file

@ -7,6 +7,8 @@ import (
"github.com/cli/cli/v2/internal/gh"
)
//go:generate moq -rm -out client_mock.go . CapiClient
const baseCAPIURL = "https://api.githubcopilot.com"
const capiHost = "api.githubcopilot.com"

View file

@ -0,0 +1,273 @@
// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq
package capi
import (
"context"
"sync"
)
// Ensure, that CapiClientMock does implement CapiClient.
// If this is not the case, regenerate this file with moq.
var _ CapiClient = &CapiClientMock{}
// CapiClientMock is a mock implementation of CapiClient.
//
// func TestSomethingThatUsesCapiClient(t *testing.T) {
//
// // make and configure a mocked CapiClient
// mockedCapiClient := &CapiClientMock{
// CreateJobFunc: func(ctx context.Context, owner string, repo string, problemStatement string, baseBranch string) (*Job, error) {
// panic("mock out the CreateJob method")
// },
// GetJobFunc: func(ctx context.Context, owner string, repo string, jobID string) (*Job, error) {
// panic("mock out the GetJob method")
// },
// ListSessionsForRepoFunc: func(ctx context.Context, owner string, repo string, limit int) ([]*Session, error) {
// panic("mock out the ListSessionsForRepo method")
// },
// ListSessionsForViewerFunc: func(ctx context.Context, limit int) ([]*Session, error) {
// panic("mock out the ListSessionsForViewer method")
// },
// }
//
// // use mockedCapiClient in code that requires CapiClient
// // and then make assertions.
//
// }
type CapiClientMock struct {
// CreateJobFunc mocks the CreateJob method.
CreateJobFunc func(ctx context.Context, owner string, repo string, problemStatement string, baseBranch string) (*Job, error)
// GetJobFunc mocks the GetJob method.
GetJobFunc func(ctx context.Context, owner string, repo string, jobID string) (*Job, error)
// ListSessionsForRepoFunc mocks the ListSessionsForRepo method.
ListSessionsForRepoFunc func(ctx context.Context, owner string, repo string, limit int) ([]*Session, error)
// ListSessionsForViewerFunc mocks the ListSessionsForViewer method.
ListSessionsForViewerFunc func(ctx context.Context, limit int) ([]*Session, error)
// calls tracks calls to the methods.
calls struct {
// CreateJob holds details about calls to the CreateJob method.
CreateJob []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// Owner is the owner argument value.
Owner string
// Repo is the repo argument value.
Repo string
// ProblemStatement is the problemStatement argument value.
ProblemStatement string
// BaseBranch is the baseBranch argument value.
BaseBranch string
}
// GetJob holds details about calls to the GetJob method.
GetJob []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// Owner is the owner argument value.
Owner string
// Repo is the repo argument value.
Repo string
// JobID is the jobID argument value.
JobID string
}
// ListSessionsForRepo holds details about calls to the ListSessionsForRepo method.
ListSessionsForRepo []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// Owner is the owner argument value.
Owner string
// Repo is the repo argument value.
Repo string
// Limit is the limit argument value.
Limit int
}
// ListSessionsForViewer holds details about calls to the ListSessionsForViewer method.
ListSessionsForViewer []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// Limit is the limit argument value.
Limit int
}
}
lockCreateJob sync.RWMutex
lockGetJob sync.RWMutex
lockListSessionsForRepo sync.RWMutex
lockListSessionsForViewer sync.RWMutex
}
// CreateJob calls CreateJobFunc.
func (mock *CapiClientMock) CreateJob(ctx context.Context, owner string, repo string, problemStatement string, baseBranch string) (*Job, error) {
if mock.CreateJobFunc == nil {
panic("CapiClientMock.CreateJobFunc: method is nil but CapiClient.CreateJob was just called")
}
callInfo := struct {
Ctx context.Context
Owner string
Repo string
ProblemStatement string
BaseBranch string
}{
Ctx: ctx,
Owner: owner,
Repo: repo,
ProblemStatement: problemStatement,
BaseBranch: baseBranch,
}
mock.lockCreateJob.Lock()
mock.calls.CreateJob = append(mock.calls.CreateJob, callInfo)
mock.lockCreateJob.Unlock()
return mock.CreateJobFunc(ctx, owner, repo, problemStatement, baseBranch)
}
// CreateJobCalls gets all the calls that were made to CreateJob.
// Check the length with:
//
// len(mockedCapiClient.CreateJobCalls())
func (mock *CapiClientMock) CreateJobCalls() []struct {
Ctx context.Context
Owner string
Repo string
ProblemStatement string
BaseBranch string
} {
var calls []struct {
Ctx context.Context
Owner string
Repo string
ProblemStatement string
BaseBranch string
}
mock.lockCreateJob.RLock()
calls = mock.calls.CreateJob
mock.lockCreateJob.RUnlock()
return calls
}
// GetJob calls GetJobFunc.
func (mock *CapiClientMock) GetJob(ctx context.Context, owner string, repo string, jobID string) (*Job, error) {
if mock.GetJobFunc == nil {
panic("CapiClientMock.GetJobFunc: method is nil but CapiClient.GetJob was just called")
}
callInfo := struct {
Ctx context.Context
Owner string
Repo string
JobID string
}{
Ctx: ctx,
Owner: owner,
Repo: repo,
JobID: jobID,
}
mock.lockGetJob.Lock()
mock.calls.GetJob = append(mock.calls.GetJob, callInfo)
mock.lockGetJob.Unlock()
return mock.GetJobFunc(ctx, owner, repo, jobID)
}
// GetJobCalls gets all the calls that were made to GetJob.
// Check the length with:
//
// len(mockedCapiClient.GetJobCalls())
func (mock *CapiClientMock) GetJobCalls() []struct {
Ctx context.Context
Owner string
Repo string
JobID string
} {
var calls []struct {
Ctx context.Context
Owner string
Repo string
JobID string
}
mock.lockGetJob.RLock()
calls = mock.calls.GetJob
mock.lockGetJob.RUnlock()
return calls
}
// ListSessionsForRepo calls ListSessionsForRepoFunc.
func (mock *CapiClientMock) ListSessionsForRepo(ctx context.Context, owner string, repo string, limit int) ([]*Session, error) {
if mock.ListSessionsForRepoFunc == nil {
panic("CapiClientMock.ListSessionsForRepoFunc: method is nil but CapiClient.ListSessionsForRepo was just called")
}
callInfo := struct {
Ctx context.Context
Owner string
Repo string
Limit int
}{
Ctx: ctx,
Owner: owner,
Repo: repo,
Limit: limit,
}
mock.lockListSessionsForRepo.Lock()
mock.calls.ListSessionsForRepo = append(mock.calls.ListSessionsForRepo, callInfo)
mock.lockListSessionsForRepo.Unlock()
return mock.ListSessionsForRepoFunc(ctx, owner, repo, limit)
}
// ListSessionsForRepoCalls gets all the calls that were made to ListSessionsForRepo.
// Check the length with:
//
// len(mockedCapiClient.ListSessionsForRepoCalls())
func (mock *CapiClientMock) ListSessionsForRepoCalls() []struct {
Ctx context.Context
Owner string
Repo string
Limit int
} {
var calls []struct {
Ctx context.Context
Owner string
Repo string
Limit int
}
mock.lockListSessionsForRepo.RLock()
calls = mock.calls.ListSessionsForRepo
mock.lockListSessionsForRepo.RUnlock()
return calls
}
// ListSessionsForViewer calls ListSessionsForViewerFunc.
func (mock *CapiClientMock) ListSessionsForViewer(ctx context.Context, limit int) ([]*Session, error) {
if mock.ListSessionsForViewerFunc == nil {
panic("CapiClientMock.ListSessionsForViewerFunc: method is nil but CapiClient.ListSessionsForViewer was just called")
}
callInfo := struct {
Ctx context.Context
Limit int
}{
Ctx: ctx,
Limit: limit,
}
mock.lockListSessionsForViewer.Lock()
mock.calls.ListSessionsForViewer = append(mock.calls.ListSessionsForViewer, callInfo)
mock.lockListSessionsForViewer.Unlock()
return mock.ListSessionsForViewerFunc(ctx, limit)
}
// ListSessionsForViewerCalls gets all the calls that were made to ListSessionsForViewer.
// Check the length with:
//
// len(mockedCapiClient.ListSessionsForViewerCalls())
func (mock *CapiClientMock) ListSessionsForViewerCalls() []struct {
Ctx context.Context
Limit int
} {
var calls []struct {
Ctx context.Context
Limit int
}
mock.lockListSessionsForViewer.RLock()
calls = mock.calls.ListSessionsForViewer
mock.lockListSessionsForViewer.RUnlock()
return calls
}

View file

@ -94,7 +94,11 @@ func (c *CAPIClient) CreateJob(ctx context.Context, owner, repo, problemStatemen
}
if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusOK { // accept 201 or 200
return nil, fmt.Errorf("failed to create job: %s", j.ErrorInfo.Message)
if j.ErrorInfo != nil {
return nil, fmt.Errorf("failed to create job: %s", j.ErrorInfo.Message)
}
statusText := fmt.Sprintf("%d %s", res.StatusCode, http.StatusText(res.StatusCode))
return nil, fmt.Errorf("failed to create job: %s", statusText)
}
return &j, nil

View file

@ -0,0 +1,369 @@
package capi
import (
"context"
"net/http"
"testing"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetJobRequiresRepoAndJobID(t *testing.T) {
client := &CAPIClient{}
_, err := client.GetJob(context.Background(), "", "", "only-job-id")
assert.EqualError(t, err, "owner, repo, and jobID are required")
_, err = client.GetJob(context.Background(), "", "only-repo", "")
assert.EqualError(t, err, "owner, repo, and jobID are required")
_, err = client.GetJob(context.Background(), "only-owner", "", "")
assert.EqualError(t, err, "owner, repo, and jobID are required")
_, err = client.GetJob(context.Background(), "", "", "")
assert.EqualError(t, err, "owner, repo, and jobID are required")
}
func TestGetJob(t *testing.T) {
sampleDateString := "2025-08-29T00:00:00Z"
sampleDate, err := time.Parse(time.RFC3339, sampleDateString)
require.NoError(t, err)
tests := []struct {
name string
httpStubs func(*testing.T, *httpmock.Registry)
wantErr string
wantOut *Job
}{
{
name: "job without PR",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("GET", "agents/swe/v1/jobs/OWNER/REPO/job123"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(200, heredoc.Docf(`
{
"job_id": "job123",
"session_id": "sess1",
"problem_statement": "Do the thing",
"event_type": "foo",
"content_filter_mode": "foo",
"status": "foo",
"result": "foo",
"actor": {
"id": 1,
"login": "octocat"
},
"created_at": "%[1]s",
"updated_at": "%[1]s"
}`,
sampleDateString,
)),
)
},
wantOut: &Job{
ID: "job123",
SessionID: "sess1",
ProblemStatement: "Do the thing",
EventType: "foo",
ContentFilterMode: "foo",
Status: "foo",
Result: "foo",
Actor: &JobActor{
ID: 1,
Login: "octocat",
},
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
},
},
{
name: "job with PR",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("GET", "agents/swe/v1/jobs/OWNER/REPO/job123"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(200, heredoc.Docf(`
{
"job_id": "job123",
"session_id": "sess1",
"problem_statement": "Do the thing",
"event_type": "foo",
"content_filter_mode": "foo",
"status": "foo",
"result": "foo",
"actor": {
"id": 1,
"login": "octocat"
},
"created_at": "%[1]s",
"updated_at": "%[1]s",
"pull_request": {
"id": 101,
"number": 42
}
}`,
sampleDateString,
)),
)
},
wantOut: &Job{
ID: "job123",
SessionID: "sess1",
ProblemStatement: "Do the thing",
EventType: "foo",
ContentFilterMode: "foo",
Status: "foo",
Result: "foo",
Actor: &JobActor{
ID: 1,
Login: "octocat",
},
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
PullRequest: &JobPullRequest{
ID: 101,
Number: 42,
},
},
},
{
name: "job not found",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("GET", "agents/swe/v1/jobs/OWNER/REPO/job123"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(404, `{}`),
)
},
wantErr: "failed to get job: 404 Not Found",
},
{
name: "API error",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("GET", "agents/swe/v1/jobs/OWNER/REPO/job123"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(500, `{}`),
)
},
wantErr: "failed to get job: 500 Internal Server Error",
},
{
name: "invalid JSON response",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("GET", "agents/swe/v1/jobs/OWNER/REPO/job123"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(200, ``),
)
},
wantErr: "failed to decode get job response: EOF",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reg := &httpmock.Registry{}
if tt.httpStubs != nil {
tt.httpStubs(t, reg)
}
defer reg.Verify(t)
httpClient := &http.Client{Transport: reg}
cfg := config.NewBlankConfig()
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
job, err := capiClient.GetJob(context.Background(), "OWNER", "REPO", "job123")
if tt.wantErr != "" {
require.EqualError(t, err, tt.wantErr)
require.Nil(t, job)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantOut, job)
})
}
}
func TestCreateJobRequiresRepoAndProblemStatement(t *testing.T) {
client := &CAPIClient{}
_, err := client.CreateJob(context.Background(), "", "only-repo", "", "")
assert.EqualError(t, err, "owner and repo are required")
_, err = client.CreateJob(context.Background(), "only-owner", "", "", "")
assert.EqualError(t, err, "owner and repo are required")
_, err = client.CreateJob(context.Background(), "", "", "", "")
assert.EqualError(t, err, "owner and repo are required")
_, err = client.CreateJob(context.Background(), "owner", "repo", "", "")
assert.EqualError(t, err, "problem statement is required")
}
func TestCreateJob(t *testing.T) {
sampleDateString := "2025-08-29T00:00:00Z"
sampleDate, err := time.Parse(time.RFC3339, sampleDateString)
require.NoError(t, err)
tests := []struct {
name string
baseBranch string
httpStubs func(*testing.T, *httpmock.Registry)
wantErr string
wantOut *Job
}{
{
name: "success",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.RESTPayload(201,
heredoc.Docf(`
{
"job_id": "job123",
"session_id": "sess1",
"problem_statement": "Do the thing",
"event_type": "foo",
"content_filter_mode": "foo",
"status": "foo",
"result": "foo",
"actor": {
"id": 1,
"login": "octocat"
},
"created_at": "%[1]s",
"updated_at": "%[1]s"
}
`, sampleDateString),
func(payload map[string]interface{}) {
assert.Equal(t, "Do the thing", payload["problem_statement"])
assert.Equal(t, "gh_cli", payload["event_type"])
},
),
)
},
wantOut: &Job{
ID: "job123",
SessionID: "sess1",
ProblemStatement: "Do the thing",
EventType: "foo",
ContentFilterMode: "foo",
Status: "foo",
Result: "foo",
Actor: &JobActor{
ID: 1,
Login: "octocat",
},
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
},
},
{
name: "success with base branch",
baseBranch: "some-branch",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.RESTPayload(201,
heredoc.Docf(`
{
"job_id": "job123",
"session_id": "sess1",
"problem_statement": "Do the thing",
"event_type": "foo",
"content_filter_mode": "foo",
"status": "foo",
"result": "foo",
"actor": {
"id": 1,
"login": "octocat"
},
"created_at": "%[1]s",
"updated_at": "%[1]s"
}
`, sampleDateString),
func(payload map[string]interface{}) {
assert.Equal(t, "Do the thing", payload["problem_statement"])
assert.Equal(t, "gh_cli", payload["event_type"])
assert.Equal(t, "refs/heads/some-branch", payload["pull_request"].(map[string]interface{})["base_ref"])
},
),
)
},
wantOut: &Job{
ID: "job123",
SessionID: "sess1",
ProblemStatement: "Do the thing",
EventType: "foo",
ContentFilterMode: "foo",
Status: "foo",
Result: "foo",
Actor: &JobActor{
ID: 1,
Login: "octocat",
},
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
},
},
{
name: "API error, included in response body",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(500, heredoc.Doc(`{
"error": {
"message": "some error"
}
}`)),
)
},
wantErr: "failed to create job: some error",
},
{
name: "API error",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(500, `{}`),
)
},
wantErr: "failed to create job: 500 Internal Server Error",
},
{
name: "invalid JSON response",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(200, ``),
)
},
wantErr: "failed to decode create job response: EOF",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reg := &httpmock.Registry{}
if tt.httpStubs != nil {
tt.httpStubs(t, reg)
}
defer reg.Verify(t)
httpClient := &http.Client{Transport: reg}
cfg := config.NewBlankConfig()
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
job, err := capiClient.CreateJob(context.Background(), "OWNER", "REPO", "Do the thing", tt.baseBranch)
if tt.wantErr != "" {
require.EqualError(t, err, tt.wantErr)
require.Nil(t, job)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantOut, job)
})
}
}

View file

@ -16,7 +16,7 @@ import (
"github.com/vmihailenco/msgpack/v5"
)
const defaultSessionsPerPage = 50
var defaultSessionsPerPage = 50
// session is an in-flight agent task
type session struct {
@ -58,27 +58,45 @@ type sessionPullRequest struct {
// Session is a hydrated in-flight agent task
type Session struct {
session
PullRequest *api.PullRequest `json:"-"`
ID string
Name string
UserID uint64
AgentID int64
Logs string
State string
OwnerID uint64
RepoID uint64
ResourceType string
ResourceID int64
LastUpdatedAt time.Time
CreatedAt time.Time
CompletedAt time.Time
EventURL string
EventType string
PullRequest *api.PullRequest
}
// ListSessionsForViewer lists all agent sessions for the
// authenticated user up to limit.
func (c *CAPIClient) ListSessionsForViewer(ctx context.Context, limit int) ([]*Session, error) {
if limit == 0 {
return nil, nil
}
url := baseCAPIURL + "/agents/sessions"
pageSize := defaultSessionsPerPage
var sessions []session
page := 1
perPage := defaultSessionsPerPage
sessions := make([]session, 0, limit+pageSize)
for {
for page := 1; ; page++ {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Set("page_size", strconv.Itoa(perPage))
q.Set("page_size", strconv.Itoa(pageSize))
q.Set("page_number", strconv.Itoa(page))
req.URL.RawQuery = q.Encode()
@ -96,11 +114,11 @@ func (c *CAPIClient) ListSessionsForViewer(ctx context.Context, limit int) ([]*S
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode sessions response: %w", err)
}
if len(response.Sessions) == 0 || len(sessions) >= limit {
sessions = append(sessions, response.Sessions...)
if len(response.Sessions) < pageSize || len(sessions) >= limit {
break
}
sessions = append(sessions, response.Sessions...)
page++
}
// Drop any above the limit
@ -111,7 +129,7 @@ func (c *CAPIClient) ListSessionsForViewer(ctx context.Context, limit int) ([]*S
// Hydrate the result with pull request data.
result, err := c.hydrateSessionPullRequests(sessions)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to fetch session resources: %w", err)
}
return result, nil
@ -123,20 +141,23 @@ func (c *CAPIClient) ListSessionsForRepo(ctx context.Context, owner string, repo
return nil, fmt.Errorf("owner and repo are required")
}
if limit == 0 {
return nil, nil
}
url := fmt.Sprintf("%s/agents/sessions/nwo/%s/%s", baseCAPIURL, url.PathEscape(owner), url.PathEscape(repo))
pageSize := defaultSessionsPerPage
var sessions []session
page := 1
perPage := defaultSessionsPerPage
sessions := make([]session, 0, limit+pageSize)
for {
for page := 1; ; page++ {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Set("page_size", strconv.Itoa(perPage))
q.Set("page_size", strconv.Itoa(pageSize))
q.Set("page_number", strconv.Itoa(page))
req.URL.RawQuery = q.Encode()
@ -154,11 +175,11 @@ func (c *CAPIClient) ListSessionsForRepo(ctx context.Context, owner string, repo
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode sessions response: %w", err)
}
if len(response.Sessions) == 0 || len(sessions) >= limit {
sessions = append(sessions, response.Sessions...)
if len(response.Sessions) < pageSize || len(sessions) >= limit {
break
}
sessions = append(sessions, response.Sessions...)
page++
}
// Drop any above the limit
@ -169,7 +190,7 @@ func (c *CAPIClient) ListSessionsForRepo(ctx context.Context, owner string, repo
// Hydrate the result with pull request data.
result, err := c.hydrateSessionPullRequests(sessions)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to fetch session resources: %w", err)
}
return result, nil
}
@ -194,10 +215,12 @@ func (c *CAPIClient) hydrateSessionPullRequests(sessions []session) ([]*Session,
var resp struct {
Nodes []struct {
TypeName string `graphql:"__typename"`
PullRequest sessionPullRequest `graphql:"... on PullRequest"`
} `graphql:"nodes(ids: $ids)"`
}
// TODO handle pagination
host, _ := c.authCfg.DefaultHost()
err := apiClient.Query(host, "FetchPRsForAgentTaskSessions", &resp, map[string]any{
"ids": prNodeIds,
@ -227,10 +250,9 @@ func (c *CAPIClient) hydrateSessionPullRequests(sessions []session) ([]*Session,
newSessions := make([]*Session, 0, len(sessions))
for _, s := range sessions {
newSessions = append(newSessions, &Session{
session: s,
PullRequest: prMap[strconv.FormatInt(s.ResourceID, 10)],
})
newSession := fromAPISession(s)
newSession.PullRequest = prMap[strconv.FormatInt(s.ResourceID, 10)]
newSessions = append(newSessions, newSession)
}
return newSessions, nil
@ -253,3 +275,23 @@ func generatePullRequestNodeID(repoID, pullRequestID int64) string {
return "PR_" + encoded
}
func fromAPISession(s session) *Session {
return &Session{
ID: s.ID,
Name: s.Name,
UserID: s.UserID,
AgentID: s.AgentID,
Logs: s.Logs,
State: s.State,
OwnerID: s.OwnerID,
RepoID: s.RepoID,
ResourceType: s.ResourceType,
ResourceID: s.ResourceID,
LastUpdatedAt: s.LastUpdatedAt,
CreatedAt: s.CreatedAt,
CompletedAt: s.CompletedAt,
EventURL: s.EventURL,
EventType: s.EventType,
}
}

View file

@ -0,0 +1,811 @@
package capi
import (
"context"
"net/http"
"net/url"
"testing"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListSessionsForViewer(t *testing.T) {
sampleDateString := "2025-08-29T00:00:00Z"
sampleDate, err := time.Parse(time.RFC3339, sampleDateString)
require.NoError(t, err)
tests := []struct {
name string
perPage int
limit int
httpStubs func(*testing.T, *httpmock.Registry)
wantErr string
wantOut []*Session
}{
{
name: "zero limit",
limit: 0,
wantOut: nil,
},
{
name: "no sessions",
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"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(`{"sessions":[]}`),
)
},
wantOut: nil,
},
{
name: "single session",
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"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(heredoc.Docf(`
{
"sessions": [
{
"id": "sess1",
"name": "Build artifacts",
"user_id": 1,
"agent_id": 2,
"logs": "",
"state": "completed",
"owner_id": 10,
"repo_id": 1000,
"resource_type": "pull",
"resource_id": 2000,
"created_at": "%[1]s"
}
]
}`,
sampleDateString,
)),
)
// GraphQL hydration
reg.Register(
httpmock.GraphQL(`query FetchPRsForAgentTaskSessions\b`),
httpmock.GraphQLQuery(heredoc.Docf(`
{
"data": {
"nodes": [
{
"__typename": "PullRequest",
"id": "PR_node",
"fullDatabaseId": "2000",
"number": 42,
"title": "Improve docs",
"state": "OPEN",
"url": "https://github.com/OWNER/REPO/pull/42",
"body": "",
"createdAt": "%[1]s",
"updatedAt": "%[1]s",
"repository": {
"nameWithOwner": "OWNER/REPO"
}
}
]
}
}`,
sampleDateString,
), func(q string, vars map[string]interface{}) {
assert.Equal(t, []interface{}{"PR_kwDNA-jNB9A"}, vars["ids"])
}),
)
},
wantOut: []*Session{
{
ID: "sess1",
Name: "Build artifacts",
UserID: 1,
AgentID: 2,
Logs: "",
State: "completed",
OwnerID: 10,
RepoID: 1000,
ResourceType: "pull",
ResourceID: 2000,
CreatedAt: sampleDate,
PullRequest: &api.PullRequest{
ID: "PR_node",
FullDatabaseID: "2000",
Number: 42,
Title: "Improve docs",
State: "OPEN",
URL: "https://github.com/OWNER/REPO/pull/42",
Body: "",
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
Repository: &api.PRRepository{
NameWithOwner: "OWNER/REPO",
},
},
},
},
},
{
name: "multiple sessions, paginated",
perPage: 1, // to enforce pagination
limit: 2,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(
httpmock.QueryMatcher("GET", "agents/sessions", url.Values{
"page_number": {"1"},
"page_size": {"1"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(heredoc.Docf(`
{
"sessions": [
{
"id": "sess1",
"name": "Build artifacts",
"user_id": 1,
"agent_id": 2,
"logs": "",
"state": "completed",
"owner_id": 10,
"repo_id": 1000,
"resource_type": "pull",
"resource_id": 2000,
"created_at": "%[1]s"
}
]
}`,
sampleDateString,
)),
)
// Second page
reg.Register(
httpmock.WithHost(
httpmock.QueryMatcher("GET", "agents/sessions", url.Values{
"page_number": {"2"},
"page_size": {"1"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(heredoc.Docf(`
{
"sessions": [
{
"id": "sess2",
"name": "Build artifacts",
"user_id": 1,
"agent_id": 2,
"logs": "",
"state": "completed",
"owner_id": 10,
"repo_id": 1000,
"resource_type": "pull",
"resource_id": 2001,
"created_at": "%[1]s"
}
]
}`,
sampleDateString,
)),
)
// GraphQL hydration
reg.Register(
httpmock.GraphQL(`query FetchPRsForAgentTaskSessions\b`),
httpmock.GraphQLQuery(heredoc.Docf(`
{
"data": {
"nodes": [
{
"__typename": "PullRequest",
"id": "PR_node",
"fullDatabaseId": "2000",
"number": 42,
"title": "Improve docs",
"state": "OPEN",
"url": "https://github.com/OWNER/REPO/pull/42",
"body": "",
"createdAt": "%[1]s",
"updatedAt": "%[1]s",
"repository": {
"nameWithOwner": "OWNER/REPO"
}
},
{
"__typename": "PullRequest",
"id": "PR_node",
"fullDatabaseId": "2001",
"number": 43,
"title": "Improve docs",
"state": "OPEN",
"url": "https://github.com/OWNER/REPO/pull/43",
"body": "",
"createdAt": "%[1]s",
"updatedAt": "%[1]s",
"repository": {
"nameWithOwner": "OWNER/REPO"
}
}
]
}
}`,
sampleDateString,
), func(q string, vars map[string]interface{}) {
assert.Equal(t, []interface{}{"PR_kwDNA-jNB9A", "PR_kwDNA-jNB9E"}, vars["ids"])
}),
)
},
wantOut: []*Session{
{
ID: "sess1",
Name: "Build artifacts",
UserID: 1,
AgentID: 2,
Logs: "",
State: "completed",
OwnerID: 10,
RepoID: 1000,
ResourceType: "pull",
ResourceID: 2000,
CreatedAt: sampleDate,
PullRequest: &api.PullRequest{
ID: "PR_node",
FullDatabaseID: "2000",
Number: 42,
Title: "Improve docs",
State: "OPEN",
URL: "https://github.com/OWNER/REPO/pull/42",
Body: "",
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
Repository: &api.PRRepository{
NameWithOwner: "OWNER/REPO",
},
},
},
{
ID: "sess2",
Name: "Build artifacts",
UserID: 1,
AgentID: 2,
Logs: "",
State: "completed",
OwnerID: 10,
RepoID: 1000,
ResourceType: "pull",
ResourceID: 2001,
CreatedAt: sampleDate,
PullRequest: &api.PullRequest{
ID: "PR_node",
FullDatabaseID: "2001",
Number: 43,
Title: "Improve docs",
State: "OPEN",
URL: "https://github.com/OWNER/REPO/pull/43",
Body: "",
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
Repository: &api.PRRepository{
NameWithOwner: "OWNER/REPO",
},
},
},
},
},
{
name: "API error",
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"},
}),
"api.githubcopilot.com",
),
httpmock.StatusStringResponse(500, "{}"),
)
},
wantErr: "failed to list sessions:",
}, {
name: "API error at hydration",
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"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(heredoc.Docf(`
{
"sessions": [
{
"id": "sess1",
"name": "Build artifacts",
"user_id": 1,
"agent_id": 2,
"logs": "",
"state": "completed",
"owner_id": 10,
"repo_id": 1000,
"resource_type": "pull",
"resource_id": 2000,
"created_at": "%[1]s"
}
]
}`,
sampleDateString,
)),
)
// GraphQL hydration
reg.Register(
httpmock.GraphQL(`query FetchPRsForAgentTaskSessions\b`),
httpmock.StatusStringResponse(500, `{}`),
)
},
wantErr: `failed to fetch session resources: non-200 OK status code:`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reg := &httpmock.Registry{}
if tt.httpStubs != nil {
tt.httpStubs(t, reg)
}
defer reg.Verify(t)
httpClient := &http.Client{Transport: reg}
cfg := config.NewBlankConfig()
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
if tt.perPage != 0 {
last := defaultSessionsPerPage
defaultSessionsPerPage = tt.perPage
defer func() {
defaultSessionsPerPage = last
}()
}
sessions, err := capiClient.ListSessionsForViewer(context.Background(), tt.limit)
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
require.Nil(t, sessions)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantOut, sessions)
})
}
}
func TestListSessionForRepoRequiresRepo(t *testing.T) {
client := &CAPIClient{}
_, err := client.ListSessionsForRepo(context.Background(), "", "only-repo", 0)
assert.EqualError(t, err, "owner and repo are required")
_, err = client.ListSessionsForRepo(context.Background(), "only-owner", "", 0)
assert.EqualError(t, err, "owner and repo are required")
_, err = client.ListSessionsForRepo(context.Background(), "", "", 0)
assert.EqualError(t, err, "owner and repo are required")
}
func TestListSessionsForRepo(t *testing.T) {
sampleDateString := "2025-08-29T00:00:00Z"
sampleDate, err := time.Parse(time.RFC3339, sampleDateString)
require.NoError(t, err)
tests := []struct {
name string
perPage int
limit int
httpStubs func(*testing.T, *httpmock.Registry)
wantErr string
wantOut []*Session
}{
{
name: "zero limit",
limit: 0,
wantOut: nil,
},
{
name: "no sessions",
limit: 10,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(
httpmock.QueryMatcher("GET", "agents/sessions/nwo/OWNER/REPO", url.Values{
"page_number": {"1"},
"page_size": {"50"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(`{"sessions":[]}`),
)
},
wantOut: nil,
},
{
name: "single session",
limit: 10,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(
httpmock.QueryMatcher("GET", "agents/sessions/nwo/OWNER/REPO", url.Values{
"page_number": {"1"},
"page_size": {"50"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(heredoc.Docf(`
{
"sessions": [
{
"id": "sess1",
"name": "Build artifacts",
"user_id": 1,
"agent_id": 2,
"logs": "",
"state": "completed",
"owner_id": 10,
"repo_id": 1000,
"resource_type": "pull",
"resource_id": 2000,
"created_at": "%[1]s"
}
]
}`,
sampleDateString,
)),
)
// GraphQL hydration
reg.Register(
httpmock.GraphQL(`query FetchPRsForAgentTaskSessions\b`),
httpmock.GraphQLQuery(heredoc.Docf(`
{
"data": {
"nodes": [
{
"__typename": "PullRequest",
"id": "PR_node",
"fullDatabaseId": "2000",
"number": 42,
"title": "Improve docs",
"state": "OPEN",
"url": "https://github.com/OWNER/REPO/pull/42",
"body": "",
"createdAt": "%[1]s",
"updatedAt": "%[1]s",
"repository": {
"nameWithOwner": "OWNER/REPO"
}
}
]
}
}`,
sampleDateString,
), func(q string, vars map[string]interface{}) {
assert.Equal(t, []interface{}{"PR_kwDNA-jNB9A"}, vars["ids"])
}),
)
},
wantOut: []*Session{
{
ID: "sess1",
Name: "Build artifacts",
UserID: 1,
AgentID: 2,
Logs: "",
State: "completed",
OwnerID: 10,
RepoID: 1000,
ResourceType: "pull",
ResourceID: 2000,
CreatedAt: sampleDate,
PullRequest: &api.PullRequest{
ID: "PR_node",
FullDatabaseID: "2000",
Number: 42,
Title: "Improve docs",
State: "OPEN",
URL: "https://github.com/OWNER/REPO/pull/42",
Body: "",
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
Repository: &api.PRRepository{
NameWithOwner: "OWNER/REPO",
},
},
},
},
},
{
name: "multiple sessions, paginated",
perPage: 1, // to enforce pagination
limit: 2,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(
httpmock.QueryMatcher("GET", "agents/sessions/nwo/OWNER/REPO", url.Values{
"page_number": {"1"},
"page_size": {"1"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(heredoc.Docf(`
{
"sessions": [
{
"id": "sess1",
"name": "Build artifacts",
"user_id": 1,
"agent_id": 2,
"logs": "",
"state": "completed",
"owner_id": 10,
"repo_id": 1000,
"resource_type": "pull",
"resource_id": 2000,
"created_at": "%[1]s"
}
]
}`,
sampleDateString,
)),
)
// Second page
reg.Register(
httpmock.WithHost(
httpmock.QueryMatcher("GET", "agents/sessions/nwo/OWNER/REPO", url.Values{
"page_number": {"2"},
"page_size": {"1"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(heredoc.Docf(`
{
"sessions": [
{
"id": "sess2",
"name": "Build artifacts",
"user_id": 1,
"agent_id": 2,
"logs": "",
"state": "completed",
"owner_id": 10,
"repo_id": 1000,
"resource_type": "pull",
"resource_id": 2001,
"created_at": "%[1]s"
}
]
}`,
sampleDateString,
)),
)
// GraphQL hydration
reg.Register(
httpmock.GraphQL(`query FetchPRsForAgentTaskSessions\b`),
httpmock.GraphQLQuery(heredoc.Docf(`
{
"data": {
"nodes": [
{
"__typename": "PullRequest",
"id": "PR_node",
"fullDatabaseId": "2000",
"number": 42,
"title": "Improve docs",
"state": "OPEN",
"url": "https://github.com/OWNER/REPO/pull/42",
"body": "",
"createdAt": "%[1]s",
"updatedAt": "%[1]s",
"repository": {
"nameWithOwner": "OWNER/REPO"
}
},
{
"__typename": "PullRequest",
"id": "PR_node",
"fullDatabaseId": "2001",
"number": 43,
"title": "Improve docs",
"state": "OPEN",
"url": "https://github.com/OWNER/REPO/pull/43",
"body": "",
"createdAt": "%[1]s",
"updatedAt": "%[1]s",
"repository": {
"nameWithOwner": "OWNER/REPO"
}
}
]
}
}`,
sampleDateString,
), func(q string, vars map[string]interface{}) {
assert.Equal(t, []interface{}{"PR_kwDNA-jNB9A", "PR_kwDNA-jNB9E"}, vars["ids"])
}),
)
},
wantOut: []*Session{
{
ID: "sess1",
Name: "Build artifacts",
UserID: 1,
AgentID: 2,
Logs: "",
State: "completed",
OwnerID: 10,
RepoID: 1000,
ResourceType: "pull",
ResourceID: 2000,
CreatedAt: sampleDate,
PullRequest: &api.PullRequest{
ID: "PR_node",
FullDatabaseID: "2000",
Number: 42,
Title: "Improve docs",
State: "OPEN",
URL: "https://github.com/OWNER/REPO/pull/42",
Body: "",
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
Repository: &api.PRRepository{
NameWithOwner: "OWNER/REPO",
},
},
},
{
ID: "sess2",
Name: "Build artifacts",
UserID: 1,
AgentID: 2,
Logs: "",
State: "completed",
OwnerID: 10,
RepoID: 1000,
ResourceType: "pull",
ResourceID: 2001,
CreatedAt: sampleDate,
PullRequest: &api.PullRequest{
ID: "PR_node",
FullDatabaseID: "2001",
Number: 43,
Title: "Improve docs",
State: "OPEN",
URL: "https://github.com/OWNER/REPO/pull/43",
Body: "",
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
Repository: &api.PRRepository{
NameWithOwner: "OWNER/REPO",
},
},
},
},
},
{
name: "API error",
limit: 10,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(
httpmock.QueryMatcher("GET", "agents/sessions/nwo/OWNER/REPO", url.Values{
"page_number": {"1"},
"page_size": {"50"},
}),
"api.githubcopilot.com",
),
httpmock.StatusStringResponse(500, "{}"),
)
},
wantErr: "failed to list sessions:",
}, {
name: "API error at hydration",
limit: 10,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(
httpmock.QueryMatcher("GET", "agents/sessions/nwo/OWNER/REPO", url.Values{
"page_number": {"1"},
"page_size": {"50"},
}),
"api.githubcopilot.com",
),
httpmock.StringResponse(heredoc.Docf(`
{
"sessions": [
{
"id": "sess1",
"name": "Build artifacts",
"user_id": 1,
"agent_id": 2,
"logs": "",
"state": "completed",
"owner_id": 10,
"repo_id": 1000,
"resource_type": "pull",
"resource_id": 2000,
"created_at": "%[1]s"
}
]
}`,
sampleDateString,
)),
)
// GraphQL hydration
reg.Register(
httpmock.GraphQL(`query FetchPRsForAgentTaskSessions\b`),
httpmock.StatusStringResponse(500, `{}`),
)
},
wantErr: `failed to fetch session resources: non-200 OK status code:`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reg := &httpmock.Registry{}
if tt.httpStubs != nil {
tt.httpStubs(t, reg)
}
defer reg.Verify(t)
httpClient := &http.Client{Transport: reg}
cfg := config.NewBlankConfig()
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
if tt.perPage != 0 {
last := defaultSessionsPerPage
defaultSessionsPerPage = tt.perPage
defer func() {
defaultSessionsPerPage = last
}()
}
sessions, err := capiClient.ListSessionsForRepo(context.Background(), "OWNER", "REPO", tt.limit)
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
require.Nil(t, sessions)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantOut, sessions)
})
}
}

View file

@ -14,6 +14,7 @@ import (
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/agent-task/capi"
"github.com/cli/cli/v2/pkg/cmd/agent-task/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
@ -32,7 +33,8 @@ type CreateOptions struct {
func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command {
opts := &CreateOptions{
IO: f.IOStreams,
IO: f.IOStreams,
CapiClient: shared.CapiClientFunc(f),
}
var fromFileName string
@ -42,6 +44,9 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
Short: "Create an agent task (preview)",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Support -R/--repo override
opts.BaseRepo = f.BaseRepo
if err := cmdutil.MutuallyExclusive("only one of -F or arg can be provided", len(args) > 0, fromFileName != ""); err != nil {
return err
}
@ -60,13 +65,11 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
}
opts.ProblemStatement = trimmed
}
if opts.ProblemStatement == "" {
return cmdutil.FlagErrorf("a task description is required")
}
// Support -R/--repo override
if f != nil {
opts.BaseRepo = f.BaseRepo
}
if runF != nil {
return runF(opts)
}
@ -86,36 +89,16 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
$ gh agent-task create "fix errors" --base branch
`),
}
if f != nil {
cmdutil.EnableRepoOverride(cmd, f)
}
cmdutil.EnableRepoOverride(cmd, f)
cmd.Flags().StringVarP(&fromFileName, "from-file", "F", "", "Read task description from `file` (use \"-\" to read from standard input)")
cmd.Flags().StringVarP(&opts.BaseBranch, "base", "b", "", "Base branch for the pull request (use default branch if not provided)")
opts.CapiClient = func() (capi.CapiClient, error) {
cfg, err := f.Config()
if err != nil {
return nil, err
}
httpClient, err := f.HttpClient()
if err != nil {
return nil, err
}
authCfg := cfg.Authentication()
return capi.NewCAPIClient(httpClient, authCfg), nil
}
return cmd
}
func createRun(opts *CreateOptions) error {
if opts.ProblemStatement == "" {
return cmdutil.FlagErrorf("a task description is required")
}
if opts.BaseRepo == nil {
return errors.New("failed to resolve repository")
}
repo, err := opts.BaseRepo()
if err != nil || repo == nil {
// Not printing the error that came back from BaseRepo() here because we want

View file

@ -1,117 +1,123 @@
package create
import (
"net/http"
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"testing"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cenkalti/backoff/v4"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/agent-task/capi"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/google/shlex"
"github.com/stretchr/testify/require"
)
// Test basic option parsing & repository requirement
func TestNewCmdCreate_Args(t *testing.T) {
func TestNewCmdCreate(t *testing.T) {
tmpDir := t.TempDir()
tmpEmptyFile := filepath.Join(tmpDir, "empty-task-description.md")
err := os.WriteFile(tmpEmptyFile, []byte(" \n\n"), 0600)
require.NoError(t, err)
tmpFile := filepath.Join(tmpDir, "task-description.md")
err = os.WriteFile(tmpFile, []byte("task description from file"), 0600)
require.NoError(t, err)
tests := []struct {
name string
args []string
fileContent string // if non-empty, create temp file and substitute {{FILE}} token in args
wantOpts *CreateOptions // nil when expecting error
expectedErr string
name string
args string
stdin string
wantOpts *CreateOptions // nil when expecting error
wantErr string
}{
{
name: "no args nor file",
args: []string{},
expectedErr: "a task description is required",
name: "no args nor file",
wantErr: "a task description is required",
},
{
name: "arg only success",
args: []string{"task description from args"},
args: "'task description from args'",
wantOpts: &CreateOptions{
ProblemStatement: "task description from args",
},
},
{
name: "from-file success",
args: []string{"-F", "{{FILE}}"},
fileContent: "task description from file",
name: "from-file success",
args: fmt.Sprintf("-F '%s'", tmpFile),
wantOpts: &CreateOptions{
ProblemStatement: "task description from file",
},
},
{
name: "file content from stdin success",
args: []string{"-F", "-"},
fileContent: "task from stdin",
wantOpts: &CreateOptions{ProblemStatement: "task from stdin"},
name: "file content from stdin success",
args: "-F -",
stdin: "task description from stdin",
wantOpts: &CreateOptions{
ProblemStatement: "task description from stdin",
},
},
{
name: "mutually exclusive arg and file",
args: []string{"Some task inline", "-F", "{{FILE}}"},
fileContent: "Some task",
expectedErr: "only one of -F or arg can be provided",
name: "mutually exclusive arg and file",
args: "'some task inline' -F foo.md",
wantErr: "only one of -F or arg can be provided",
},
{
name: "missing file path",
args: []string{"-F", "does-not-exist.md"},
expectedErr: "could not read task description file: open does-not-exist.md: no such file or directory",
name: "missing file path",
args: "-F does-not-exist.md",
wantErr: "could not read task description file: open does-not-exist.md:",
},
{
name: "empty file",
args: []string{"-F", "{{FILE}}"},
fileContent: " \n\n",
expectedErr: "task description file is empty",
name: "empty file",
args: fmt.Sprintf("-F '%s'", tmpEmptyFile),
wantErr: "task description file is empty",
},
{
name: "empty from stdin",
args: "-F -",
stdin: " \n\n",
wantErr: "task description file is empty",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ios, stdinBuf, _, _ := iostreams.Test()
// Provide file content either via stdin ( -F - ) or by creating a temp file
if tt.fileContent != "" {
isStdin := len(tt.args) == 2 && tt.args[0] == "-F" && tt.args[1] == "-"
hasFileToken := slices.Contains(tt.args, "{{FILE}}")
switch {
case isStdin:
stdinBuf.WriteString(tt.fileContent)
case hasFileToken:
dir := t.TempDir()
path := filepath.Join(dir, "task.md")
if err := os.WriteFile(path, []byte(tt.fileContent), 0o600); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
for i, a := range tt.args {
if a == "{{FILE}}" {
tt.args[i] = path
}
}
}
ios, stdin, _, _ := iostreams.Test()
f := &cmdutil.Factory{
IOStreams: ios,
}
f := &cmdutil.Factory{IOStreams: ios}
var gotOpts *CreateOptions
cmd := NewCmdCreate(f, func(o *CreateOptions) error {
gotOpts = o
return nil
})
cmd.SetArgs(tt.args)
_, err := cmd.ExecuteC()
if tt.expectedErr != "" {
require.Error(t, err)
require.Equal(t, tt.expectedErr, err.Error())
argv, err := shlex.Split(tt.args)
require.NoError(t, err)
cmd.SetArgs(argv)
cmd.SetIn(stdin)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
if tt.stdin != "" {
stdin.WriteString(tt.stdin)
}
_, err = cmd.ExecuteC()
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
return
}
require.NoError(t, err)
if tt.wantOpts != nil {
require.Equal(t, tt.wantOpts.ProblemStatement, gotOpts.ProblemStatement)
@ -121,177 +127,182 @@ func TestNewCmdCreate_Args(t *testing.T) {
}
func Test_createRun(t *testing.T) {
createdJobSuccessResponse := heredoc.Doc(`{
"job_id":"job123",
"session_id":"sess1",
"actor":{"id":1,"login":"octocat"},
"created_at":"2025-08-29T00:00:00Z",
"updated_at":"2025-08-29T00:00:00Z"
}`)
createdJobSuccessWithPRResponse := heredoc.Doc(`{
"job_id":"job123",
"session_id":"sess1",
"actor":{"id":1,"login":"octocat"},
"created_at":"2025-08-29T00:00:00Z",
"updated_at":"2025-08-29T00:00:00Z",
"pull_request":{"id":101,"number":42}
}`)
createdJobTimeoutResponse := heredoc.Doc(`{
"job_id":"jobABC",
"session_id":"sess1",
"actor":{"id":1,"login":"octocat"},
"created_at":"2025-08-29T00:00:00Z",
"updated_at":"2025-08-29T00:00:00Z"
}`)
sampleDateString := "2025-08-29T00:00:00Z"
sampleDate, err := time.Parse(time.RFC3339, sampleDateString)
require.NoError(t, err)
createdJobSuccess := capi.Job{
ID: "job123",
SessionID: "sess1",
Actor: &capi.JobActor{
ID: 1,
Login: "octocat",
},
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
}
createdJobSuccessWithPR := capi.Job{
ID: "job123",
SessionID: "sess1",
Actor: &capi.JobActor{
ID: 1,
Login: "octocat",
},
CreatedAt: sampleDate,
UpdatedAt: sampleDate,
PullRequest: &capi.JobPullRequest{
ID: 101,
Number: 42,
},
}
tests := []struct {
name string
stubs func(*httpmock.Registry)
baseRepoFunc func() (ghrepo.Interface, error)
problemStatement string
baseBranch string
wantStdout string
wantStdErr string
wantErr string
name string
capiStubs func(*testing.T, *capi.CapiClientMock)
baseRepoFunc func() (ghrepo.Interface, error)
baseBranch string
wantStdout string
wantStdErr string
wantErr string
}{
{
name: "base branch included in create payload",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
problemStatement: "Do the thing",
baseBranch: "feature",
stubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.RESTPayload(201, createdJobSuccessWithPRResponse, func(payload map[string]interface{}) {
prRaw, ok := payload["pull_request"].(map[string]interface{})
if !ok {
require.FailNow(t, "expected pull_request object in payload")
}
if prRaw["base_ref"] != "refs/heads/feature" {
require.FailNow(t, "expected pull_request.base_ref to be 'refs/heads/feature'")
}
if payload["problem_statement"] != "Do the thing" {
require.FailNow(t, "unexpected problem_statement value")
}
}),
)
},
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
name: "missing repo returns error",
baseRepoFunc: func() (ghrepo.Interface, error) { return nil, nil },
wantErr: "a repository is required; re-run in a repository or supply one with --repo owner/name",
},
{
name: "get job API failure surfaces error",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
problemStatement: "Do the thing",
stubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(201, createdJobTimeoutResponse),
)
reg.Register(
httpmock.WithHost(httpmock.REST("GET", "agents/swe/v1/jobs/OWNER/REPO/jobABC"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(500, `{"error":{"message":"internal server error"}}`),
)
},
wantStdErr: "failed to get job: 500 Internal Server Error\n",
wantStdout: "job jobABC queued. View progress: https://github.com/copilot/agents\n",
},
{
name: "success with immediate PR",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
problemStatement: "Do the thing",
stubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(201, createdJobSuccessWithPRResponse),
)
},
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
},
{
name: "success with delayed PR after polling",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
problemStatement: "Do the thing",
stubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(201, createdJobSuccessResponse),
)
reg.Register(
httpmock.WithHost(httpmock.REST("GET", "agents/swe/v1/jobs/OWNER/REPO/job123"), "api.githubcopilot.com"),
httpmock.StringResponse(`{"job_id":"job123","pull_request":{"id":101,"number":42}}`),
)
},
wantStdout: "https://github.com/OWNER/REPO/pull/42\n",
},
{
name: "fallback after timeout returns link to global agents page",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
problemStatement: "Do the thing",
stubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(201, createdJobTimeoutResponse),
)
// 4 attempts: initial + 3 retries
for range 4 {
reg.Register(
httpmock.WithHost(httpmock.REST("GET", "agents/swe/v1/jobs/OWNER/REPO/jobABC"), "api.githubcopilot.com"),
httpmock.StringResponse(`{"job_id":"jobABC"}`),
)
name: "base branch included in create payload",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
baseBranch: "feature",
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
require.Equal(t, "OWNER", owner)
require.Equal(t, "REPO", repo)
require.Equal(t, "Do the thing", problemStatement)
require.Equal(t, "feature", baseBranch)
return &createdJobSuccess, nil
}
m.GetJobFunc = func(ctx context.Context, owner, repo, jobID string) (*capi.Job, error) {
require.Equal(t, "OWNER", owner)
require.Equal(t, "REPO", repo)
require.Equal(t, "job123", jobID)
return &createdJobSuccessWithPR, nil
}
},
wantStdout: "job jobABC queued. View progress: https://github.com/copilot/agents\n",
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
},
{
name: "missing repo returns error",
problemStatement: "task",
baseRepoFunc: func() (ghrepo.Interface, error) { return nil, nil },
wantErr: "a repository is required; re-run in a repository or supply one with --repo owner/name",
},
{
name: "create task API failure returns error",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
problemStatement: "do the thing",
stubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(500, `{"error":{"message":"some API error"}}`),
)
name: "create task API failure returns error",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
require.Equal(t, "OWNER", owner)
require.Equal(t, "REPO", repo)
require.Equal(t, "Do the thing", problemStatement)
require.Equal(t, "", baseBranch)
return nil, errors.New("some error")
}
},
wantErr: "failed to create job: some API error",
wantErr: "some error",
},
{
name: "missing task description returns error",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
problemStatement: "",
wantErr: "a task description is required",
name: "get job API failure surfaces error",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
require.Equal(t, "OWNER", owner)
require.Equal(t, "REPO", repo)
require.Equal(t, "Do the thing", problemStatement)
require.Equal(t, "", baseBranch)
return &createdJobSuccess, nil
}
m.GetJobFunc = func(ctx context.Context, owner, repo, jobID string) (*capi.Job, error) {
return nil, errors.New("some error")
}
},
wantStdErr: "some error\n",
wantStdout: "job job123 queued. View progress: https://github.com/copilot/agents\n",
},
{
name: "success with immediate PR",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
require.Equal(t, "OWNER", owner)
require.Equal(t, "REPO", repo)
require.Equal(t, "Do the thing", problemStatement)
require.Equal(t, "", baseBranch)
return &createdJobSuccessWithPR, nil
}
},
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
},
{
name: "success with delayed PR after polling",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
require.Equal(t, "OWNER", owner)
require.Equal(t, "REPO", repo)
require.Equal(t, "Do the thing", problemStatement)
require.Equal(t, "", baseBranch)
return &createdJobSuccess, nil
}
m.GetJobFunc = func(ctx context.Context, owner, repo, jobID string) (*capi.Job, error) {
require.Equal(t, "OWNER", owner)
require.Equal(t, "REPO", repo)
require.Equal(t, "job123", jobID)
return &createdJobSuccessWithPR, nil
}
},
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
},
{
name: "fallback after timeout returns link to global agents page",
baseRepoFunc: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
require.Equal(t, "OWNER", owner)
require.Equal(t, "REPO", repo)
require.Equal(t, "Do the thing", problemStatement)
require.Equal(t, "", baseBranch)
return &createdJobSuccess, nil
}
count := 0
m.GetJobFunc = func(ctx context.Context, owner, repo, jobID string) (*capi.Job, error) {
if count++; count > 4 {
require.FailNow(t, "too many get calls")
}
return &createdJobSuccess, nil
}
},
wantStdout: "job job123 queued. View progress: https://github.com/copilot/agents\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
capiClientMock := &capi.CapiClientMock{}
if tt.capiStubs != nil {
tt.capiStubs(t, capiClientMock)
}
ios, _, stdout, stderr := iostreams.Test()
opts := &CreateOptions{
IO: ios,
ProblemStatement: tt.problemStatement,
ProblemStatement: "Do the thing",
BaseRepo: tt.baseRepoFunc,
BaseBranch: tt.baseBranch,
CapiClient: func() (capi.CapiClient, error) {
return capiClientMock, nil
},
}
// A backoff with no internal between retries to keep tests fast,
// and also a max number of retries so we don't infinitely poll.
opts.BackOff = backoff.WithMaxRetries(&backoff.ZeroBackOff{}, 3)
reg := &httpmock.Registry{}
if tt.stubs != nil {
tt.stubs(reg)
cfg := config.NewBlankConfig()
cfg.Set("github.com", "oauth_token", "OTOKEN")
authCfg := cfg.Authentication()
client := capi.NewCAPIClient(&http.Client{Transport: reg}, authCfg)
opts.CapiClient = func() (capi.CapiClient, error) { return client, nil }
}
err := createRun(opts)
if tt.wantErr != "" {
@ -303,10 +314,6 @@ func Test_createRun(t *testing.T) {
require.Equal(t, tt.wantStdout, stdout.String())
require.Equal(t, tt.wantStdErr, stderr.String())
if tt.stubs != nil {
reg.Verify(t)
}
})
}
}

View file

@ -6,7 +6,6 @@ import (
"time"
"github.com/cli/cli/v2/internal/browser"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/tableprinter"
"github.com/cli/cli/v2/internal/text"
@ -23,9 +22,8 @@ const defaultLimit = 30
// ListOptions are the options for the list command
type ListOptions struct {
IO *iostreams.IOStreams
Config func() (gh.Config, error)
Limit int
CapiClient func() (*capi.CAPIClient, error)
CapiClient func() (capi.CapiClient, error)
BaseRepo func() (ghrepo.Interface, error)
Web bool
Browser browser.Browser
@ -34,10 +32,10 @@ type ListOptions struct {
// NewCmdList creates the list command
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
opts := &ListOptions{
IO: f.IOStreams,
Config: f.Config,
Limit: defaultLimit,
Browser: f.Browser,
IO: f.IOStreams,
CapiClient: shared.CapiClientFunc(f),
Limit: defaultLimit,
Browser: f.Browser,
}
cmd := &cobra.Command{
@ -46,7 +44,6 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
// Support -R/--repo override
opts.BaseRepo = f.BaseRepo
if opts.Limit < 1 {
@ -59,26 +56,11 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
},
}
if f != nil {
cmdutil.EnableRepoOverride(cmd, f)
}
cmdutil.EnableRepoOverride(cmd, f)
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, fmt.Sprintf("Maximum number of agent tasks to fetch (default %d)", defaultLimit))
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open agent tasks in the browser")
opts.CapiClient = func() (*capi.CAPIClient, error) {
cfg, err := opts.Config()
if err != nil {
return nil, err
}
httpClient, err := f.HttpClient()
if err != nil {
return nil, err
}
authCfg := cfg.Authentication()
return capi.NewCAPIClient(httpClient, authCfg), nil
}
return cmd
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,23 @@
package shared
import (
"github.com/cli/cli/v2/pkg/cmd/agent-task/capi"
"github.com/cli/cli/v2/pkg/cmdutil"
)
func CapiClientFunc(f *cmdutil.Factory) func() (capi.CapiClient, error) {
return func() (capi.CapiClient, error) {
cfg, err := f.Config()
if err != nil {
return nil, err
}
httpClient, err := f.HttpClient()
if err != nil {
return nil, err
}
authCfg := cfg.Authentication()
return capi.NewCAPIClient(httpClient, authCfg), nil
}
}