From 8f62e35f6beb4c29438178c343cf84b2a293dab2 Mon Sep 17 00:00:00 2001 From: Francesco Renzi Date: Tue, 7 Feb 2023 08:47:59 +0100 Subject: [PATCH] Add options to multi client (#2257) --- github/actions/fake/multi_client.go | 4 ++-- github/actions/multi_client.go | 14 ++++++++------ github/actions/multi_client_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/github/actions/fake/multi_client.go b/github/actions/fake/multi_client.go index 95c0c6fc..e6625102 100644 --- a/github/actions/fake/multi_client.go +++ b/github/actions/fake/multi_client.go @@ -34,10 +34,10 @@ func NewMultiClient(opts ...MultiClientOption) actions.MultiClient { return f } -func (f *fakeMultiClient) GetClientFor(ctx context.Context, githubConfigURL string, creds actions.ActionsAuth, namespace string) (actions.ActionsService, error) { +func (f *fakeMultiClient) GetClientFor(ctx context.Context, githubConfigURL string, creds actions.ActionsAuth, namespace string, options ...actions.ClientOption) (actions.ActionsService, error) { return f.defaultClient, f.defaultErr } -func (f *fakeMultiClient) GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData actions.KubernetesSecretData) (actions.ActionsService, error) { +func (f *fakeMultiClient) GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData actions.KubernetesSecretData, options ...actions.ClientOption) (actions.ActionsService, error) { return f.defaultClient, f.defaultErr } diff --git a/github/actions/multi_client.go b/github/actions/multi_client.go index 1e204139..bfef8893 100644 --- a/github/actions/multi_client.go +++ b/github/actions/multi_client.go @@ -11,8 +11,8 @@ import ( ) type MultiClient interface { - GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string) (ActionsService, error) - GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData KubernetesSecretData) (ActionsService, error) + GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string, options ...ClientOption) (ActionsService, error) + GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData KubernetesSecretData, options ...ClientOption) (ActionsService, error) } type multiClient struct { @@ -52,7 +52,7 @@ func NewMultiClient(userAgent string, logger logr.Logger) MultiClient { } } -func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string) (ActionsService, error) { +func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string, options ...ClientOption) (ActionsService, error) { m.logger.Info("retrieve actions client", "githubConfigURL", githubConfigURL, "namespace", namespace) if creds.Token == "" && creds.AppCreds == nil { @@ -66,8 +66,10 @@ func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string, client, err := NewClient( githubConfigURL, &creds, - WithUserAgent(m.userAgent), - WithLogger(m.logger), + append([]ClientOption{ + WithUserAgent(m.userAgent), + WithLogger(m.logger), + }, options...)..., ) if err != nil { return nil, err @@ -98,7 +100,7 @@ func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string, type KubernetesSecretData map[string][]byte -func (m *multiClient) GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData KubernetesSecretData) (ActionsService, error) { +func (m *multiClient) GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData KubernetesSecretData, options ...ClientOption) (ActionsService, error) { if len(secretData) == 0 { return nil, fmt.Errorf("must provide secret data with either PAT or GitHub App Auth") } diff --git a/github/actions/multi_client_test.go b/github/actions/multi_client_test.go index a61686c6..80d54a3f 100644 --- a/github/actions/multi_client_test.go +++ b/github/actions/multi_client_test.go @@ -49,6 +49,32 @@ func TestMultiClientCaching(t *testing.T) { assert.Len(t, multiClient.clients, 2) } +func TestMultiClientOptions(t *testing.T) { + logger := logr.Discard() + ctx := context.Background() + + defaultNamespace := "default" + defaultConfigURL := "https://github.com/org/repo" + defaultCreds := &ActionsAuth{ + Token: "token", + } + + multiClient := NewMultiClient("test-user-agent", logger) + service, err := multiClient.GetClientFor( + ctx, + defaultConfigURL, + *defaultCreds, + defaultNamespace, + WithUserAgent("test-option"), + ) + require.NoError(t, err) + + client := service.(*Client) + req, err := client.NewGitHubAPIRequest(ctx, "GET", "/test", nil) + require.NoError(t, err) + assert.Equal(t, "test-option", req.Header.Get("User-Agent")) +} + func TestCreateJWT(t *testing.T) { key := `-----BEGIN RSA PRIVATE KEY----- MIICWgIBAAKBgHXfRT9cv9UY9fAAD4+1RshpfSSZe277urfEmPfX3/Og9zJYRk//