@@ -5,82 +5,76 @@ import { currentUserQuery } from '../queries/currentUserQuery'
55import { endpointSetting } from './endpointSetting'
66import { readConfiguration } from './readConfiguration'
77
8- let cachedAccessToken : Promise < string > | undefined
8+ let cachedAccessToken : Promise < string | undefined > | undefined
99const invalidAccessTokens = new Set < string > ( )
1010
11- export function accessTokenSetting ( ) : Promise < string > {
12- const fromSettings = readConfiguration ( ) . get < string > ( 'accessToken' , '' )
11+ export function accessTokenSetting ( ) : string | undefined {
12+ const fromSettings = readConfiguration ( ) . get < string > ( 'accessToken' )
1313 if ( fromSettings ) {
14- return Promise . resolve ( fromSettings )
14+ return fromSettings
1515 }
1616
1717 const environmentVariable = process . env . SRC_ACCESS_TOKEN
1818 if ( environmentVariable && ! invalidAccessTokens . has ( environmentVariable ) ) {
19- return Promise . resolve ( environmentVariable )
19+ return environmentVariable
2020 }
2121
22- return promptUserForAccessTokenSetting ( )
22+ return undefined
2323}
2424
2525export async function deleteAccessTokenSetting ( tokenValueToDelete : string ) : Promise < void > {
2626 invalidAccessTokens . add ( tokenValueToDelete )
2727 const currentValue = readConfiguration ( ) . get < string > ( 'accessToken' )
2828 if ( currentValue === tokenValueToDelete ) {
2929 cachedAccessToken = undefined
30- await readConfiguration ( ) . update ( 'accessToken' , undefined )
30+ await readConfiguration ( ) . update ( 'accessToken' , undefined , vscode . ConfigurationTarget . Global )
31+ } else {
32+ log . appendLine (
33+ `can't delete access token '${ tokenValueToDelete } ' because it doesn't match ` +
34+ `existing configuration value '${ currentValue || 'undefined' } '`
35+ )
3136 }
3237}
3338
34- export async function promptUserForAccessTokenSetting ( title ? : string ) : Promise < string > {
39+ export function promptUserForAccessTokenSetting ( title : string , detail : string ) : Promise < string | undefined > {
3540 if ( ! cachedAccessToken ) {
36- cachedAccessToken = unconditionallyPromptUserForAccessTokenSetting ( title )
41+ cachedAccessToken = unconditionallyPromptUserForAccessTokenSetting ( title , detail )
3742 cachedAccessToken . then (
38- ( ) => { } ,
43+ token => {
44+ log . appendLine ( `new access token from user: ${ token || 'undefined' } ` )
45+ } ,
3946 error => {
40- log . error ( 'askUserToCreateAccessToken ' , error )
47+ log . error ( 'promptUserForAccessTokenSetting ' , error )
4148 cachedAccessToken = undefined
4249 }
4350 )
4451 }
4552 return cachedAccessToken
4653}
47- async function unconditionallyPromptUserForAccessTokenSetting ( title ?: string ) : Promise < string > {
54+
55+ async function unconditionallyPromptUserForAccessTokenSetting (
56+ title : string ,
57+ detail : string
58+ ) : Promise < string | undefined > {
4859 const openBrowserMessage = 'Open browser to create an access token'
49- const learnMore = 'Learn more about access tokens'
50- const pasteAccessToken = 'Paste existing access token'
51- const userChoice = await vscode . window . showErrorMessage (
52- title || 'Missing Sourcegraph Access Token' ,
53- {
54- modal : true ,
55- detail : 'An access token is required to use the Sourcegraph extension. To fix this problem, create a new access token on the Sourcegraph website or set the $SRC_ACCESS_TOKEN environment variable and restart VS Code.' ,
56- } ,
57- openBrowserMessage ,
58- learnMore ,
59- pasteAccessToken
60- )
61- if ( userChoice ) {
62- const openUrl =
63- userChoice === openBrowserMessage
64- ? `${ endpointSetting ( ) } /user/settings/tokens`
65- : userChoice === learnMore
66- ? 'https://docs.sourcegraph.com/cli/how-tos/creating_an_access_token'
67- : undefined
68- if ( openUrl ) {
69- await open ( openUrl )
70- }
71- const token = await vscode . window . showInputBox ( {
60+ const logout = 'Continue without an access token'
61+ const userChoice = await vscode . window . showErrorMessage ( title , { modal : true , detail } , openBrowserMessage , logout )
62+
63+ if ( userChoice === openBrowserMessage ) {
64+ await open ( `${ endpointSetting ( ) } /user/settings/tokens` )
65+ const newToken = await vscode . window . showInputBox ( {
7266 title : 'Paste your Sourcegraph access token here' ,
7367 ignoreFocusOut : true ,
7468 } )
75- if ( token ) {
69+ if ( newToken ) {
7670 try {
77- const currentUser = await currentUserQuery ( token )
71+ const currentUser = await currentUserQuery ( newToken )
72+ await readConfiguration ( ) . update ( 'accessToken' , newToken , vscode . ConfigurationTarget . Global )
7873 const successMessage = `Successfully logged into Sourcegraph as user '${ currentUser } '`
7974 log . appendLine ( successMessage )
8075 await vscode . window . showInformationMessage ( successMessage )
81- await readConfiguration ( ) . update ( 'accessToken' , token , vscode . ConfigurationTarget . Global )
8276 cachedAccessToken = undefined
83- return token
77+ return newToken
8478 } catch {
8579 await vscode . window . showErrorMessage (
8680 "Invalid Access Token. To fix this problem, update the 'sourcegraph.accessToken' setting and try again"
@@ -92,7 +86,8 @@ async function unconditionallyPromptUserForAccessTokenSetting(title?: string): P
9286 } else {
9387 log . error ( 'askUserToCreateAccessToken - The user decided not to open the browser' )
9488 }
95- throw new Error ( 'No access token' )
89+
90+ return undefined
9691}
9792
9893export async function updateAccessTokenSetting ( newValue ?: string ) : Promise < void > {
0 commit comments