diff --git a/x-pack/plugins/code/common/repository_utils.test.ts b/x-pack/plugins/code/common/repository_utils.test.ts index cefa2bf1e7e85..57756d9aa98c5 100644 --- a/x-pack/plugins/code/common/repository_utils.test.ts +++ b/x-pack/plugins/code/common/repository_utils.test.ts @@ -94,3 +94,16 @@ test('Repository url parsing with port', () => { org: 'elastic', }); }); + +test('Normalize repository index name', () => { + const indexName1 = RepositoryUtils.normalizeRepoUriToIndexName('github.com/elastic/Kibana'); + const indexName2 = RepositoryUtils.normalizeRepoUriToIndexName('github.com/elastic/kibana'); + + expect(indexName1 === indexName2).toBeFalsy(); + expect(indexName1).toEqual('github.aaakk.us.kg-elastic-kibana-e2b881a9'); + expect(indexName2).toEqual('github.aaakk.us.kg-elastic-kibana-7bf00473'); + + const indexName3 = RepositoryUtils.normalizeRepoUriToIndexName('github.com/elastic-kibana/code'); + const indexName4 = RepositoryUtils.normalizeRepoUriToIndexName('github.com/elastic/kibana-code'); + expect(indexName3 === indexName4).toBeFalsy(); +}); diff --git a/x-pack/plugins/code/common/repository_utils.ts b/x-pack/plugins/code/common/repository_utils.ts index a189df744ee2e..2e01415edd14c 100644 --- a/x-pack/plugins/code/common/repository_utils.ts +++ b/x-pack/plugins/code/common/repository_utils.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import crypto from 'crypto'; import GitUrlParse from 'git-url-parse'; import path from 'path'; import { Location } from 'vscode-languageserver'; @@ -66,10 +67,15 @@ export class RepositoryUtils { } public static normalizeRepoUriToIndexName(repoUri: RepositoryUri) { - return repoUri - .split('/') - .join('-') - .toLowerCase(); + const hash = crypto + .createHash('md5') + .update(repoUri) + .digest('hex') + .substring(0, 8); + const segs: string[] = repoUri.split('/'); + segs.push(hash); + // Elasticsearch index name is case insensitive + return segs.join('-').toLowerCase(); } public static locationToUrl(loc: Location) {