diff --git a/x-pack/plugins/code/common/git_url_utils.test.ts b/x-pack/plugins/code/common/git_url_utils.test.ts index 44caa9d6d53d..1c35e383753a 100644 --- a/x-pack/plugins/code/common/git_url_utils.test.ts +++ b/x-pack/plugins/code/common/git_url_utils.test.ts @@ -30,7 +30,7 @@ test('Git url validation', () => { // An url without protocol expect(isValidGitUrl('/Users/elastic/elasticsearch')).toBeFalsy(); - expect(isValidGitUrl('github.com/elastic/elasticsearch')).toBeFalsy(); + expect(isValidGitUrl('github.com/elastic/elasticsearch')).toBeTruthy(); // An valid git url but without whitelisted host expect(isValidGitUrl('https://github.com/elastic/elasticsearch.git', ['gitlab.com'])).toBeFalsy(); diff --git a/x-pack/plugins/code/common/git_url_utils.ts b/x-pack/plugins/code/common/git_url_utils.ts index a12cf4355632..7ab58f0686ad 100644 --- a/x-pack/plugins/code/common/git_url_utils.ts +++ b/x-pack/plugins/code/common/git_url_utils.ts @@ -11,26 +11,23 @@ export function isValidGitUrl( hostWhitelist?: string[], protocolWhitelist?: string[] ): boolean { - const regex = /(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)?(\/?|\#[-\d\w._]+?)$/; - const isPatternValid = regex.test(url); + try { + const repo = GitUrlParse(url); - if (!isPatternValid) { - return false; - } - - const repo = GitUrlParse(url); + let isHostValid = true; + if (hostWhitelist && hostWhitelist.length > 0) { + const hostSet = new Set(hostWhitelist); + isHostValid = hostSet.has(repo.source); + } - let isHostValid = true; - if (hostWhitelist && hostWhitelist.length > 0) { - const hostSet = new Set(hostWhitelist); - isHostValid = hostSet.has(repo.source); - } + let isProtocolValid = true; + if (protocolWhitelist && protocolWhitelist.length > 0) { + const protocolSet = new Set(protocolWhitelist); + isProtocolValid = protocolSet.has(repo.protocol); + } - let isProtocolValid = true; - if (protocolWhitelist && protocolWhitelist.length > 0) { - const protocolSet = new Set(protocolWhitelist); - isProtocolValid = protocolSet.has(repo.protocol); + return isHostValid && isProtocolValid; + } catch (error) { + return false; } - - return isHostValid && isProtocolValid; } diff --git a/x-pack/plugins/code/common/repository_utils.test.ts b/x-pack/plugins/code/common/repository_utils.test.ts index 57756d9aa98c..244e42497ab4 100644 --- a/x-pack/plugins/code/common/repository_utils.test.ts +++ b/x-pack/plugins/code/common/repository_utils.test.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { FileTreeItemType } from '../model'; import { RepositoryUtils } from './repository_utils'; test('Repository url parsing', () => { @@ -107,3 +108,72 @@ test('Normalize repository index name', () => { const indexName4 = RepositoryUtils.normalizeRepoUriToIndexName('github.com/elastic/kibana-code'); expect(indexName3 === indexName4).toBeFalsy(); }); + +test('Parse repository uri', () => { + expect(RepositoryUtils.orgNameFromUri('github.com/elastic/kibana')).toEqual('elastic'); + expect(RepositoryUtils.repoNameFromUri('github.com/elastic/kibana')).toEqual('kibana'); + expect(RepositoryUtils.repoFullNameFromUri('github.com/elastic/kibana')).toEqual( + 'elastic/kibana' + ); + + // For invalid repository uri + expect(() => { + RepositoryUtils.orgNameFromUri('foo/bar'); + }).toThrowError('Invalid repository uri.'); + expect(() => { + RepositoryUtils.repoNameFromUri('foo/bar'); + }).toThrowError('Invalid repository uri.'); + expect(() => { + RepositoryUtils.repoFullNameFromUri('foo/bar'); + }).toThrowError('Invalid repository uri.'); +}); + +test('Repository local path', () => { + expect(RepositoryUtils.repositoryLocalPath('/tmp', 'github.com/elastic/kibana')).toEqual( + '/tmp/github.com/elastic/kibana' + ); + expect(RepositoryUtils.repositoryLocalPath('tmp', 'github.com/elastic/kibana')).toEqual( + 'tmp/github.com/elastic/kibana' + ); +}); + +test('Parse location to url', () => { + expect( + RepositoryUtils.locationToUrl({ + uri: 'git://github.com/elastic/eui/blob/master/generator-eui/app/component.js', + range: { + start: { + line: 4, + character: 17, + }, + end: { + line: 27, + character: 1, + }, + }, + }) + ).toEqual('/github.com/elastic/eui/blob/master/generator-eui/app/component.js!L5:17'); +}); + +test('Get all files from a repository file tree', () => { + expect( + RepositoryUtils.getAllFiles({ + name: 'foo', + type: FileTreeItemType.Directory, + path: '/foo', + children: [ + { + name: 'bar', + type: FileTreeItemType.File, + path: '/foo/bar', + }, + { + name: 'boo', + type: FileTreeItemType.File, + path: '/foo/boo', + }, + ], + childrenCount: 2, + }) + ).toEqual(['/foo/bar', '/foo/boo']); +}); diff --git a/x-pack/plugins/code/common/repository_utils.ts b/x-pack/plugins/code/common/repository_utils.ts index 2e01415edd14..925ae5931338 100644 --- a/x-pack/plugins/code/common/repository_utils.ts +++ b/x-pack/plugins/code/common/repository_utils.ts @@ -36,9 +36,9 @@ export class RepositoryUtils { const segs = repoUri.split('/'); if (segs && segs.length === 3) { return segs[1]; - } else { - return 'invalid'; } + + throw new Error('Invalid repository uri.'); } // From uri 'origin/org/name' to 'name' @@ -46,9 +46,9 @@ export class RepositoryUtils { const segs = repoUri.split('/'); if (segs && segs.length === 3) { return segs[2]; - } else { - return 'invalid'; } + + throw new Error('Invalid repository uri.'); } // From uri 'origin/org/name' to 'org/name' @@ -56,9 +56,9 @@ export class RepositoryUtils { const segs = repoUri.split('/'); if (segs && segs.length === 3) { return segs[1] + '/' + segs[2]; - } else { - return 'invalid'; } + + throw new Error('Invalid repository uri.'); } // Return the local data path of a given repository. diff --git a/x-pack/plugins/code/public/components/file_tree/file_tree.test.tsx b/x-pack/plugins/code/public/components/file_tree/file_tree.test.tsx index 855970b527b5..f09a45016d2b 100644 --- a/x-pack/plugins/code/public/components/file_tree/file_tree.test.tsx +++ b/x-pack/plugins/code/public/components/file_tree/file_tree.test.tsx @@ -17,7 +17,7 @@ const location: Location = createLocation({ pathname: '/github.com/google/guava/tree/master/guava/src/com/google', }); -const match: match = createMatch({ +const m: match = createMatch({ path: '/:resource/:org/:repo/:pathType(blob|tree)/:revision/:path*:goto(!.*)?', url: '/github.com/google/guava/tree/master/guava/src/com/google', isExact: true, @@ -40,7 +40,7 @@ test('render correctly', () => { node={props.node} openedPaths={props.openedPaths} history={history} - match={match} + match={m} location={location} closeTreePath={mockFunction} openTreePath={mockFunction} diff --git a/x-pack/plugins/code/public/components/query_bar/components/typeahead/__snapshots__/suggestion_component.test.tsx.snap b/x-pack/plugins/code/public/components/query_bar/components/typeahead/__snapshots__/suggestion_component.test.tsx.snap index b0039bf50a8e..daa1c1e2bf9f 100644 --- a/x-pack/plugins/code/public/components/query_bar/components/typeahead/__snapshots__/suggestion_component.test.tsx.snap +++ b/x-pack/plugins/code/public/components/query_bar/components/typeahead/__snapshots__/suggestion_component.test.tsx.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`render file item 1`] = ` - - + `; exports[`render repository item 1`] = ` - - + `; exports[`render symbol item 1`] = ` - - + `; diff --git a/x-pack/plugins/code/public/components/query_bar/components/typeahead/__snapshots__/suggestions_component.test.tsx.snap b/x-pack/plugins/code/public/components/query_bar/components/typeahead/__snapshots__/suggestions_component.test.tsx.snap index fa854103ace3..d98864ac096c 100644 --- a/x-pack/plugins/code/public/components/query_bar/components/typeahead/__snapshots__/suggestions_component.test.tsx.snap +++ b/x-pack/plugins/code/public/components/query_bar/components/typeahead/__snapshots__/suggestions_component.test.tsx.snap @@ -212,7 +212,7 @@ exports[`render full suggestions component 1`] = ` - - +
- - +
- - - + - +
diff --git a/x-pack/test/functional/apps/code/code_intelligence.ts b/x-pack/test/functional/apps/code/code_intelligence.ts index b3abf3835c8c..7ab61522ccb7 100644 --- a/x-pack/test/functional/apps/code/code_intelligence.ts +++ b/x-pack/test/functional/apps/code/code_intelligence.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import expect from 'expect.js'; +import expect from '@kbn/expect'; import { TestInvoker } from './lib/types'; // tslint:disable:no-default-export diff --git a/x-pack/test/functional/apps/code/explore_repository.ts b/x-pack/test/functional/apps/code/explore_repository.ts index fc2a70acad00..e2c2610d970f 100644 --- a/x-pack/test/functional/apps/code/explore_repository.ts +++ b/x-pack/test/functional/apps/code/explore_repository.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import expect from 'expect.js'; +import expect from '@kbn/expect'; import { TestInvoker } from './lib/types'; // tslint:disable:no-default-export diff --git a/x-pack/test/functional/apps/code/manage_repositories.ts b/x-pack/test/functional/apps/code/manage_repositories.ts index 2efed49473bf..935b5e3200d0 100644 --- a/x-pack/test/functional/apps/code/manage_repositories.ts +++ b/x-pack/test/functional/apps/code/manage_repositories.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import expect from 'expect.js'; +import expect from '@kbn/expect'; import { TestInvoker } from './lib/types'; // tslint:disable:no-default-export diff --git a/x-pack/test/functional/apps/code/search.ts b/x-pack/test/functional/apps/code/search.ts index 60ec8b3ad86a..8d9018ebebbd 100644 --- a/x-pack/test/functional/apps/code/search.ts +++ b/x-pack/test/functional/apps/code/search.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import expect from 'expect.js'; +import expect from '@kbn/expect'; import { TestInvoker } from './lib/types'; // tslint:disable:no-default-export diff --git a/x-pack/test/functional/apps/code/with_security.ts b/x-pack/test/functional/apps/code/with_security.ts index b75d839f37cb..f66882cb6a70 100644 --- a/x-pack/test/functional/apps/code/with_security.ts +++ b/x-pack/test/functional/apps/code/with_security.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import expect from 'expect.js'; +import expect from '@kbn/expect'; import { TestInvoker } from './lib/types'; // tslint:disable:no-default-export