From 8a4154cfe7b7f5ae784ca11774bc0846d5926cbc Mon Sep 17 00:00:00 2001 From: William Martin Date: Fri, 14 Nov 2025 18:56:03 +0100 Subject: [PATCH] Refactor cfg out of CAPI Client --- pkg/cmd/agent-task/capi/client.go | 13 ++++--------- pkg/cmd/agent-task/capi/job_test.go | 7 ++----- pkg/cmd/agent-task/capi/sessions.go | 3 +-- pkg/cmd/agent-task/capi/sessions_test.go | 14 ++++---------- pkg/cmd/agent-task/shared/capi.go | 4 +++- 5 files changed, 14 insertions(+), 27 deletions(-) diff --git a/pkg/cmd/agent-task/capi/client.go b/pkg/cmd/agent-task/capi/client.go index aee09bcd9..38f48e777 100644 --- a/pkg/cmd/agent-task/capi/client.go +++ b/pkg/cmd/agent-task/capi/client.go @@ -3,8 +3,6 @@ package capi import ( "context" "net/http" - - "github.com/cli/cli/v2/internal/gh" ) //go:generate moq -rm -out client_mock.go . CapiClient @@ -27,22 +25,19 @@ type CapiClient interface { // CAPIClient is a client for interacting with the Copilot API type CAPIClient struct { httpClient *http.Client - authCfg gh.AuthConfig + host string } -// NewCAPIClient creates a new CAPI client. Provide a token and an HTTP client which +// NewCAPIClient creates a new CAPI client. Provide a token, host, and an HTTP client which // will be used as the base transport for CAPI requests. // // The provided HTTP client will be mutated for use with CAPI, so it should not // be reused elsewhere. -func NewCAPIClient(httpClient *http.Client, authCfg gh.AuthConfig) *CAPIClient { - host, _ := authCfg.DefaultHost() - token, _ := authCfg.ActiveToken(host) - +func NewCAPIClient(httpClient *http.Client, token string, host string) *CAPIClient { httpClient.Transport = newCAPITransport(token, httpClient.Transport) return &CAPIClient{ httpClient: httpClient, - authCfg: authCfg, + host: host, } } diff --git a/pkg/cmd/agent-task/capi/job_test.go b/pkg/cmd/agent-task/capi/job_test.go index 4e14a28a6..58b9805b7 100644 --- a/pkg/cmd/agent-task/capi/job_test.go +++ b/pkg/cmd/agent-task/capi/job_test.go @@ -7,7 +7,6 @@ import ( "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" @@ -168,8 +167,7 @@ func TestGetJob(t *testing.T) { httpClient := &http.Client{Transport: reg} - cfg := config.NewBlankConfig() - capiClient := NewCAPIClient(httpClient, cfg.Authentication()) + capiClient := NewCAPIClient(httpClient, "", "github.com") job, err := capiClient.GetJob(context.Background(), "OWNER", "REPO", "job123") @@ -412,8 +410,7 @@ func TestCreateJob(t *testing.T) { httpClient := &http.Client{Transport: reg} - cfg := config.NewBlankConfig() - capiClient := NewCAPIClient(httpClient, cfg.Authentication()) + capiClient := NewCAPIClient(httpClient, "", "github.com") job, err := capiClient.CreateJob(context.Background(), "OWNER", "REPO", "Do the thing", tt.baseBranch, tt.customAgent) diff --git a/pkg/cmd/agent-task/capi/sessions.go b/pkg/cmd/agent-task/capi/sessions.go index c147afa9c..8e3969d69 100644 --- a/pkg/cmd/agent-task/capi/sessions.go +++ b/pkg/cmd/agent-task/capi/sessions.go @@ -366,8 +366,7 @@ func (c *CAPIClient) hydrateSessionPullRequestsAndUsers(sessions []session) ([]* ids = append(ids, userNodeIds...) // TODO handle pagination - host, _ := c.authCfg.DefaultHost() - err := apiClient.Query(host, "FetchPRsAndUsersForAgentTaskSessions", &resp, map[string]any{ + err := apiClient.Query(c.host, "FetchPRsAndUsersForAgentTaskSessions", &resp, map[string]any{ "ids": ids, }) diff --git a/pkg/cmd/agent-task/capi/sessions_test.go b/pkg/cmd/agent-task/capi/sessions_test.go index 848fd716a..223f04320 100644 --- a/pkg/cmd/agent-task/capi/sessions_test.go +++ b/pkg/cmd/agent-task/capi/sessions_test.go @@ -9,8 +9,6 @@ import ( "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" @@ -1163,8 +1161,7 @@ func TestListLatestSessionsForViewer(t *testing.T) { httpClient := &http.Client{Transport: reg} - cfg := config.NewBlankConfig() - capiClient := NewCAPIClient(httpClient, cfg.Authentication()) + capiClient := NewCAPIClient(httpClient, "", "github.com") if tt.perPage != 0 { last := defaultSessionsPerPage @@ -1543,8 +1540,7 @@ func TestListSessionsByResourceID(t *testing.T) { httpClient := &http.Client{Transport: reg} - cfg := config.NewBlankConfig() - capiClient := NewCAPIClient(httpClient, cfg.Authentication()) + capiClient := NewCAPIClient(httpClient, "", "github.com") if tt.perPage != 0 { last := defaultSessionsPerPage @@ -1823,8 +1819,7 @@ func TestGetSession(t *testing.T) { httpClient := &http.Client{Transport: reg} - cfg := config.NewBlankConfig() - capiClient := NewCAPIClient(httpClient, cfg.Authentication()) + capiClient := NewCAPIClient(httpClient, "", "github.com") session, err := capiClient.GetSession(context.Background(), "some-uuid") @@ -1900,8 +1895,7 @@ func TestGetPullRequestDatabaseID(t *testing.T) { httpClient := &http.Client{Transport: reg} - cfg := config.NewBlankConfig() - capiClient := NewCAPIClient(httpClient, cfg.Authentication()) + capiClient := NewCAPIClient(httpClient, "", "github.com") databaseID, url, err := capiClient.GetPullRequestDatabaseID(context.Background(), "github.com", "OWNER", "REPO", 42) diff --git a/pkg/cmd/agent-task/shared/capi.go b/pkg/cmd/agent-task/shared/capi.go index c61aa13a7..657945817 100644 --- a/pkg/cmd/agent-task/shared/capi.go +++ b/pkg/cmd/agent-task/shared/capi.go @@ -28,7 +28,9 @@ func CapiClientFunc(f *cmdutil.Factory) func() (capi.CapiClient, error) { } authCfg := cfg.Authentication() - return capi.NewCAPIClient(httpClient, authCfg), nil + host, _ := authCfg.DefaultHost() + token, _ := authCfg.ActiveToken(host) + return capi.NewCAPIClient(httpClient, token, host), nil } }