diff --git a/x-pack/legacy/plugins/code/server/__tests__/lsp_service.ts b/x-pack/legacy/plugins/code/server/__tests__/lsp_service.ts index 4c85eb49968d75..6480402db98276 100644 --- a/x-pack/legacy/plugins/code/server/__tests__/lsp_service.ts +++ b/x-pack/legacy/plugins/code/server/__tests__/lsp_service.ts @@ -85,6 +85,7 @@ describe('lsp_service tests', () => { } const repoUri = 'github.com/test/test_repo'; + const mockRndPath = '__random'; // @ts-ignore before(async () => { @@ -101,7 +102,7 @@ describe('lsp_service tests', () => { function mockLspService() { const esClient = mockEsClient(); - return new LspService( + const service = new LspService( '127.0.0.1', serverOptions, gitOps, @@ -110,6 +111,9 @@ describe('lsp_service tests', () => { new ConsoleLoggerFactory(), new RepositoryConfigController(esClient) ); + // @ts-ignore + service.workspaceHandler.randomPath = () => 'random'; + return service; } async function sendHoverRequest(lspservice: LspService, revision: string) { @@ -145,13 +149,13 @@ describe('lsp_service tests', () => { ctrlSpy.restore(); const workspaceFolderExists = fs.existsSync( - path.join(serverOptions.workspacePath, repoUri, revision) + path.join(serverOptions.workspacePath, repoUri, mockRndPath, revision) ); // workspace is opened assert.ok(workspaceFolderExists); const workspacePath = fs.realpathSync( - path.resolve(serverOptions.workspacePath, repoUri, revision) + path.resolve(serverOptions.workspacePath, repoUri, mockRndPath, revision) ); // workspace handler is working, filled workspacePath sinon.assert.calledWith( @@ -177,7 +181,12 @@ describe('lsp_service tests', () => { // send a dummy request to open a workspace; const response = await sendHoverRequest(lspservice, revision); assert.ok(response); - const workspacePath = path.resolve(serverOptions.workspacePath, repoUri, revision); + const workspacePath = path.resolve( + serverOptions.workspacePath, + repoUri, + mockRndPath, + revision + ); const workspaceFolderExists = fs.existsSync(workspacePath); // workspace is opened assert.ok(workspaceFolderExists); @@ -216,7 +225,12 @@ describe('lsp_service tests', () => { // send a dummy request to open a workspace; const response = await sendHoverRequest(lspservice, revision); assert.ok(response); - const workspacePath = path.resolve(serverOptions.workspacePath, repoUri, revision); + const workspacePath = path.resolve( + serverOptions.workspacePath, + repoUri, + mockRndPath, + revision + ); const git = simplegit(workspacePath); const workspaceCommit = await git.revparse(['HEAD']); // workspace is newest now diff --git a/x-pack/legacy/plugins/code/server/indexer/schema/document.ts b/x-pack/legacy/plugins/code/server/indexer/schema/document.ts index e28590bc328398..c2a2cd1e334d50 100644 --- a/x-pack/legacy/plugins/code/server/indexer/schema/document.ts +++ b/x-pack/legacy/plugins/code/server/indexer/schema/document.ts @@ -17,8 +17,6 @@ export const RepositoryDeleteStatusReservedField = 'repository_delete_status'; export const RepositoryIndexStatusReservedField = 'repository_index_status'; // The field name of repository config object nested in the Document index. export const RepositoryConfigReservedField = 'repository_config'; -// The field name of repository config object nested in the Document index. -export const RepositoryRandomPathReservedField = 'repository_random_path'; export const ALL_RESERVED = [ RepositoryReservedField, @@ -26,7 +24,6 @@ export const ALL_RESERVED = [ RepositoryDeleteStatusReservedField, RepositoryIndexStatusReservedField, RepositoryConfigReservedField, - RepositoryRandomPathReservedField, ]; // Correspond to model/search/Document @@ -107,9 +104,6 @@ export const DocumentSchema = { }, }, }, - [RepositoryRandomPathReservedField]: { - type: 'keyword', - }, // A single Repository Git Status object resides in this document index. [RepositoryGitStatusReservedField]: { properties: { diff --git a/x-pack/legacy/plugins/code/server/lsp/workspace_handler.ts b/x-pack/legacy/plugins/code/server/lsp/workspace_handler.ts index a9b8744f960280..dbd2ee455a99b1 100644 --- a/x-pack/legacy/plugins/code/server/lsp/workspace_handler.ts +++ b/x-pack/legacy/plugins/code/server/lsp/workspace_handler.ts @@ -9,6 +9,7 @@ import del from 'del'; import fs from 'fs'; import { delay } from 'lodash'; import path from 'path'; +import crypto from 'crypto'; import { ResponseMessage } from 'vscode-jsonrpc/lib/messages'; import { Hover, Location, TextDocumentPositionParams } from 'vscode-languageserver'; @@ -97,7 +98,7 @@ export class WorkspaceHandler { wt = await this.openWorktree( git, workspaceBranch, - await this.revisionDir(repositoryUri, ref), + await this.revisionDir(repositoryUri, ref, this.randomPath()), targetRevision ); } @@ -110,6 +111,10 @@ export class WorkspaceHandler { }; } + private randomPath() { + return crypto.randomBytes(4).toString('hex'); + } + public async openWorktree( git: SimpleGit, workspaceBranch: string, @@ -378,14 +383,20 @@ export class WorkspaceHandler { } } - public async revisionDir(repositoryUri: string, ref: string) { - return path.join(await this.workspaceDir(repositoryUri), ref); + public async revisionDir(repositoryUri: string, ref: string, randomStr: string = '') { + return path.join(await this.workspaceDir(repositoryUri, randomStr), ref); } - private async workspaceDir(repoUri: string) { - const randomStr = - this.objectClient && (await this.objectClient.getRepositoryRandomStr(repoUri)); + private async workspaceDir(repoUri: string, randomStr: string = '') { const base = path.join(this.workspacePath, repoUri); + if (randomStr === '') { + const git = await this.gitOps.openGit(repoUri); + const trees = await this.listWorktrees(git); + if (trees.size > 0) { + const wt = trees.values().next().value; + return path.dirname(wt.path); + } + } if (randomStr) { return path.join(base, `__${randomStr}`); } else { diff --git a/x-pack/legacy/plugins/code/server/routes/repository.ts b/x-pack/legacy/plugins/code/server/routes/repository.ts index 5947dc869968a2..862586b406de40 100644 --- a/x-pack/legacy/plugins/code/server/routes/repository.ts +++ b/x-pack/legacy/plugins/code/server/routes/repository.ts @@ -93,10 +93,7 @@ export function repositoryRoute( // Persist to elasticsearch await repoObjectClient.setRepository(repo.uri, repo); - const randomStr = Math.random() - .toString(36) - .substring(2, 15); - await repoObjectClient.setRepositoryRandomStr(repo.uri, randomStr); + // Kick off clone job const payload = { url: repoUrl, diff --git a/x-pack/legacy/plugins/code/server/search/repository_object_client.ts b/x-pack/legacy/plugins/code/server/search/repository_object_client.ts index c7deb2faa3e7f2..23ae73d35b8fc5 100644 --- a/x-pack/legacy/plugins/code/server/search/repository_object_client.ts +++ b/x-pack/legacy/plugins/code/server/search/repository_object_client.ts @@ -18,7 +18,6 @@ import { RepositoryIndexName, RepositoryIndexNamePrefix, RepositoryIndexStatusReservedField, - RepositoryRandomPathReservedField, RepositoryReservedField, RepositorySearchIndexWithScope, } from '../indexer/schema'; @@ -47,14 +46,6 @@ export class RepositoryObjectClient { return await this.getRepositoryObject(repoUri, RepositoryConfigReservedField); } - public async getRepository(repoUri: RepositoryUri): Promise { - return await this.getRepositoryObject(repoUri, RepositoryReservedField); - } - - public async getRepositoryRandomStr(repoUri: RepositoryUri): Promise { - return await this.getRepositoryObject(repoUri, RepositoryRandomPathReservedField); - } - public async getRepositories(uris: string[]): Promise { if (uris.length === 0) { return []; @@ -62,6 +53,10 @@ export class RepositoryObjectClient { return this.getRepositoriesInternal(RepositorySearchIndexWithScope(uris)); } + public async getRepository(repoUri: RepositoryUri): Promise { + return await this.getRepositoryObject(repoUri, RepositoryReservedField); + } + public async getAllRepositories(): Promise { return await this.getRepositoriesInternal(`${RepositoryIndexNamePrefix}*`); } @@ -107,10 +102,6 @@ export class RepositoryObjectClient { return await this.setRepositoryObject(repoUri, RepositoryConfigReservedField, config); } - public async setRepositoryRandomStr(repoUri: RepositoryUri, randomStr: string) { - return await this.setRepositoryObject(repoUri, RepositoryRandomPathReservedField, randomStr); - } - public async setRepository(repoUri: RepositoryUri, repo: Repository) { return await this.setRepositoryObject(repoUri, RepositoryReservedField, repo); }