Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 0d6a0e7

Browse files
committed
show nicer message for unauthed users who try to use extensions
Previously, users would see a terse message like "to toggle coverage or edit settings, you must sign in". This was not very helpful, especially when the user was on GitHub (sign in where? GitHub? I am signed in!). Now it reads as follows: > Unable to update user setting git.blame.lineDecorations because you are not signed in. > > Sign into Sourcegraph on localhost:3080
1 parent 0f95297 commit 0d6a0e7

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

cmd/frontend/graphqlbackend/configuration.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ func (r *schemaResolver) ConfigurationMutation(ctx context.Context, args *struct
5555
if canAdmin, err := subject.ViewerCanAdminister(ctx); err != nil {
5656
return nil, err
5757
} else if !canAdmin {
58-
if !actor.FromContext(ctx).IsAuthenticated() {
59-
// TODO(sqs): Quick hack to show a less confusing error message when an anon user
60-
// toggles code coverage on Sourcegraph.com. Make the extension actually show a friendly
61-
// message and/or implement anon user settings on Sourcegraph.com.
62-
return nil, errors.New("to toggle coverage or edit settings, you must sign in or sign up")
63-
}
6458
return nil, errors.New("viewer is not allowed to edit these settings")
6559
}
6660

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"@babel/polyfill": "^7.0.0",
6767
"@slimsag/react-shortcuts": "^1.2.1",
6868
"@sourcegraph/codeintellify": "^3.9.0",
69-
"@sourcegraph/extensions-client-common": "^10.4.1",
69+
"@sourcegraph/extensions-client-common": "^10.4.2",
7070
"@sourcegraph/phabricator-extension": "^1.18.0",
7171
"@sqs/jsonc-parser": "^1.0.3",
7272
"@types/react": "16.4.14",

src/extensions/ExtensionsClientCommonContext.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import MenuIcon from 'mdi-react/MenuIcon'
2222
import SettingsIcon from 'mdi-react/SettingsIcon'
2323
import WarningIcon from 'mdi-react/WarningIcon'
2424
import { concat, Observable } from 'rxjs'
25-
import { distinctUntilChanged, map, switchMap, take } from 'rxjs/operators'
25+
import { distinctUntilChanged, map, switchMap, take, withLatestFrom } from 'rxjs/operators'
2626
import { InitData } from 'sourcegraph/module/extension/extensionHost'
2727
import { MessageTransports } from 'sourcegraph/module/protocol/jsonrpc2/connection'
2828
import { createWebWorkerMessageTransports } from 'sourcegraph/module/protocol/jsonrpc2/transports/webWorker'
2929
import ExtensionHostWorker from 'worker-loader!./extensionHost.worker'
30+
import { authenticatedUser } from '../auth'
3031
import { gql, queryGraphQL } from '../backend/graphql'
3132
import * as GQL from '../backend/graphqlschema'
3233
import { sendLSPHTTPRequests } from '../backend/lsp'
@@ -73,25 +74,44 @@ export function createExtensionsContextController(): ExtensionsContextController
7374
function updateExtensionSettings(subject: string, args: UpdateExtensionSettingsArgs): Observable<void> {
7475
return configurationCascade.pipe(
7576
take(1),
76-
switchMap(configurationCascade => {
77+
withLatestFrom(authenticatedUser),
78+
switchMap(([configurationCascade, authenticatedUser]) => {
7779
const subjectConfig = configurationCascade.subjects.find(s => s.id === subject)
7880
if (!subjectConfig) {
7981
throw new Error(`no configuration subject: ${subject}`)
8082
}
8183
const lastID = subjectConfig.latestSettings ? subjectConfig.latestSettings.id : null
8284

8385
let edit: GQL.IConfigurationEdit
86+
let editDescription: string
8487
if ('edit' in args && args.edit) {
8588
edit = { keyPath: toGQLKeyPath(args.edit.path), value: args.edit.value }
89+
editDescription = `update user setting ` + '`' + args.edit.path + '`'
8690
} else if ('extensionID' in args) {
8791
edit = {
8892
keyPath: toGQLKeyPath(['extensions', args.extensionID]),
8993
value: typeof args.enabled === 'boolean' ? args.enabled : null,
9094
}
95+
editDescription =
96+
`${typeof args.enabled === 'boolean' ? 'enable' : 'disable'} extension ` +
97+
'`' +
98+
args.extensionID +
99+
'`'
91100
} else {
92101
throw new Error('no edit')
93102
}
94103

104+
if (!authenticatedUser) {
105+
const u = new URL(window.context.appURL)
106+
throw new Error(
107+
`Unable to ${editDescription} because you are not signed in.` +
108+
'\n\n' +
109+
`[**Sign into Sourcegraph ${
110+
u.hostname === 'sourcegraph.com' ? '' : `on ${u.host}`
111+
}**](${`${u.href.replace(/\/$/, '')}/sign-in`})`
112+
)
113+
}
114+
95115
return editConfiguration(subject, lastID, edit)
96116
}),
97117
switchMap(() => concat(refreshConfiguration(), [void 0]))

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -971,10 +971,10 @@
971971
rxjs "^6.3.2"
972972
vscode-languageserver-types "^3.8.2"
973973

974-
"@sourcegraph/extensions-client-common@^10.4.1":
975-
version "10.4.1"
976-
resolved "https://registry.yarnpkg.com/@sourcegraph/extensions-client-common/-/extensions-client-common-10.4.1.tgz#2f40925509e9c6ce2d177e3a329a530290f79a69"
977-
integrity sha512-K8T4vfmhV6y/4QVcKyLahuBBOC15/YXzO9y/ggxekw/EDnqF2tXjMxQHg62EM8sM2p+F28pvs2aDZAI0QOMEyw==
974+
"@sourcegraph/extensions-client-common@^10.4.2":
975+
version "10.4.2"
976+
resolved "https://registry.yarnpkg.com/@sourcegraph/extensions-client-common/-/extensions-client-common-10.4.2.tgz#f964076bf460ca1b697ebe48e9a51a5d4c3f7fa7"
977+
integrity sha512-UPgOXGrDkoiOm7nPM2mdK68f1+qFEjUTAeBgDH8f5A2seMIvXVaoNAdregC7m4V/K1PTnAHort4X8j4Bpj+ZRw==
978978
dependencies:
979979
"@slimsag/react-shortcuts" "^1.2.0"
980980
bootstrap "^4.1.3"

0 commit comments

Comments
 (0)