Skip to content

Commit 8a4154c

Browse files
committed
Refactor cfg out of CAPI Client
1 parent 680a8c4 commit 8a4154c

5 files changed

Lines changed: 14 additions & 27 deletions

File tree

pkg/cmd/agent-task/capi/client.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package capi
33
import (
44
"context"
55
"net/http"
6-
7-
"github.com/cli/cli/v2/internal/gh"
86
)
97

108
//go:generate moq -rm -out client_mock.go . CapiClient
@@ -27,22 +25,19 @@ type CapiClient interface {
2725
// CAPIClient is a client for interacting with the Copilot API
2826
type CAPIClient struct {
2927
httpClient *http.Client
30-
authCfg gh.AuthConfig
28+
host string
3129
}
3230

33-
// NewCAPIClient creates a new CAPI client. Provide a token and an HTTP client which
31+
// NewCAPIClient creates a new CAPI client. Provide a token, host, and an HTTP client which
3432
// will be used as the base transport for CAPI requests.
3533
//
3634
// The provided HTTP client will be mutated for use with CAPI, so it should not
3735
// be reused elsewhere.
38-
func NewCAPIClient(httpClient *http.Client, authCfg gh.AuthConfig) *CAPIClient {
39-
host, _ := authCfg.DefaultHost()
40-
token, _ := authCfg.ActiveToken(host)
41-
36+
func NewCAPIClient(httpClient *http.Client, token string, host string) *CAPIClient {
4237
httpClient.Transport = newCAPITransport(token, httpClient.Transport)
4338
return &CAPIClient{
4439
httpClient: httpClient,
45-
authCfg: authCfg,
40+
host: host,
4641
}
4742
}
4843

pkg/cmd/agent-task/capi/job_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/MakeNowJust/heredoc"
10-
"github.com/cli/cli/v2/internal/config"
1110
"github.com/cli/cli/v2/pkg/httpmock"
1211
"github.com/stretchr/testify/assert"
1312
"github.com/stretchr/testify/require"
@@ -168,8 +167,7 @@ func TestGetJob(t *testing.T) {
168167

169168
httpClient := &http.Client{Transport: reg}
170169

171-
cfg := config.NewBlankConfig()
172-
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
170+
capiClient := NewCAPIClient(httpClient, "", "github.com")
173171

174172
job, err := capiClient.GetJob(context.Background(), "OWNER", "REPO", "job123")
175173

@@ -412,8 +410,7 @@ func TestCreateJob(t *testing.T) {
412410

413411
httpClient := &http.Client{Transport: reg}
414412

415-
cfg := config.NewBlankConfig()
416-
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
413+
capiClient := NewCAPIClient(httpClient, "", "github.com")
417414

418415
job, err := capiClient.CreateJob(context.Background(), "OWNER", "REPO", "Do the thing", tt.baseBranch, tt.customAgent)
419416

pkg/cmd/agent-task/capi/sessions.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ func (c *CAPIClient) hydrateSessionPullRequestsAndUsers(sessions []session) ([]*
366366
ids = append(ids, userNodeIds...)
367367

368368
// TODO handle pagination
369-
host, _ := c.authCfg.DefaultHost()
370-
err := apiClient.Query(host, "FetchPRsAndUsersForAgentTaskSessions", &resp, map[string]any{
369+
err := apiClient.Query(c.host, "FetchPRsAndUsersForAgentTaskSessions", &resp, map[string]any{
371370
"ids": ids,
372371
})
373372

pkg/cmd/agent-task/capi/sessions_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99

1010
"github.com/MakeNowJust/heredoc"
1111
"github.com/cli/cli/v2/api"
12-
13-
"github.com/cli/cli/v2/internal/config"
1412
"github.com/cli/cli/v2/pkg/httpmock"
1513
"github.com/stretchr/testify/assert"
1614
"github.com/stretchr/testify/require"
@@ -1163,8 +1161,7 @@ func TestListLatestSessionsForViewer(t *testing.T) {
11631161

11641162
httpClient := &http.Client{Transport: reg}
11651163

1166-
cfg := config.NewBlankConfig()
1167-
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
1164+
capiClient := NewCAPIClient(httpClient, "", "github.com")
11681165

11691166
if tt.perPage != 0 {
11701167
last := defaultSessionsPerPage
@@ -1543,8 +1540,7 @@ func TestListSessionsByResourceID(t *testing.T) {
15431540

15441541
httpClient := &http.Client{Transport: reg}
15451542

1546-
cfg := config.NewBlankConfig()
1547-
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
1543+
capiClient := NewCAPIClient(httpClient, "", "github.com")
15481544

15491545
if tt.perPage != 0 {
15501546
last := defaultSessionsPerPage
@@ -1823,8 +1819,7 @@ func TestGetSession(t *testing.T) {
18231819

18241820
httpClient := &http.Client{Transport: reg}
18251821

1826-
cfg := config.NewBlankConfig()
1827-
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
1822+
capiClient := NewCAPIClient(httpClient, "", "github.com")
18281823

18291824
session, err := capiClient.GetSession(context.Background(), "some-uuid")
18301825

@@ -1900,8 +1895,7 @@ func TestGetPullRequestDatabaseID(t *testing.T) {
19001895

19011896
httpClient := &http.Client{Transport: reg}
19021897

1903-
cfg := config.NewBlankConfig()
1904-
capiClient := NewCAPIClient(httpClient, cfg.Authentication())
1898+
capiClient := NewCAPIClient(httpClient, "", "github.com")
19051899

19061900
databaseID, url, err := capiClient.GetPullRequestDatabaseID(context.Background(), "github.com", "OWNER", "REPO", 42)
19071901

pkg/cmd/agent-task/shared/capi.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ func CapiClientFunc(f *cmdutil.Factory) func() (capi.CapiClient, error) {
2828
}
2929

3030
authCfg := cfg.Authentication()
31-
return capi.NewCAPIClient(httpClient, authCfg), nil
31+
host, _ := authCfg.DefaultHost()
32+
token, _ := authCfg.ActiveToken(host)
33+
return capi.NewCAPIClient(httpClient, token, host), nil
3234
}
3335
}
3436

0 commit comments

Comments
 (0)