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

vsce: patch release v2.2.13 #43204

Merged
merged 2 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions client/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@
"category": "Sourcegraph",
"title": "Remove Repository from Sourcegraph File System",
"icon": "$(trash)"
},
{
"command": "sourcegraph.auth",
"category": "Sourcegraph",
"title": "Log in to Sourcegraph"
}
],
"authentication": [
Expand Down Expand Up @@ -232,7 +227,6 @@
"package": "yarn run -T ts-node ./scripts/package.ts",
"prebuild": "yarn build-inline-extensions",
"prewatch": "yarn build-inline-extensions",
"vscode:prepublish": "yarn build-inline-extensions && yarn build",
"build-inline-extensions": "node scripts/build-inline-extensions",
"task:gulp": "yarn run -T cross-env NODE_OPTIONS=\"--max_old_space_size=8192\" gulp",
"build:esbuild": "NODE_ENV=development yarn task:gulp esbuild",
Expand All @@ -252,5 +246,8 @@
"release:minor": "VSCE_RELEASE_TYPE=minor yarn run -T ts-node ./scripts/release.ts",
"release:patch": "VSCE_RELEASE_TYPE=patch yarn run -T ts-node ./scripts/release.ts",
"release:pre": "VSCE_RELEASE_TYPE=prerelease yarn run -T ts-node ./scripts/release.ts"
},
"devDependencies": {
"vsce": "^2.7.0"
}
}
3 changes: 2 additions & 1 deletion client/vscode/scripts/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import fs from 'fs'
const originalPackageJson = fs.readFileSync('package.json').toString()

try {
childProcess.execSync('yarn build-inline-extensions && yarn build', { stdio: 'inherit' })
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const packageJson: any = JSON.parse(originalPackageJson)
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
packageJson.name = 'sourcegraph'
fs.writeFileSync('package.json', JSON.stringify(packageJson))

childProcess.execSync('yarn vsce package --yarn --allow-star-activation -o dist', { stdio: 'inherit' })
childProcess.execSync('vsce package --yarn --allow-star-activation -o dist', { stdio: 'inherit' })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure if this would work on CI (vsce might not be in the PATH). Does it work to run yarn vsce here considering you moved vsce to be a dependency of this project? I guess we can also just try this on CI and if it works we're good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I run it locally it was giving me the error message about duplicated workspace name --do you get the same error as well? It might not work the same on CI though so we can definitely make the change again if it doesn't run, what do you think?

} finally {
fs.writeFileSync('package.json', originalPackageJson)
}
9 changes: 5 additions & 4 deletions client/vscode/scripts/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ const tokens = {
// Assume this is for testing purpose if tokens are not found
const hasTokens = tokens.vscode !== undefined && tokens.openvsx !== undefined
const commands = {
vscode_info: 'yarn vsce show sourcegraph.sourcegraph --json',
vscode_info: 'vsce show sourcegraph.sourcegraph --json',
// To publish to VS Code Marketplace
vscode_publish: `yarn vsce publish ${isPreRelease} --pat $VSCODE_MARKETPLACE_TOKEN --yarn --allow-star-activation`,
vscode_publish: `vsce publish ${isPreRelease} --pat $VSCODE_MARKETPLACE_TOKEN --yarn --allow-star-activation`,
// To package the extension without publishing
vscode_package: `yarn vsce package ${isPreRelease} --yarn --allow-star-activation`,
vscode_package: `vsce package ${isPreRelease} --yarn --allow-star-activation`,
// To publish to the open-vsx registry
openvsx_publish: 'yarn npx --yes ovsx publish --yarn -p $VSCODE_OPENVSX_TOKEN',
openvsx_publish: 'npx --yes ovsx publish --yarn -p $VSCODE_OPENVSX_TOKEN',
}
// Publish the extension with the correct extension name "sourcegraph"
try {
childProcess.execSync('yarn build-inline-extensions && yarn build', { stdio: 'inherit' })
// Get the latest release version nubmer of the last release from VS Code Marketplace using the vsce cli tool
const response = childProcess.execSync(commands.vscode_info).toString()
/*
Expand Down
6 changes: 3 additions & 3 deletions client/vscode/src/backend/authenticatedUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export function observeAuthenticatedUser(secretStorage: vscode.SecretStorage): O

function updateAuthenticatedUser(): void {
requestGraphQLFromVSCode<CurrentAuthStateResult, CurrentAuthStateVariables>(currentAuthStateQuery, {})
.then(async authenticatedUserResult => {
.then(authenticatedUserResult => {
authenticatedUsers.next(authenticatedUserResult.data ? authenticatedUserResult.data.currentUser : null)
if (!authenticatedUserResult.data) {
await secretStorage.delete(scretTokenKey)
throw new Error('Not an authenticated user')
}
authenticatedUsers.next(authenticatedUserResult.data ? authenticatedUserResult.data.currentUser : null)
})
.catch(error => {
console.error('core auth error', error)
Expand Down
39 changes: 10 additions & 29 deletions client/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ import { openSourcegraphUriCommand } from './file-system/commands'
import { initializeSourcegraphFileSystem } from './file-system/initialize'
import { SourcegraphUri } from './file-system/SourcegraphUri'
import { Event } from './graphql-operations'
import { accessTokenSetting } from './settings/accessTokenSetting'
import { endpointRequestHeadersSetting, endpointSetting, setEndpoint } from './settings/endpointSetting'
import { accessTokenSetting, processOldToken } from './settings/accessTokenSetting'
import { endpointRequestHeadersSetting, endpointSetting } from './settings/endpointSetting'
import { invalidateContextOnSettingsChange } from './settings/invalidation'
import { LocalStorageService, SELECTED_SEARCH_CONTEXT_SPEC_KEY } from './settings/LocalStorageService'
import { watchUninstall } from './settings/uninstall'
import { createVSCEStateMachine, VSCEQueryState } from './state'
import { focusSearchPanel, openSourcegraphLinks, registerWebviews, copySourcegraphLinks } from './webview/commands'
import { processOldToken, scretTokenKey, SourcegraphAuthProvider } from './webview/platform/AuthProvider'
import { scretTokenKey, SourcegraphAuthActions, SourcegraphAuthProvider } from './webview/platform/AuthProvider'
/**
* See CONTRIBUTING docs for the Architecture Diagram
*/
export async function activate(context: vscode.ExtensionContext): Promise<void> {
const secretStorage = context.secrets
// Move token from user setting to secret storage
await processOldToken(secretStorage)
// Register SourcegraphAuthProvider
context.subscriptions.push(
vscode.authentication.registerAuthenticationProvider(
Expand All @@ -43,10 +41,12 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
new SourcegraphAuthProvider(secretStorage)
)
)
const session = await vscode.authentication.getSession(endpointSetting(), [], { createIfNone: false })
const authenticatedUser = observeAuthenticatedUser(secretStorage)
await processOldToken(secretStorage)
const initialInstanceURL = endpointSetting()
const initialAccessToken = await secretStorage.get(scretTokenKey)
const createIfNone = initialAccessToken ? { createIfNone: true } : { createIfNone: false }
const session = await vscode.authentication.getSession(endpointSetting(), [], createIfNone)
const authenticatedUser = observeAuthenticatedUser(secretStorage)
const localStorageService = new LocalStorageService(context.globalState)
const stateMachine = createVSCEStateMachine({ localStorageService })
invalidateContextOnSettingsChange({ context, stateMachine })
Expand All @@ -72,22 +72,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
sourcegraphURL: `${initialInstanceURL}/.api`,
session,
})
async function login(newtoken: string, newuri: string): Promise<void> {
try {
const newEndpoint = new URL(newuri)
const newTokenKey = newEndpoint.hostname
await secretStorage.store(newTokenKey, newtoken)
await setEndpoint(newEndpoint.href)
// stateMachine.emit({ type: 'sourcegraph_url_change' })
} catch (error) {
console.error(error)
}
}
async function logout(): Promise<void> {
await secretStorage.delete(scretTokenKey)
await setEndpoint(undefined)
extensionCoreAPI.reloadWindow()
}
const authActions = new SourcegraphAuthActions(secretStorage)
const extensionCoreAPI: ExtensionCoreAPI = {
panelInitialized: panelId => initializedPanelIDs.next(panelId),
observeState: () => proxySubscribable(stateMachine.observeState()),
Expand All @@ -103,8 +88,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
openLink: uri => openSourcegraphLinks(uri),
copyLink: uri => copySourcegraphLinks(uri),
getAccessToken: accessTokenSetting(context.secrets),
removeAccessToken: () => logout(),
setEndpointUri: (accessToken, uri) => login(accessToken, uri),
removeAccessToken: () => authActions.logout(),
setEndpointUri: (accessToken, uri) => authActions.login(accessToken, uri),
reloadWindow: () => vscode.commands.executeCommand('workbench.action.reloadWindow'),
focusSearchPanel,
streamSearch,
Expand Down Expand Up @@ -134,8 +119,4 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
initializeCodeSharingCommands(context, eventSourceType, localStorageService)
// Watch for uninstall to log uninstall event
watchUninstall(eventSourceType, localStorageService)

// Add Sourcegraph to workspace recommendations (disabled for now as it was reported to violate
// VS Code's UX guidelines for notifications: https://code.visualstudio.com/api/ux-guidelines/notifications)
// recommendSourcegraph(localStorageService).catch(() => {})
}
15 changes: 14 additions & 1 deletion client/vscode/src/settings/accessTokenSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@ import { scretTokenKey } from '../webview/platform/AuthProvider'
import { endpointHostnameSetting, endpointProtocolSetting } from './endpointSetting'
import { readConfiguration } from './readConfiguration'

// IMPORTANT: Call this function only once when extention is first activated
export async function processOldToken(secretStorage: vscode.SecretStorage): Promise<void> {
// Process the token that lives in user configuration
// Move them to secrets and then remove them by setting it as undefined
const storageToken = await secretStorage.get(scretTokenKey)
const oldToken = vscode.workspace.getConfiguration().get<string>('sourcegraph.accessToken') || ''
if (!storageToken && oldToken.length > 8) {
await secretStorage.store(scretTokenKey, oldToken)
await removeOldAccessTokenSetting()
}
return
}

export async function accessTokenSetting(secretStorage: vscode.SecretStorage): Promise<string> {
const currentToken = await secretStorage.get(scretTokenKey)
return currentToken || ''
}

export async function removeAccessTokenSetting(): Promise<void> {
export async function removeOldAccessTokenSetting(): Promise<void> {
await readConfiguration().update('accessToken', undefined, vscode.ConfigurationTarget.Global)
await readConfiguration().update('accessToken', undefined, vscode.ConfigurationTarget.Workspace)
return
Expand Down
16 changes: 10 additions & 6 deletions client/vscode/src/settings/endpointSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import * as vscode from 'vscode'
import { readConfiguration } from './readConfiguration'

export function endpointSetting(): string {
const url = readConfiguration().get<string>('url') || 'https://sourcegraph.com'
const url = vscode.workspace.getConfiguration().get<string>('sourcegraph.url') || 'https://sourcegraph.com'
return removeEndingSlash(url)
}

export async function setEndpoint(newEndpoint: string | undefined): Promise<void> {
const newEndpointURL = newEndpoint ? removeEndingSlash(newEndpoint) : undefined
await readConfiguration().update('url', newEndpointURL, vscode.ConfigurationTarget.Global)
await readConfiguration().update('url', newEndpointURL, vscode.ConfigurationTarget.Workspace)
export async function setEndpoint(newEndpoint: string): Promise<void> {
const newEndpointURL = newEndpoint ? removeEndingSlash(newEndpoint) : 'https://sourcegraph.com'
const currentEndpointHostname = new URL(endpointSetting()).hostname
const newEndpointHostname = new URL(newEndpointURL).hostname
if (currentEndpointHostname !== newEndpointHostname) {
await readConfiguration().update('url', newEndpointURL)
}
return
}

export function endpointHostnameSetting(): string {
Expand All @@ -27,7 +31,7 @@ export function endpointProtocolSetting(): string {
}

export function endpointRequestHeadersSetting(): object {
return readConfiguration().get<object>('requestHeaders') || {}
return vscode.workspace.getConfiguration().get<object>('sourcegraph.requestHeaders') || {}
}

function removeEndingSlash(uri: string): string {
Expand Down
4 changes: 3 additions & 1 deletion client/vscode/src/settings/recommendations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Disabled due to violation of the VS Code's UX guidelines for notifications
* To be revaluated in the future
* To be revaluated in the future: https://code.visualstudio.com/api/ux-guidelines/notifications
* This functions add Sourcegraph to workspace recommendations if haven't already
* eg: recommendSourcegraph(localStorageService).catch(() => {})
*/

import * as vscode from 'vscode'
Expand Down
13 changes: 0 additions & 13 deletions client/vscode/src/webview/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@ export function registerWebviews({
})
)

context.subscriptions.push(
vscode.commands.registerCommand('sourcegraph.auth', async (token: string, uri?: string) => {
// Get our PAT session.
await context.secrets.store(scretTokenKey, token)
const session = await vscode.authentication.getSession(uri || endpointSetting(), [], {
forceNewSession: true,
})
if (session) {
await vscode.window.showInformationMessage('Logged in sucessfully')
}
})
)

// Update `EventSource` Authorization header on access token / headers change.
// It will also be changed when the token has been changed --handled by Auth Provider
context.subscriptions.push(
Expand Down
37 changes: 24 additions & 13 deletions client/vscode/src/webview/platform/AuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
AuthenticationProvider,
AuthenticationProviderAuthenticationSessionsChangeEvent,
AuthenticationSession,
ConfigurationTarget,
commands,
Disposable,
Event,
EventEmitter,
Expand All @@ -13,10 +13,9 @@ import {

import polyfillEventSource from '@sourcegraph/shared/src/polyfills/vendor/eventSource'

import { endpointRequestHeadersSetting, endpointSetting } from '../../settings/endpointSetting'
import { readConfiguration } from '../../settings/readConfiguration'
import { endpointRequestHeadersSetting, endpointSetting, setEndpoint } from '../../settings/endpointSetting'

export const scretTokenKey = new URL(endpointSetting()).hostname
export const scretTokenKey = 'SOURCEGRAPH_AUTH'

class SourcegraphAuthSession implements AuthenticationSession {
public readonly account = {
Expand Down Expand Up @@ -126,14 +125,26 @@ export class SourcegraphAuthProvider implements AuthenticationProvider, Disposab
}
}

// Call this function only once when extention is first activated
export async function processOldToken(secretStorage: SecretStorage): Promise<void> {
// Process the token that lives in user configuration
// Move them to secrets and then remove them by setting it as undefined
const oldToken = readConfiguration().get<string>('accessToken')
if (oldToken && oldToken !== undefined) {
await secretStorage.store(scretTokenKey, oldToken)
await readConfiguration().update('accessToken', undefined, ConfigurationTarget.Global)
await readConfiguration().update('accessToken', undefined, ConfigurationTarget.Workspace)
export class SourcegraphAuthActions {
private currentEndpoint = endpointSetting()

constructor(private readonly secretStorage: SecretStorage) {}

public async login(newtoken: string, newuri: string): Promise<void> {
try {
await this.secretStorage.store(scretTokenKey, newtoken)
if (this.currentEndpoint !== newuri) {
await setEndpoint(newuri)
}
return
} catch (error) {
console.error(error)
}
}

public async logout(): Promise<void> {
await this.secretStorage.delete(scretTokenKey)
await commands.executeCommand('workbench.action.reloadWindow')
return
}
}
13 changes: 11 additions & 2 deletions client/vscode/src/webview/sidebars/auth/AuthSidebarView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const AuthSidebarView: React.FunctionComponent<React.PropsWithChildren<Au
const [hostname, setHostname] = useState(instanceHostname)
const [accessToken, setAccessToken] = useState<string | undefined>('initial')
const [endpointUrl, setEndpointUrl] = useState(instanceURL)
const sourcegraphDotCom = 'https://www.sourcegraph.com'
const isSourcegraphDotCom = useMemo(() => {
const hostname = new URL(instanceURL).hostname
if (hostname === 'sourcegraph.com' || hostname === 'www.sourcegraph.com') {
Expand Down Expand Up @@ -90,6 +91,13 @@ export const AuthSidebarView: React.FunctionComponent<React.PropsWithChildren<Au
setEndpointUrl(event.target.value)
}, [])

const onInstanceTypeChange = useCallback(() => {
setUsePrivateInstance(!usePrivateInstance)
if (!usePrivateInstance) {
setEndpointUrl(sourcegraphDotCom)
}
}, [usePrivateInstance])

const validateAccessToken: React.FormEventHandler<HTMLFormElement> = (event): void => {
event.preventDefault()
if (state !== 'validating' && accessToken) {
Expand All @@ -111,9 +119,10 @@ export const AuthSidebarView: React.FunctionComponent<React.PropsWithChildren<Au
.toPromise()
currentAuthStateResult
.then(async ({ data }) => {
await extensionCoreAPI.setEndpointUri(accessToken, endpointUrl)
if (data?.currentUser) {
await extensionCoreAPI.setEndpointUri(accessToken, endpointUrl)
setState('success')
return
}
setState('failure')
return
Expand Down Expand Up @@ -270,7 +279,7 @@ export const AuthSidebarView: React.FunctionComponent<React.PropsWithChildren<Au
</Alert>
)}
<Text className="my-0">
<VSCodeLink onClick={() => setUsePrivateInstance(!usePrivateInstance)}>
<VSCodeLink onClick={() => onInstanceTypeChange()}>
{!usePrivateInstance ? 'Need to connect to a private instance?' : 'Not a private instance user?'}
</VSCodeLink>
</Text>
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6097,6 +6097,8 @@ __metadata:
"@sourcegraph/vscode@workspace:client/vscode":
version: 0.0.0-use.local
resolution: "@sourcegraph/vscode@workspace:client/vscode"
dependencies:
vsce: ^2.7.0
languageName: unknown
linkType: soft

Expand Down