Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codeintel: Provide hint on auth error #1079

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions cmd/src/code_intel_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (

"github.com/pkg/browser"

"github.com/sourcegraph/sourcegraph/lib/accesstoken"
"github.com/sourcegraph/sourcegraph/lib/codeintel/upload"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/output"

"github.com/sourcegraph/src-cli/internal/api"
Expand Down Expand Up @@ -86,17 +88,18 @@ func handleCodeIntelUpload(args []string) error {
}
}
if err != nil {
return handleUploadError(nil, err)
return handleUploadError(cfg.AccessToken, err)
}

client := api.NewClient(api.ClientOpts{
Out: io.Discard,
Flags: codeintelUploadFlags.apiFlags,
})

uploadID, err := upload.UploadIndex(ctx, codeintelUploadFlags.file, client, codeintelUploadOptions(out, isSCIPAvailable))
uploadOptions := codeintelUploadOptions(out, isSCIPAvailable)
uploadID, err := upload.UploadIndex(ctx, codeintelUploadFlags.file, client, uploadOptions)
if err != nil {
return handleUploadError(out, err)
return handleUploadError(uploadOptions.SourcegraphInstanceOptions.AccessToken, err)
}

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

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

func filterLSIFUnauthorizedError(out *output.Output, err error) error {
func attachHintsForAuthorizationError(accessToken string, err error) error {
var actionableHints []string

likelyTokenError := accessToken == ""
if _, err = accesstoken.ParsePersonalAccessToken(accessToken); accessToken != "" && err != nil {
likelyTokenError = true
actionableHints = append(actionableHints,
"However, the provided access token does not match expected format; was it truncated?",
"Typically the access token looks like sgp_<40 hex chars> or sgp_<instance-id>_<40 hex chars>.")
}

if likelyTokenError {
return errorWithHint{err: err, hint: strings.Join(mergeStringSlices(
[]string{"A Sourcegraph access token must be provided via SRC_ACCESS_TOKEN for uploading SCIP/LSIF data."},
actionableHints,
[]string{"For more details, see https://sourcegraph.com/docs/cli/how-tos/creating_an_access_token."},
), "\n")}
}

needsGitHubToken := strings.HasPrefix(codeintelUploadFlags.repo, "github.com")
needsGitLabToken := strings.HasPrefix(codeintelUploadFlags.repo, "gitlab.com")

Expand Down
52 changes: 27 additions & 25 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/sourcegraph/src-cli

go 1.22

toolchain go1.22.2

require (
cloud.google.com/go/storage v1.30.1
github.com/atotto/clipboard v0.1.4
Expand All @@ -26,14 +28,14 @@ require (
github.com/mattn/go-isatty v0.0.19
github.com/neelance/parallel v0.0.0-20160708114440-4de9ce63d14c
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/sourcegraph/conc v0.3.0
github.com/sourcegraph/conc v0.3.1-0.20240108182409-4afefce20f9b
github.com/sourcegraph/go-diff v0.6.2-0.20221123165719-f8cd299c40f3
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf
github.com/sourcegraph/scip v0.3.1-0.20230627154934-45df7f6d33fc
github.com/sourcegraph/sourcegraph/lib v0.0.0-20231108085505-01324538d7f0
github.com/sourcegraph/sourcegraph/lib v0.0.0-20240510113214-3a8666b99a37
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.17.0
golang.org/x/sync v0.3.0
golang.org/x/net v0.21.0
golang.org/x/sync v0.6.0
google.golang.org/api v0.132.0
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -48,23 +50,23 @@ require (
github.com/google/pprof v0.0.0-20230602150820-91b7bce49751 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.12.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/onsi/ginkgo/v2 v2.9.7 // indirect
github.com/onsi/gomega v1.27.8 // indirect
go.uber.org/goleak v1.2.1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230717213848-3f92550aa753 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230717213848-3f92550aa753 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
)

require (
cloud.google.com/go v0.110.4 // indirect
cloud.google.com/go/compute v1.22.0 // indirect
cloud.google.com/go v0.110.9 // indirect
cloud.google.com/go/compute v1.23.2 // indirect
cloud.google.com/go/compute/metadata v0.2.4-0.20230617002413-005d2dfb6b68 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/iam v1.1.4 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
Expand Down Expand Up @@ -94,9 +96,9 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.8.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/envoyproxy/protoc-gen-validate v0.10.1 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/getsentry/sentry-go v0.25.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand All @@ -109,7 +111,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
Expand Down Expand Up @@ -153,7 +155,7 @@ require (
github.com/pseudomuto/protoc-gen-doc v1.5.1 // indirect
github.com/pseudomuto/protokit v0.2.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sourcegraph/log v0.0.0-20231018134238-fbadff7458bb // indirect
github.com/spf13/cobra v1.7.0 // indirect
Expand All @@ -169,18 +171,18 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.10.0 // indirect
golang.org/x/tools v0.18.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230717213848-3f92550aa753 // indirect
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0 // indirect
gopkg.in/inf.v0 v0.9.1 // direct
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
Expand Down
Loading
Loading