2014-03-19 18:36:56 +00:00
|
|
|
from endpoints.api import (ApiResource, parse_args, query_param, truthy_bool, nickname, resource,
|
2014-08-19 23:05:28 +00:00
|
|
|
require_scope, path_param)
|
2014-03-13 20:31:37 +00:00
|
|
|
from data import model
|
2014-03-25 21:26:45 +00:00
|
|
|
from auth.permissions import (OrganizationMemberPermission, ViewTeamPermission,
|
2014-07-22 17:39:41 +00:00
|
|
|
ReadRepositoryPermission, UserAdminPermission,
|
|
|
|
AdministerOrganizationPermission)
|
2014-03-13 20:31:37 +00:00
|
|
|
from auth.auth_context import get_authenticated_user
|
2014-03-19 18:36:56 +00:00
|
|
|
from auth import scopes
|
2015-04-06 23:17:18 +00:00
|
|
|
from app import avatar, get_app_url
|
|
|
|
from operator import itemgetter
|
2014-03-13 20:31:37 +00:00
|
|
|
|
2015-04-06 23:17:18 +00:00
|
|
|
import math
|
2014-03-13 20:31:37 +00:00
|
|
|
|
|
|
|
@resource('/v1/entities/<prefix>')
|
|
|
|
class EntitySearch(ApiResource):
|
|
|
|
""" Resource for searching entities. """
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('prefix', 'The prefix of the entities being looked up')
|
2014-03-13 20:31:37 +00:00
|
|
|
@parse_args
|
|
|
|
@query_param('namespace', 'Namespace to use when querying for org entities.', type=str,
|
|
|
|
default='')
|
|
|
|
@query_param('includeTeams', 'Whether to include team names.', type=truthy_bool, default=False)
|
2014-07-22 17:39:41 +00:00
|
|
|
@query_param('includeOrgs', 'Whether to include orgs names.', type=truthy_bool, default=False)
|
2014-03-13 20:31:37 +00:00
|
|
|
@nickname('getMatchingEntities')
|
|
|
|
def get(self, args, prefix):
|
|
|
|
""" Get a list of entities that match the specified prefix. """
|
|
|
|
teams = []
|
2014-07-22 17:39:41 +00:00
|
|
|
org_data = []
|
2014-03-13 20:31:37 +00:00
|
|
|
|
|
|
|
namespace_name = args['namespace']
|
|
|
|
robot_namespace = None
|
|
|
|
organization = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
organization = model.get_organization(namespace_name)
|
|
|
|
|
|
|
|
# namespace name was an org
|
|
|
|
permission = OrganizationMemberPermission(namespace_name)
|
|
|
|
if permission.can():
|
|
|
|
robot_namespace = namespace_name
|
|
|
|
|
|
|
|
if args['includeTeams']:
|
2014-11-24 21:07:38 +00:00
|
|
|
teams = model.get_matching_teams(prefix, organization)
|
2014-03-13 20:31:37 +00:00
|
|
|
|
2014-07-22 17:39:41 +00:00
|
|
|
if args['includeOrgs'] and AdministerOrganizationPermission(namespace_name) \
|
|
|
|
and namespace_name.startswith(prefix):
|
|
|
|
org_data = [{
|
|
|
|
'name': namespace_name,
|
|
|
|
'kind': 'org',
|
|
|
|
'is_org_member': True,
|
2015-03-30 21:55:04 +00:00
|
|
|
'avatar': avatar.get_data_for_org(organization),
|
2014-07-22 17:39:41 +00:00
|
|
|
}]
|
|
|
|
|
2014-03-13 20:31:37 +00:00
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
# namespace name was a user
|
2014-03-18 19:58:37 +00:00
|
|
|
user = get_authenticated_user()
|
|
|
|
if user and user.username == namespace_name:
|
2014-03-25 21:26:45 +00:00
|
|
|
# Check if there is admin user permissions (login only)
|
|
|
|
admin_permission = UserAdminPermission(user.username)
|
|
|
|
if admin_permission.can():
|
|
|
|
robot_namespace = namespace_name
|
2014-03-13 20:31:37 +00:00
|
|
|
|
|
|
|
users = model.get_matching_users(prefix, robot_namespace, organization)
|
|
|
|
|
|
|
|
def entity_team_view(team):
|
|
|
|
result = {
|
|
|
|
'name': team.name,
|
|
|
|
'kind': 'team',
|
2015-03-30 21:55:04 +00:00
|
|
|
'is_org_member': True,
|
|
|
|
'avatar': avatar.get_data_for_team(team)
|
2014-03-13 20:31:37 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
|
|
|
|
def user_view(user):
|
|
|
|
user_json = {
|
|
|
|
'name': user.username,
|
|
|
|
'kind': 'user',
|
2015-03-30 21:55:04 +00:00
|
|
|
'is_robot': user.robot,
|
|
|
|
'avatar': avatar.get_data_for_user(user)
|
2014-03-13 20:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if organization is not None:
|
2015-03-30 21:55:04 +00:00
|
|
|
user_json['is_org_member'] = user.robot or user.is_org_member
|
2014-03-13 20:31:37 +00:00
|
|
|
|
|
|
|
return user_json
|
|
|
|
|
|
|
|
team_data = [entity_team_view(team) for team in teams]
|
|
|
|
user_data = [user_view(user) for user in users]
|
|
|
|
|
|
|
|
return {
|
2014-07-22 17:39:41 +00:00
|
|
|
'results': team_data + user_data + org_data
|
2014-03-13 20:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def team_view(orgname, team):
|
|
|
|
view_permission = ViewTeamPermission(orgname, team.name)
|
|
|
|
role = model.get_team_org_role(team).name
|
|
|
|
return {
|
|
|
|
'id': team.id,
|
|
|
|
'name': team.name,
|
|
|
|
'description': team.description,
|
|
|
|
'can_view': view_permission.can(),
|
|
|
|
'role': role
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-06 23:17:18 +00:00
|
|
|
@resource('/v1/find/all')
|
|
|
|
class ConductSearch(ApiResource):
|
|
|
|
""" Resource for finding users, repositories, teams, etc. """
|
|
|
|
@parse_args
|
|
|
|
@query_param('query', 'The search query.', type=str, default='')
|
|
|
|
@require_scope(scopes.READ_REPO)
|
|
|
|
@nickname('conductSearch')
|
|
|
|
def get(self, args):
|
|
|
|
""" Get a list of entities and resources that match the specified query. """
|
|
|
|
prefix = args['query']
|
|
|
|
username = None
|
|
|
|
results = []
|
|
|
|
|
|
|
|
def entity_view(entity):
|
|
|
|
kind = 'user'
|
|
|
|
avatar_data = avatar.get_data_for_user(entity)
|
|
|
|
href = '/user/' + entity.username
|
|
|
|
|
|
|
|
if entity.organization:
|
|
|
|
kind = 'organization'
|
|
|
|
avatar_data = avatar.get_data_for_org(entity)
|
|
|
|
href = '/organization/' + entity.username
|
|
|
|
elif entity.robot:
|
|
|
|
kind = 'robot'
|
|
|
|
href = '/user?tab=robots'
|
|
|
|
avatar_data = None
|
|
|
|
|
|
|
|
return {
|
|
|
|
'kind': kind,
|
|
|
|
'avatar': avatar_data,
|
|
|
|
'name': entity.username,
|
|
|
|
'score': 1,
|
|
|
|
'href': href
|
|
|
|
}
|
|
|
|
|
|
|
|
if get_authenticated_user():
|
|
|
|
username = get_authenticated_user().username
|
|
|
|
|
|
|
|
# Find the matching teams.
|
|
|
|
matching_teams = model.get_matching_user_teams(prefix, get_authenticated_user())
|
|
|
|
for team in matching_teams:
|
|
|
|
results.append({
|
|
|
|
'kind': 'team',
|
|
|
|
'name': team.name,
|
|
|
|
'organization': entity_view(team.organization),
|
|
|
|
'avatar': avatar.get_data_for_team(team),
|
|
|
|
'score': 2,
|
|
|
|
'href': '/organization/' + team.organization.username + '/teams/' + team.name
|
|
|
|
})
|
|
|
|
|
|
|
|
# Find the matching repositories.
|
|
|
|
matching_repos = model.get_matching_repositories(prefix, username)
|
|
|
|
matching_repo_counts = {t[0]: t[1] for t in model.get_repository_pull_counts(matching_repos)}
|
|
|
|
|
|
|
|
for repo in matching_repos:
|
|
|
|
results.append({
|
|
|
|
'kind': 'repository',
|
|
|
|
'namespace': entity_view(repo.namespace_user),
|
|
|
|
'name': repo.name,
|
|
|
|
'description': repo.description,
|
|
|
|
'is_public': repo.visibility.name == 'public',
|
|
|
|
'score': math.log(matching_repo_counts.get(repo.id, 1), 10),
|
|
|
|
'href': '/repository/' + repo.namespace_user.username + '/' + repo.name
|
|
|
|
})
|
|
|
|
|
|
|
|
# Find the matching users, robots and organizations.
|
|
|
|
matching_entities = model.get_matching_user_entities(prefix, get_authenticated_user())
|
|
|
|
for entity in matching_entities:
|
|
|
|
results.append(entity_view(entity))
|
|
|
|
|
|
|
|
return {'results': sorted(results, key=itemgetter('score'), reverse=True)}
|
|
|
|
|
|
|
|
|
2014-03-13 20:31:37 +00:00
|
|
|
@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='')
|
2014-03-25 21:26:45 +00:00
|
|
|
@require_scope(scopes.READ_REPO)
|
2014-03-13 20:31:37 +00:00
|
|
|
@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 {
|
2014-09-24 22:01:35 +00:00
|
|
|
'namespace': repo.namespace_user.username,
|
2014-03-13 20:31:37 +00:00
|
|
|
'name': repo.name,
|
|
|
|
'description': repo.description
|
|
|
|
}
|
|
|
|
|
|
|
|
username = None
|
2014-03-25 21:26:45 +00:00
|
|
|
user = get_authenticated_user()
|
|
|
|
if user is not None:
|
|
|
|
username = user.username
|
2014-03-13 20:31:37 +00:00
|
|
|
|
|
|
|
matching = model.get_matching_repositories(prefix, username)
|
|
|
|
return {
|
2014-03-25 21:26:45 +00:00
|
|
|
'repositories': [repo_view(repo) for repo in matching
|
2014-11-24 21:07:38 +00:00
|
|
|
if (repo.visibility.name == 'public' or
|
2014-09-24 22:01:35 +00:00
|
|
|
ReadRepositoryPermission(repo.namespace_user.username, repo.name).can())]
|
2014-07-22 17:39:41 +00:00
|
|
|
}
|