Skip to content

Commit cd32acb

Browse files
committed
review feedback
- rename `RequireAccessToken` to `checkIfCIAccessTokenRequired` - provide more context in error on why it happened
1 parent d7bad5b commit cd32acb

2 files changed

Lines changed: 28 additions & 24 deletions

File tree

cmd/src/main.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var (
8282

8383
errConfigMerge = errors.New("when using a configuration file, zero or all environment variables must be set")
8484
errConfigAuthorizationConflict = errors.New("when passing an 'Authorization' additional headers, SRC_ACCESS_TOKEN must never be set")
85-
errCIAccessTokenRequired = errors.New("SRC_ACCESS_TOKEN must be set in CI")
85+
errCIAccessTokenRequired = errors.New("CI is true and SRC_ACCESS_TOKEN is not set or empty. When running in CI OAuth tokens cannot be used, only SRC_ACCESS_TOKEN. Either set CI=false or define a SRC_ACCESS_TOKEN")
8686
)
8787

8888
// commands contains all registered subcommands.
@@ -168,6 +168,9 @@ func (c *config) InCI() bool {
168168
}
169169

170170
func (c *config) requireCIAccessToken() error {
171+
// In CI we typically do not have access to the keyring and the machine is also typically headless
172+
// we therefore require SRC_ACCESS_TOKEN to be set when in CI.
173+
// If someone really wants to run with OAuth in CI they can temporarily do CI=false
171174
if c.InCI() && c.AuthMode() != AuthModeAccessToken {
172175
return errCIAccessTokenRequired
173176
}
@@ -178,14 +181,14 @@ func (c *config) requireCIAccessToken() error {
178181
// apiClient returns an api.Client built from the configuration.
179182
func (c *config) apiClient(flags *api.Flags, out io.Writer) api.Client {
180183
opts := api.ClientOpts{
181-
EndpointURL: c.endpointURL,
182-
AccessToken: c.accessToken,
183-
AdditionalHeaders: c.additionalHeaders,
184-
Flags: flags,
185-
Out: out,
186-
ProxyURL: c.proxyURL,
187-
ProxyPath: c.proxyPath,
188-
RequireAccessToken: c.InCI(),
184+
EndpointURL: c.endpointURL,
185+
AccessToken: c.accessToken,
186+
AdditionalHeaders: c.additionalHeaders,
187+
Flags: flags,
188+
Out: out,
189+
ProxyURL: c.proxyURL,
190+
ProxyPath: c.proxyPath,
191+
RequireAccessTokenInCI: c.InCI(),
189192
}
190193

191194
// Only use OAuth if we do not have SRC_ACCESS_TOKEN set

internal/api/api.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"crypto/tls"
88
"encoding/json"
9+
"errors"
910
"fmt"
1011
"io"
1112
"net/http"
@@ -71,10 +72,10 @@ type request struct {
7172

7273
// ClientOpts encapsulates the options given to NewClient.
7374
type ClientOpts struct {
74-
EndpointURL *url.URL
75-
AccessToken string
76-
AdditionalHeaders map[string]string
77-
RequireAccessToken bool
75+
EndpointURL *url.URL
76+
AccessToken string
77+
AdditionalHeaders map[string]string
78+
RequireAccessTokenInCI bool
7879

7980
// Flags are the standard API client flags provided by NewFlags. If nil,
8081
// default values will be used.
@@ -139,20 +140,20 @@ func NewClient(opts ClientOpts) Client {
139140

140141
return &client{
141142
opts: ClientOpts{
142-
EndpointURL: opts.EndpointURL,
143-
AccessToken: opts.AccessToken,
144-
AdditionalHeaders: opts.AdditionalHeaders,
145-
RequireAccessToken: opts.RequireAccessToken,
146-
Flags: flags,
147-
Out: opts.Out,
143+
EndpointURL: opts.EndpointURL,
144+
AccessToken: opts.AccessToken,
145+
AdditionalHeaders: opts.AdditionalHeaders,
146+
RequireAccessTokenInCI: opts.RequireAccessTokenInCI,
147+
Flags: flags,
148+
Out: opts.Out,
148149
},
149150
httpClient: httpClient,
150151
}
151152
}
152153

153-
func (c *client) requireAccessToken() error {
154-
if c.opts.RequireAccessToken && c.opts.AccessToken == "" {
155-
return fmt.Errorf("SRC_ACCESS_TOKEN must be set in CI")
154+
func (c *client) checkIfCIAccessTokenRequired() error {
155+
if c.opts.RequireAccessTokenInCI && c.opts.AccessToken == "" {
156+
return errors.New("SRC_ACCESS_TOKEN must be set when CI=true")
156157
}
157158

158159
return nil
@@ -183,7 +184,7 @@ func (c *client) NewHTTPRequest(ctx context.Context, method, p string, body io.R
183184
}
184185

185186
func (c *client) createHTTPRequest(ctx context.Context, method, p string, body io.Reader) (*http.Request, error) {
186-
if err := c.requireAccessToken(); err != nil {
187+
if err := c.checkIfCIAccessTokenRequired(); err != nil {
187188
return nil, err
188189
}
189190

@@ -216,7 +217,7 @@ func (c *client) createHTTPRequest(ctx context.Context, method, p string, body i
216217
}
217218

218219
func (r *request) do(ctx context.Context, result any) (bool, error) {
219-
if err := r.client.requireAccessToken(); err != nil {
220+
if err := r.client.checkIfCIAccessTokenRequired(); err != nil {
220221
return false, err
221222
}
222223

0 commit comments

Comments
 (0)