2015-05-14 20:47:38 +00:00
|
|
|
""" List, create and manage repositories. """
|
|
|
|
|
2014-03-10 22:30:41 +00:00
|
|
|
import logging
|
2015-03-10 05:03:39 +00:00
|
|
|
import datetime
|
2015-06-09 21:58:57 +00:00
|
|
|
|
2015-03-11 00:22:46 +00:00
|
|
|
from datetime import timedelta
|
2014-03-10 22:30:41 +00:00
|
|
|
|
2014-03-18 23:21:27 +00:00
|
|
|
from flask import request
|
2014-03-10 22:30:41 +00:00
|
|
|
|
|
|
|
from data import model
|
2015-06-09 21:58:57 +00:00
|
|
|
from data.database import (Repository as RepositoryTable, Visibility, RepositoryTag,
|
2015-07-15 21:25:41 +00:00
|
|
|
RepositoryActionCount, Namespace, fn)
|
2015-06-09 21:58:57 +00:00
|
|
|
|
2014-03-11 19:20:03 +00:00
|
|
|
from endpoints.api import (truthy_bool, format_date, nickname, log_action, validate_json_request,
|
2014-03-13 00:33:57 +00:00
|
|
|
require_repo_read, require_repo_write, require_repo_admin,
|
|
|
|
RepositoryParamResource, resource, query_param, parse_args, ApiResource,
|
2014-08-19 23:05:28 +00:00
|
|
|
request_error, require_scope, Unauthorized, NotFound, InvalidRequest,
|
|
|
|
path_param)
|
|
|
|
|
2014-03-18 23:21:27 +00:00
|
|
|
from auth.permissions import (ModifyRepositoryPermission, AdministerRepositoryPermission,
|
2015-07-15 21:25:41 +00:00
|
|
|
CreateRepositoryPermission)
|
2014-03-13 00:33:57 +00:00
|
|
|
from auth.auth_context import get_authenticated_user
|
2014-03-14 22:41:14 +00:00
|
|
|
from auth import scopes
|
2014-03-10 22:30:41 +00:00
|
|
|
|
2014-03-11 03:54:55 +00:00
|
|
|
|
2014-03-10 22:30:41 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/repository')
|
2014-03-12 20:31:37 +00:00
|
|
|
class RepositoryList(ApiResource):
|
2014-03-11 19:20:03 +00:00
|
|
|
"""Operations for creating and listing repositories."""
|
2014-03-11 03:54:55 +00:00
|
|
|
schemas = {
|
|
|
|
'NewRepo': {
|
|
|
|
'id': 'NewRepo',
|
|
|
|
'type': 'object',
|
2014-03-11 19:20:03 +00:00
|
|
|
'description': 'Description of a new repository',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
|
|
|
'repository',
|
|
|
|
'visibility',
|
2014-03-18 19:58:37 +00:00
|
|
|
'description',
|
2014-03-17 16:25:41 +00:00
|
|
|
],
|
2014-03-11 03:54:55 +00:00
|
|
|
'properties': {
|
|
|
|
'repository': {
|
|
|
|
'type': 'string',
|
2014-03-11 19:20:03 +00:00
|
|
|
'description': 'Repository name',
|
2014-03-11 03:54:55 +00:00
|
|
|
},
|
|
|
|
'visibility': {
|
|
|
|
'type': 'string',
|
2014-03-11 19:20:03 +00:00
|
|
|
'description': 'Visibility which the repository will start with',
|
2014-03-11 03:54:55 +00:00
|
|
|
'enum': [
|
|
|
|
'public',
|
|
|
|
'private',
|
2014-03-13 00:33:57 +00:00
|
|
|
],
|
2014-03-11 03:54:55 +00:00
|
|
|
},
|
|
|
|
'namespace': {
|
|
|
|
'type': 'string',
|
2014-03-11 19:20:03 +00:00
|
|
|
'description': ('Namespace in which the repository should be created. If omitted, the '
|
|
|
|
'username of the caller is used'),
|
2014-03-11 03:54:55 +00:00
|
|
|
},
|
|
|
|
'description': {
|
|
|
|
'type': 'string',
|
2014-03-11 19:20:03 +00:00
|
|
|
'description': 'Markdown encoded description for the repository',
|
2014-03-11 03:54:55 +00:00
|
|
|
},
|
2014-03-13 20:31:37 +00:00
|
|
|
},
|
|
|
|
},
|
2014-03-11 03:54:55 +00:00
|
|
|
}
|
2014-03-10 22:30:41 +00:00
|
|
|
|
2014-03-13 00:33:57 +00:00
|
|
|
@require_scope(scopes.CREATE_REPO)
|
2014-03-10 22:30:41 +00:00
|
|
|
@nickname('createRepo')
|
2014-03-11 03:54:55 +00:00
|
|
|
@validate_json_request('NewRepo')
|
2014-03-10 22:30:41 +00:00
|
|
|
def post(self):
|
2014-03-11 19:20:03 +00:00
|
|
|
"""Create a new repository."""
|
2014-03-13 00:33:57 +00:00
|
|
|
owner = get_authenticated_user()
|
2014-03-11 03:54:55 +00:00
|
|
|
req = request.get_json()
|
2014-03-18 23:21:27 +00:00
|
|
|
|
|
|
|
if owner is None and 'namespace' not in 'req':
|
|
|
|
raise InvalidRequest('Must provide a namespace or must be logged in.')
|
|
|
|
|
2014-03-11 03:54:55 +00:00
|
|
|
namespace_name = req['namespace'] if 'namespace' in req else owner.username
|
|
|
|
|
|
|
|
permission = CreateRepositoryPermission(namespace_name)
|
|
|
|
if permission.can():
|
|
|
|
repository_name = req['repository']
|
|
|
|
visibility = req['visibility']
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
existing = model.repository.get_repository(namespace_name, repository_name)
|
2014-03-11 03:54:55 +00:00
|
|
|
if existing:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(message='Repository already exists')
|
2014-03-11 03:54:55 +00:00
|
|
|
|
|
|
|
visibility = req['visibility']
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.create_repository(namespace_name, repository_name, owner, visibility)
|
2014-03-11 03:54:55 +00:00
|
|
|
repo.description = req['description']
|
|
|
|
repo.save()
|
|
|
|
|
2014-03-11 19:20:03 +00:00
|
|
|
log_action('create_repo', namespace_name, {'repo': repository_name,
|
|
|
|
'namespace': namespace_name}, repo=repo)
|
2014-03-13 00:33:57 +00:00
|
|
|
return {
|
2014-03-11 03:54:55 +00:00
|
|
|
'namespace': namespace_name,
|
|
|
|
'name': repository_name
|
2014-03-13 00:33:57 +00:00
|
|
|
}, 201
|
2014-03-11 03:54:55 +00:00
|
|
|
|
2014-03-17 20:57:35 +00:00
|
|
|
raise Unauthorized()
|
2014-03-10 22:30:41 +00:00
|
|
|
|
2014-03-19 16:09:07 +00:00
|
|
|
@require_scope(scopes.READ_REPO)
|
2014-03-10 22:30:41 +00:00
|
|
|
@nickname('listRepos')
|
2014-03-11 16:57:33 +00:00
|
|
|
@parse_args
|
2014-03-11 19:20:03 +00:00
|
|
|
@query_param('page', 'Offset page number. (int)', type=int)
|
|
|
|
@query_param('limit', 'Limit on the number of results (int)', type=int)
|
|
|
|
@query_param('namespace', 'Namespace to use when querying for org repositories.', type=str)
|
2015-05-06 23:15:03 +00:00
|
|
|
@query_param('public', 'Whether to include repositories not explicitly visible by the user.',
|
|
|
|
type=truthy_bool, default=True)
|
2014-11-19 19:50:56 +00:00
|
|
|
@query_param('private', 'Whether to include private repositories.', type=truthy_bool,
|
2014-03-11 19:20:03 +00:00
|
|
|
default=True)
|
2015-04-03 18:55:09 +00:00
|
|
|
@query_param('namespace_only', 'Whether to limit only to the given namespace.',
|
|
|
|
type=truthy_bool, default=False)
|
2015-06-09 21:58:57 +00:00
|
|
|
@query_param('last_modified', 'Whether to include when the repository was last modified.',
|
|
|
|
type=truthy_bool, default=False)
|
|
|
|
@query_param('popularity', 'Whether to include the repository\'s popularity metric.',
|
|
|
|
type=truthy_bool, default=False)
|
2014-03-11 16:57:33 +00:00
|
|
|
def get(self, args):
|
2014-03-11 19:20:03 +00:00
|
|
|
"""Fetch the list of repositories under a variety of situations."""
|
2014-03-10 22:30:41 +00:00
|
|
|
username = None
|
2015-02-24 22:50:54 +00:00
|
|
|
if get_authenticated_user():
|
2015-07-15 21:25:41 +00:00
|
|
|
starred_repos = model.repository.get_user_starred_repositories(get_authenticated_user())
|
2014-12-30 20:07:14 +00:00
|
|
|
star_lookup = set([repo.id for repo in starred_repos])
|
2014-03-10 22:30:41 +00:00
|
|
|
|
2015-02-24 22:50:54 +00:00
|
|
|
if args['private']:
|
|
|
|
username = get_authenticated_user().username
|
|
|
|
|
2014-03-10 22:30:41 +00:00
|
|
|
response = {}
|
|
|
|
|
2015-07-16 10:52:12 +00:00
|
|
|
# Find the matching repositories.
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_query = model.repository.get_visible_repositories(username,
|
|
|
|
limit=args['limit'],
|
|
|
|
page=args['page'],
|
|
|
|
include_public=args['public'],
|
|
|
|
namespace=args['namespace'],
|
|
|
|
namespace_only=args['namespace_only'])
|
2015-07-16 10:52:12 +00:00
|
|
|
|
|
|
|
# Collect the IDs of the repositories found for subequent lookup of popularity
|
|
|
|
# and/or last modified.
|
|
|
|
repository_ids = [repo.get(RepositoryTable.id) for repo in repo_query]
|
|
|
|
|
|
|
|
if args['last_modified']:
|
2015-07-15 21:25:41 +00:00
|
|
|
last_modified_map = model.repository.get_when_last_modified(repository_ids)
|
2015-07-16 10:52:12 +00:00
|
|
|
|
|
|
|
if args['popularity']:
|
2015-07-15 21:25:41 +00:00
|
|
|
action_count_map = model.repository.get_action_counts(repository_ids)
|
2015-07-16 10:52:12 +00:00
|
|
|
|
2014-12-30 20:07:14 +00:00
|
|
|
def repo_view(repo_obj):
|
|
|
|
repo = {
|
2015-06-09 21:58:57 +00:00
|
|
|
'namespace': repo_obj.get(Namespace.username),
|
|
|
|
'name': repo_obj.get(RepositoryTable.name),
|
|
|
|
'description': repo_obj.get(RepositoryTable.description),
|
|
|
|
'is_public': repo_obj.get(Visibility.name) == 'public'
|
2014-12-30 20:07:14 +00:00
|
|
|
}
|
2015-06-09 21:58:57 +00:00
|
|
|
|
2015-07-16 10:52:12 +00:00
|
|
|
repo_id = repo_obj.get(RepositoryTable.id)
|
|
|
|
|
2015-06-09 21:58:57 +00:00
|
|
|
if args['last_modified']:
|
2015-07-16 10:52:12 +00:00
|
|
|
repo['last_modified'] = last_modified_map.get(repo_id)
|
2015-06-09 21:58:57 +00:00
|
|
|
|
|
|
|
if args['popularity']:
|
2015-07-16 10:52:12 +00:00
|
|
|
repo['popularity'] = action_count_map.get(repo_id, 0)
|
2015-06-09 21:58:57 +00:00
|
|
|
|
2015-02-24 22:50:54 +00:00
|
|
|
if get_authenticated_user():
|
2015-07-16 10:52:12 +00:00
|
|
|
repo['is_starred'] = repo_id in star_lookup
|
2015-06-09 21:58:57 +00:00
|
|
|
|
2014-12-30 20:07:14 +00:00
|
|
|
return repo
|
2014-03-10 22:30:41 +00:00
|
|
|
|
2015-05-07 00:55:17 +00:00
|
|
|
response['repositories'] = [repo_view(repo) for repo in repo_query]
|
2014-03-10 22:30:41 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath:repository>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2014-03-10 22:30:41 +00:00
|
|
|
class Repository(RepositoryParamResource):
|
2014-03-11 19:20:03 +00:00
|
|
|
"""Operations for managing a specific repository."""
|
2014-03-13 00:33:57 +00:00
|
|
|
schemas = {
|
|
|
|
'RepoUpdate': {
|
|
|
|
'id': 'RepoUpdate',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Fields which can be updated in a repository.',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
|
|
|
'description',
|
|
|
|
],
|
2014-03-13 00:33:57 +00:00
|
|
|
'properties': {
|
|
|
|
'description': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'Markdown encoded description for the repository',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-10 22:30:41 +00:00
|
|
|
@require_repo_read
|
|
|
|
@nickname('getRepo')
|
|
|
|
def get(self, namespace, repository):
|
2014-03-11 19:20:03 +00:00
|
|
|
"""Fetch the specified repository."""
|
2014-03-10 22:30:41 +00:00
|
|
|
logger.debug('Get repo: %s/%s' % (namespace, repository))
|
|
|
|
|
|
|
|
def tag_view(tag):
|
2015-03-17 20:49:22 +00:00
|
|
|
tag_info = {
|
2014-03-10 22:30:41 +00:00
|
|
|
'name': tag.name,
|
2015-03-10 05:03:39 +00:00
|
|
|
'image_id': tag.image.docker_image_id,
|
|
|
|
'size': tag.image.storage.aggregate_size
|
2014-03-10 22:30:41 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 20:49:22 +00:00
|
|
|
if tag.lifetime_start_ts > 0:
|
2015-07-15 21:25:41 +00:00
|
|
|
last_modified = format_date(datetime.datetime.fromtimestamp(tag.lifetime_start_ts))
|
|
|
|
tag_info['last_modified'] = last_modified
|
2015-03-17 20:49:22 +00:00
|
|
|
|
|
|
|
return tag_info
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2014-03-10 22:30:41 +00:00
|
|
|
if repo:
|
2015-07-15 21:25:41 +00:00
|
|
|
tags = model.tag.list_repository_tags(namespace, repository, include_storage=True)
|
2014-03-10 22:30:41 +00:00
|
|
|
tag_dict = {tag.name: tag_view(tag) for tag in tags}
|
|
|
|
can_write = ModifyRepositoryPermission(namespace, repository).can()
|
|
|
|
can_admin = AdministerRepositoryPermission(namespace, repository).can()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
is_starred = (model.repository.repository_is_starred(get_authenticated_user(), repo)
|
2015-03-10 05:03:39 +00:00
|
|
|
if get_authenticated_user() else False)
|
2015-07-15 21:25:41 +00:00
|
|
|
is_public = model.repository.is_repository_public(repo)
|
2015-03-10 05:03:39 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
(pull_today, pull_thirty_day) = model.log.get_repository_pulls(repo, timedelta(days=1),
|
|
|
|
timedelta(days=30))
|
2015-05-08 02:49:11 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
(push_today, push_thirty_day) = model.log.get_repository_pushes(repo, timedelta(days=1),
|
|
|
|
timedelta(days=30))
|
2015-05-08 02:49:11 +00:00
|
|
|
|
2014-03-10 22:30:41 +00:00
|
|
|
return {
|
|
|
|
'namespace': namespace,
|
|
|
|
'name': repository,
|
|
|
|
'description': repo.description,
|
|
|
|
'tags': tag_dict,
|
|
|
|
'can_write': can_write,
|
|
|
|
'can_admin': can_admin,
|
|
|
|
'is_public': is_public,
|
2015-03-18 18:47:53 +00:00
|
|
|
'is_organization': repo.namespace_user.organization,
|
2015-03-10 05:03:39 +00:00
|
|
|
'is_starred': is_starred,
|
2015-03-11 00:22:46 +00:00
|
|
|
'status_token': repo.badge_token if not is_public else '',
|
|
|
|
'stats': {
|
|
|
|
'pulls': {
|
2015-05-08 02:49:11 +00:00
|
|
|
'today': pull_today,
|
|
|
|
'thirty_day': pull_thirty_day
|
2015-03-11 00:22:46 +00:00
|
|
|
},
|
|
|
|
'pushes': {
|
2015-05-08 02:49:11 +00:00
|
|
|
'today': push_today,
|
|
|
|
'thirty_day': push_thirty_day
|
2015-03-11 00:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-10 22:30:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-13 00:33:57 +00:00
|
|
|
@require_repo_write
|
|
|
|
@nickname('updateRepo')
|
|
|
|
@validate_json_request('RepoUpdate')
|
|
|
|
def put(self, namespace, repository):
|
|
|
|
""" Update the description in the specified repository. """
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2014-03-13 00:33:57 +00:00
|
|
|
if repo:
|
|
|
|
values = request.get_json()
|
|
|
|
repo.description = values['description']
|
|
|
|
repo.save()
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-13 00:33:57 +00:00
|
|
|
log_action('set_repo_description', namespace,
|
|
|
|
{'repo': repository, 'description': values['description']},
|
|
|
|
repo=repo)
|
|
|
|
return {
|
|
|
|
'success': True
|
|
|
|
}
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-13 00:33:57 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('deleteRepository')
|
|
|
|
def delete(self, namespace, repository):
|
2014-03-13 19:19:49 +00:00
|
|
|
""" Delete a repository. """
|
2015-07-15 21:25:41 +00:00
|
|
|
model.repository.purge_repository(namespace, repository)
|
2014-03-13 00:33:57 +00:00
|
|
|
log_action('delete_repo', namespace,
|
|
|
|
{'repo': repository, 'namespace': namespace})
|
|
|
|
return 'Deleted', 204
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath:repository>/changevisibility')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2014-03-13 00:33:57 +00:00
|
|
|
class RepositoryVisibility(RepositoryParamResource):
|
|
|
|
""" Custom verb for changing the visibility of the repository. """
|
|
|
|
schemas = {
|
|
|
|
'ChangeVisibility': {
|
|
|
|
'id': 'ChangeVisibility',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Change the visibility for the repository.',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
|
|
|
'visibility',
|
|
|
|
],
|
2014-03-13 00:33:57 +00:00
|
|
|
'properties': {
|
|
|
|
'visibility': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'Visibility which the repository will start with',
|
|
|
|
'enum': [
|
|
|
|
'public',
|
|
|
|
'private',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('changeRepoVisibility')
|
|
|
|
@validate_json_request('ChangeVisibility')
|
|
|
|
def post(self, namespace, repository):
|
|
|
|
""" Change the visibility of a repository. """
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2014-03-13 00:33:57 +00:00
|
|
|
if repo:
|
|
|
|
values = request.get_json()
|
2015-07-15 21:25:41 +00:00
|
|
|
model.repository.set_repository_visibility(repo, values['visibility'])
|
2014-03-13 00:33:57 +00:00
|
|
|
log_action('change_repo_visibility', namespace,
|
|
|
|
{'repo': repository, 'visibility': values['visibility']},
|
|
|
|
repo=repo)
|
2014-11-19 19:50:56 +00:00
|
|
|
return {'success': True}
|