Skip to content

Commit

Permalink
[Code] improve repository index naming (#33911)
Browse files Browse the repository at this point in the history
  • Loading branch information
mw-ding authored and zfy0701 committed Mar 27, 2019
1 parent ef1dc3f commit a3bee7e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 13 additions & 0 deletions x-pack/plugins/code/common/repository_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
14 changes: 10 additions & 4 deletions x-pack/plugins/code/common/repository_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a3bee7e

Please sign in to comment.