diff --git a/data/model/repository.py b/data/model/repository.py index c518fc5f7..4a732d346 100644 --- a/data/model/repository.py +++ b/data/model/repository.py @@ -341,27 +341,6 @@ def get_sorted_matching_repositories(prefix, only_public, checker, limit=10): return results -def get_matching_repositories(repo_term, username=None, limit=10, include_public=True): - namespace_term = repo_term - name_term = repo_term - - visible = _visible_repository_query(username, include_public=include_public) - - search_clauses = (Repository.name ** ('%' + name_term + '%') | - Namespace.username ** ('%' + namespace_term + '%')) - - # Handle the case where the user has already entered a namespace path. - if repo_term.find('/') > 0: - parts = repo_term.split('/', 1) - namespace_term = '/'.join(parts[:-1]) - name_term = parts[-1] - - search_clauses = (Repository.name ** ('%' + name_term + '%') & - Namespace.username ** ('%' + namespace_term + '%')) - - return visible.where(search_clauses).limit(limit) - - def lookup_repository(repo_id): try: return Repository.get(Repository.id == repo_id) diff --git a/endpoints/api/search.py b/endpoints/api/search.py index 35941ae45..ba961080f 100644 --- a/endpoints/api/search.py +++ b/endpoints/api/search.py @@ -95,38 +95,6 @@ class EntitySearch(ApiResource): } -@resource('/v1/find/repository') -class FindRepositories(ApiResource): - """ Resource for finding repositories. """ - @parse_args - @query_param('query', 'The prefix to use when querying for repositories.', type=str, default='') - @require_scope(scopes.READ_REPO) - @nickname('findRepos') - def get(self, args): - """ Get a list of repositories that match the specified prefix query. """ - prefix = args['query'] - - def repo_view(repo): - return { - 'namespace': repo.namespace_user.username, - 'name': repo.name, - 'description': repo.description - } - - username = None - user = get_authenticated_user() - if user is not None: - username = user.username - - matching = model.repository.get_matching_repositories(prefix, username) - return { - 'repositories': [repo_view(repo) for repo in matching - if (repo.visibility.name == 'public' or - ReadRepositoryPermission(repo.namespace_user.username, repo.name).can())] - } - - - def search_entity_view(username, entity, get_short_name=None): kind = 'user' avatar_data = avatar.get_data_for_user(entity) diff --git a/endpoints/v1/index.py b/endpoints/v1/index.py index da35f28cb..9613fa892 100644 --- a/endpoints/v1/index.py +++ b/endpoints/v1/index.py @@ -296,16 +296,31 @@ def put_repository_auth(namespace, repository): abort(501, 'Not Implemented', issue='not-implemented') +def conduct_repo_search(username, query, results): + """ Finds matching repositories. """ + def can_read(repo): + if repo.is_public: + return True + + return ReadRepositoryPermission(repo.namespace_user.username, repo.name).can() + + only_public = username is None + matching_repos = model.repository.get_sorted_matching_repositories(query, only_public, can_read, + limit=5) + + for repo in matching_repos: + results.append({ + 'name': repo.name, + 'description': repo.description, + 'is_public': repo.is_public, + 'href': '/repository/' + repo.namespace_user.username + '/' + repo.name + }) + + @v1_bp.route('/search', methods=['GET']) @process_auth @anon_protect def get_search(): - def result_view(repo): - return { - "name": repo.namespace_user.username + '/' + repo.name, - "description": repo.description - } - query = request.args.get('q') username = None @@ -313,14 +328,8 @@ def get_search(): if user is not None: username = user.username - if query: - matching = model.repository.get_matching_repositories(query, username) - else: - matching = [] - - results = [result_view(repo) for repo in matching - if (repo.visibility.name == 'public' or - ReadRepositoryPermission(repo.namespace_user.username, repo.name).can())] + results = [] + conduct_repo_search(username, query, results) data = { "query": query, diff --git a/test/test_api_security.py b/test/test_api_security.py index 3b0f3cad1..4e2ee14ca 100644 --- a/test/test_api_security.py +++ b/test/test_api_security.py @@ -12,7 +12,7 @@ from endpoints.api import api_bp, api from endpoints.api.team import TeamMember, TeamMemberList, OrganizationTeam, TeamMemberInvite from endpoints.api.tag import RepositoryTagImages, RepositoryTag, ListRepositoryTags, RevertTag -from endpoints.api.search import FindRepositories, EntitySearch +from endpoints.api.search import EntitySearch from endpoints.api.image import RepositoryImageChanges, RepositoryImage, RepositoryImageList from endpoints.api.build import (FileDropResource, RepositoryBuildStatus, RepositoryBuildLogs, RepositoryBuildList, RepositoryBuildResource) @@ -118,25 +118,6 @@ class ApiTestCase(unittest.TestCase): finished_database_for_testing(self) -class TestFindRepositories(ApiTestCase): - def setUp(self): - ApiTestCase.setUp(self) - self._set_url(FindRepositories) - - def test_get_anonymous(self): - self._run_test('GET', 200, None, None) - - def test_get_freshuser(self): - self._run_test('GET', 200, 'freshuser', None) - - def test_get_reader(self): - self._run_test('GET', 200, 'reader', None) - - def test_get_devtable(self): - self._run_test('GET', 200, 'devtable', None) - - - class TestUserStarredRepositoryList(ApiTestCase): def setUp(self): ApiTestCase.setUp(self) diff --git a/test/test_api_usage.py b/test/test_api_usage.py index cfe0aa8c0..578540b45 100644 --- a/test/test_api_usage.py +++ b/test/test_api_usage.py @@ -20,7 +20,7 @@ from data.database import RepositoryActionCount from endpoints.api.team import TeamMember, TeamMemberList, TeamMemberInvite, OrganizationTeam from endpoints.api.tag import RepositoryTagImages, RepositoryTag, RevertTag, ListRepositoryTags -from endpoints.api.search import FindRepositories, EntitySearch, ConductSearch +from endpoints.api.search import EntitySearch, ConductSearch from endpoints.api.image import RepositoryImage, RepositoryImageList from endpoints.api.build import (RepositoryBuildStatus, RepositoryBuildLogs, RepositoryBuildList, RepositoryBuildResource) @@ -1323,30 +1323,6 @@ class TestCreateRepo(ApiTestCase): self.assertEquals('newrepo', json['name']) -class TestFindRepos(ApiTestCase): - def test_findrepos_asguest(self): - json = self.getJsonResponse(FindRepositories, params=dict(query='p')) - self.assertEquals(len(json['repositories']), 1) - - self.assertEquals(json['repositories'][0]['namespace'], 'public') - self.assertEquals(json['repositories'][0]['name'], 'publicrepo') - - def test_findrepos_asuser(self): - self.login(NO_ACCESS_USER) - - json = self.getJsonResponse(FindRepositories, params=dict(query='p')) - self.assertEquals(len(json['repositories']), 1) - - self.assertEquals(json['repositories'][0]['namespace'], 'public') - self.assertEquals(json['repositories'][0]['name'], 'publicrepo') - - def test_findrepos_orgmember(self): - self.login(READ_ACCESS_USER) - - json = self.getJsonResponse(FindRepositories, params=dict(query='p')) - self.assertGreater(len(json['repositories']), 1) - - class TestListRepos(ApiTestCase): def test_listrepos_asguest(self): # Queries: Base + the list query