Skip to content

Commit b24f883

Browse files
codeintel: Provide hint on auth error (#1079)
1 parent e8e79e0 commit b24f883

File tree

4 files changed

+108
-157
lines changed

4 files changed

+108
-157
lines changed

cmd/src/code_intel_upload.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515

1616
"github.com/pkg/browser"
1717

18+
"github.com/sourcegraph/sourcegraph/lib/accesstoken"
1819
"github.com/sourcegraph/sourcegraph/lib/codeintel/upload"
20+
"github.com/sourcegraph/sourcegraph/lib/errors"
1921
"github.com/sourcegraph/sourcegraph/lib/output"
2022

2123
"github.com/sourcegraph/src-cli/internal/api"
@@ -86,17 +88,18 @@ func handleCodeIntelUpload(args []string) error {
8688
}
8789
}
8890
if err != nil {
89-
return handleUploadError(nil, err)
91+
return handleUploadError(cfg.AccessToken, err)
9092
}
9193

9294
client := api.NewClient(api.ClientOpts{
9395
Out: io.Discard,
9496
Flags: codeintelUploadFlags.apiFlags,
9597
})
9698

97-
uploadID, err := upload.UploadIndex(ctx, codeintelUploadFlags.file, client, codeintelUploadOptions(out, isSCIPAvailable))
99+
uploadOptions := codeintelUploadOptions(out, isSCIPAvailable)
100+
uploadID, err := upload.UploadIndex(ctx, codeintelUploadFlags.file, client, uploadOptions)
98101
if err != nil {
99-
return handleUploadError(out, err)
102+
return handleUploadError(uploadOptions.SourcegraphInstanceOptions.AccessToken, err)
100103
}
101104

102105
uploadURL, err := makeCodeIntelUploadURL(uploadID)
@@ -233,9 +236,9 @@ func (e errorWithHint) Error() string {
233236
// given output object is nil then the error will be written to standard out.
234237
//
235238
// This method returns the error that should be passed back up to the runner.
236-
func handleUploadError(out *output.Output, err error) error {
237-
if err == upload.ErrUnauthorized {
238-
err = filterLSIFUnauthorizedError(out, err)
239+
func handleUploadError(accessToken string, err error) error {
240+
if errors.Is(err, upload.ErrUnauthorized) {
241+
err = attachHintsForAuthorizationError(accessToken, err)
239242
}
240243

241244
if codeintelUploadFlags.ignoreUploadFailures {
@@ -247,8 +250,25 @@ func handleUploadError(out *output.Output, err error) error {
247250
return err
248251
}
249252

250-
func filterLSIFUnauthorizedError(out *output.Output, err error) error {
253+
func attachHintsForAuthorizationError(accessToken string, err error) error {
251254
var actionableHints []string
255+
256+
likelyTokenError := accessToken == ""
257+
if _, err = accesstoken.ParsePersonalAccessToken(accessToken); accessToken != "" && err != nil {
258+
likelyTokenError = true
259+
actionableHints = append(actionableHints,
260+
"However, the provided access token does not match expected format; was it truncated?",
261+
"Typically the access token looks like sgp_<40 hex chars> or sgp_<instance-id>_<40 hex chars>.")
262+
}
263+
264+
if likelyTokenError {
265+
return errorWithHint{err: err, hint: strings.Join(mergeStringSlices(
266+
[]string{"A Sourcegraph access token must be provided via SRC_ACCESS_TOKEN for uploading SCIP/LSIF data."},
267+
actionableHints,
268+
[]string{"For more details, see https://sourcegraph.com/docs/cli/how-tos/creating_an_access_token."},
269+
), "\n")}
270+
}
271+
252272
needsGitHubToken := strings.HasPrefix(codeintelUploadFlags.repo, "github.com")
253273
needsGitLabToken := strings.HasPrefix(codeintelUploadFlags.repo, "gitlab.com")
254274

go.mod

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module github.com/sourcegraph/src-cli
22

33
go 1.22
44

5+
toolchain go1.22.2
6+
57
require (
68
cloud.google.com/go/storage v1.30.1
79
github.com/atotto/clipboard v0.1.4
@@ -26,14 +28,14 @@ require (
2628
github.com/mattn/go-isatty v0.0.19
2729
github.com/neelance/parallel v0.0.0-20160708114440-4de9ce63d14c
2830
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
29-
github.com/sourcegraph/conc v0.3.0
31+
github.com/sourcegraph/conc v0.3.1-0.20240108182409-4afefce20f9b
3032
github.com/sourcegraph/go-diff v0.6.2-0.20221123165719-f8cd299c40f3
3133
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf
3234
github.com/sourcegraph/scip v0.3.1-0.20230627154934-45df7f6d33fc
33-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20231108085505-01324538d7f0
35+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20240510113214-3a8666b99a37
3436
github.com/stretchr/testify v1.8.4
35-
golang.org/x/net v0.17.0
36-
golang.org/x/sync v0.3.0
37+
golang.org/x/net v0.21.0
38+
golang.org/x/sync v0.6.0
3739
google.golang.org/api v0.132.0
3840
google.golang.org/protobuf v1.31.0
3941
gopkg.in/yaml.v3 v3.0.1
@@ -48,23 +50,23 @@ require (
4850
github.com/google/pprof v0.0.0-20230602150820-91b7bce49751 // indirect
4951
github.com/google/s2a-go v0.1.4 // indirect
5052
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
51-
github.com/jackc/pgconn v1.12.1 // indirect
53+
github.com/jackc/pgconn v1.14.3 // indirect
5254
github.com/jackc/pgio v1.0.0 // indirect
5355
github.com/jackc/pgpassfile v1.0.0 // indirect
54-
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
55-
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
56+
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
57+
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
5658
github.com/onsi/ginkgo/v2 v2.9.7 // indirect
5759
github.com/onsi/gomega v1.27.8 // indirect
5860
go.uber.org/goleak v1.2.1 // indirect
59-
google.golang.org/genproto/googleapis/api v0.0.0-20230717213848-3f92550aa753 // indirect
60-
google.golang.org/genproto/googleapis/rpc v0.0.0-20230717213848-3f92550aa753 // indirect
61+
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b // indirect
62+
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
6163
)
6264

6365
require (
64-
cloud.google.com/go v0.110.4 // indirect
65-
cloud.google.com/go/compute v1.22.0 // indirect
66+
cloud.google.com/go v0.110.9 // indirect
67+
cloud.google.com/go/compute v1.23.2 // indirect
6668
cloud.google.com/go/compute/metadata v0.2.4-0.20230617002413-005d2dfb6b68 // indirect
67-
cloud.google.com/go/iam v1.1.1 // indirect
69+
cloud.google.com/go/iam v1.1.4 // indirect
6870
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
6971
github.com/Masterminds/goutils v1.1.1 // indirect
7072
github.com/Masterminds/semver v1.5.0 // indirect
@@ -94,9 +96,9 @@ require (
9496
github.com/davecgh/go-spew v1.1.1 // indirect
9597
github.com/dlclark/regexp2 v1.8.0 // indirect
9698
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
97-
github.com/envoyproxy/protoc-gen-validate v0.10.1 // indirect
99+
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
98100
github.com/fatih/color v1.15.0 // indirect
99-
github.com/getsentry/sentry-go v0.25.0 // indirect
101+
github.com/getsentry/sentry-go v0.27.0 // indirect
100102
github.com/ghodss/yaml v1.0.0 // indirect
101103
github.com/go-logr/logr v1.2.4 // indirect
102104
github.com/go-openapi/jsonpointer v0.19.6 // indirect
@@ -109,7 +111,7 @@ require (
109111
github.com/golang/protobuf v1.5.3 // indirect
110112
github.com/google/gnostic v0.5.7-v3refs // indirect
111113
github.com/google/gofuzz v1.2.0 // indirect
112-
github.com/google/uuid v1.3.1 // indirect
114+
github.com/google/uuid v1.6.0 // indirect
113115
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
114116
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
115117
github.com/gorilla/css v1.0.0 // indirect
@@ -153,7 +155,7 @@ require (
153155
github.com/pseudomuto/protoc-gen-doc v1.5.1 // indirect
154156
github.com/pseudomuto/protokit v0.2.0 // indirect
155157
github.com/rivo/uniseg v0.4.3 // indirect
156-
github.com/rogpeppe/go-internal v1.11.0 // indirect
158+
github.com/rogpeppe/go-internal v1.12.0 // indirect
157159
github.com/russross/blackfriday/v2 v2.1.0 // indirect
158160
github.com/sourcegraph/log v0.0.0-20231018134238-fbadff7458bb // indirect
159161
github.com/spf13/cobra v1.7.0 // indirect
@@ -169,18 +171,18 @@ require (
169171
go.uber.org/atomic v1.11.0 // indirect
170172
go.uber.org/multierr v1.11.0 // indirect
171173
go.uber.org/zap v1.24.0 // indirect
172-
golang.org/x/crypto v0.17.0 // indirect
173-
golang.org/x/mod v0.11.0 // indirect
174-
golang.org/x/oauth2 v0.10.0 // indirect
175-
golang.org/x/sys v0.15.0 // indirect
176-
golang.org/x/term v0.15.0 // indirect
177-
golang.org/x/text v0.14.0 // indirect
174+
golang.org/x/crypto v0.23.0 // indirect
175+
golang.org/x/mod v0.15.0 // indirect
176+
golang.org/x/oauth2 v0.11.0 // indirect
177+
golang.org/x/sys v0.20.0 // indirect
178+
golang.org/x/term v0.20.0 // indirect
179+
golang.org/x/text v0.15.0 // indirect
178180
golang.org/x/time v0.3.0 // indirect
179-
golang.org/x/tools v0.10.0 // indirect
181+
golang.org/x/tools v0.18.0 // indirect
180182
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
181183
google.golang.org/appengine v1.6.7 // indirect
182-
google.golang.org/genproto v0.0.0-20230717213848-3f92550aa753 // indirect
183-
google.golang.org/grpc v1.56.3 // indirect
184+
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 // indirect
185+
google.golang.org/grpc v1.59.0 // indirect
184186
gopkg.in/inf.v0 v0.9.1 // direct
185187
gopkg.in/yaml.v2 v2.4.0 // indirect
186188
k8s.io/klog/v2 v2.90.1 // indirect

0 commit comments

Comments
 (0)