Skip to content

Commit e6c2532

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

2 files changed

Lines changed: 23 additions & 23 deletions

File tree

cmd/src/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ func (c *config) requireCIAccessToken() error {
178178
// apiClient returns an api.Client built from the configuration.
179179
func (c *config) apiClient(flags *api.Flags, out io.Writer) api.Client {
180180
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(),
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+
RequireAccessTokenInCI: c.InCI(),
189189
}
190190

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

internal/api/api.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ type request struct {
7171

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

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

140140
return &client{
141141
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,
142+
EndpointURL: opts.EndpointURL,
143+
AccessToken: opts.AccessToken,
144+
AdditionalHeaders: opts.AdditionalHeaders,
145+
RequireAccessTokenInCI: opts.RequireAccessTokenInCI,
146+
Flags: flags,
147+
Out: opts.Out,
148148
},
149149
httpClient: httpClient,
150150
}
151151
}
152152

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")
153+
func (c *client) checkIfCIAccessTokenRequired() error {
154+
if c.opts.RequireAccessTokenInCI && c.opts.AccessToken == "" {
155+
return fmt.Errorf("CI is true and no SRC_ACCESS_TOKEN is set. When running in CI OAuth tokens cannot be used, only SRC_ACCESS_TOKEN. Either set CI=false or define a SRC_ACCESS_TOKEN")
156156
}
157157

158158
return nil
@@ -183,7 +183,7 @@ func (c *client) NewHTTPRequest(ctx context.Context, method, p string, body io.R
183183
}
184184

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

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

218218
func (r *request) do(ctx context.Context, result any) (bool, error) {
219-
if err := r.client.requireAccessToken(); err != nil {
219+
if err := r.client.checkIfCIAccessTokenRequired(); err != nil {
220220
return false, err
221221
}
222222

0 commit comments

Comments
 (0)