##// END OF EJS Templates
sync alternative gravatar options to other .ini files...
sync alternative gravatar options to other .ini files - changelog update

File last commit:

r2697:4565e655 beta
r2736:3aad896d beta
Show More
api.py
784 lines | 24.6 KiB | text/x-python | PythonLexer
2012 copyrights
r1824 # -*- coding: utf-8 -*-
"""
rhodecode.controllers.api
~~~~~~~~~~~~~~~~~~~~~~~~~
API controller for RhodeCode
:created_on: Aug 20, 2011
:author: marcink
:copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
:license: GPLv3, see COPYING for more details.
"""
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License or (at your opinion) any later version of the license.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
Extended API...
r1500 import traceback
import logging
Beginning of API implementation for rhodecode
r1445 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
fixes #288...
r1594 from rhodecode.lib.auth import HasPermissionAllDecorator, \
#404 API extensions for showing permission for users...
r2151 HasPermissionAnyDecorator, PasswordGenerator, AuthUser
API: Added option to rescann repositories via api call
r2697 from rhodecode.lib.utils import map_groups, repo2db_mapper
another major refactoring with session management
r1734 from rhodecode.model.meta import Session
Beginning of API implementation for rhodecode
r1445 from rhodecode.model.scm import ScmModel
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 from rhodecode.model.repo import RepoModel
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 from rhodecode.model.user import UserModel
from rhodecode.model.users_group import UsersGroupModel
Refactored API...
r2526 from rhodecode.model.permission import PermissionModel
another major refactoring with session management
r1734
Nicolas VINOT
Correct code style
r1593 log = logging.getLogger(__name__)
Extended API...
r1500
Beginning of API implementation for rhodecode
r1445
Refactored API...
r2526 class Optional(object):
"""
Defines an optional parameter::
param = param.getval() if isinstance(param, Optional) else param
param = param() if isinstance(param, Optional) else param
is equivalent of::
param = Optional.extract(param)
"""
def __init__(self, type_):
self.type_ = type_
def __repr__(self):
return '<Optional:%s>' % self.type_.__repr__()
def __call__(self):
return self.getval()
def getval(self):
"""
returns value from this Optional instance
"""
return self.type_
@classmethod
def extract(cls, val):
if isinstance(val, cls):
return val.getval()
return val
def get_user_or_error(userid):
"""
Get user by id or name or return JsonRPCError if not found
:param userid:
"""
user = UserModel().get_user(userid)
if user is None:
raise JSONRPCError("user `%s` does not exist" % userid)
return user
def get_repo_or_error(repoid):
"""
Get repo by id or name or return JsonRPCError if not found
:param userid:
"""
repo = RepoModel().get_repo(repoid)
if repo is None:
raise JSONRPCError('repository `%s` does not exist' % (repoid))
return repo
def get_users_group_or_error(usersgroupid):
"""
Get users group by id or name or return JsonRPCError if not found
:param userid:
"""
users_group = UsersGroupModel().get_group(usersgroupid)
if users_group is None:
raise JSONRPCError('users group `%s` does not exist' % usersgroupid)
return users_group
def get_perm_or_error(permid):
"""
Get permission by id or name or return JsonRPCError if not found
:param userid:
"""
perm = PermissionModel().get_permission_by_name(permid)
if perm is None:
raise JSONRPCError('permission `%s` does not exist' % (permid))
return perm
Nicolas VINOT
Correct code style
r1593 class ApiController(JSONRPCController):
Beginning of API implementation for rhodecode
r1445 """
API Controller
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Beginning of API implementation for rhodecode
r1445 Each method needs to have USER as argument this is then based on given
API_KEY propagated as instance of user object
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Beginning of API implementation for rhodecode
r1445 Preferably this should be first argument also
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Each function should also **raise** JSONRPCError for any
Beginning of API implementation for rhodecode
r1445 errors that happens
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Beginning of API implementation for rhodecode
r1445 """
Nicolas VINOT
Correct code style
r1593 @HasPermissionAllDecorator('hg.admin')
Refactored API...
r2526 def pull(self, apiuser, repoid):
Beginning of API implementation for rhodecode
r1445 """
Dispatch pull action on given repo
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Refactored API...
r2526 :param apiuser:
:param repoid:
Beginning of API implementation for rhodecode
r1445 """
Refactored API...
r2526 repo = get_repo_or_error(repoid)
Nicolas VINOT
Implement all CRUD API operation for repo
r1587
Beginning of API implementation for rhodecode
r1445 try:
Refactored API...
r2526 ScmModel().pull_changes(repo.repo_name,
self.rhodecode_user.username)
return 'Pulled from `%s`' % repo.repo_name
Beginning of API implementation for rhodecode
r1445 except Exception:
Refactored API...
r2526 log.error(traceback.format_exc())
raise JSONRPCError(
'Unable to pull changes from `%s`' % repo.repo_name
)
Beginning of API implementation for rhodecode
r1445
Nicolas VINOT
Correct code style
r1593 @HasPermissionAllDecorator('hg.admin')
API: Added option to rescann repositories via api call
r2697 def rescan_repos(self, apiuser, remove_obsolete=Optional(False)):
"""
Dispatch rescan repositories action. If remove_obsolete is set
than also delete repos that are in database but not in the filesystem.
aka "clean zombies"
:param apiuser:
:param remove_obsolete:
"""
try:
rm_obsolete = Optional.extract(remove_obsolete)
added, removed = repo2db_mapper(ScmModel().repo_scan(),
remove_obsolete=rm_obsolete)
return {'added': added, 'removed': removed}
except Exception:
log.error(traceback.format_exc())
raise JSONRPCError(
'Unable to rescan repositories'
)
@HasPermissionAllDecorator('hg.admin')
API get_user and get_repo methods can fetch by id or names
r2010 def get_user(self, apiuser, userid):
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 """"
Get a user by username
implements #329...
r1793 :param apiuser:
Refactored API...
r2526 :param userid:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 """
Beginning of API implementation for rhodecode
r1445
Refactored API...
r2526 user = get_user_or_error(userid)
data = user.get_api_data()
data['permissions'] = AuthUser(user_id=user.user_id).permissions
return data
implements #329...
r1793
@HasPermissionAllDecorator('hg.admin')
def get_users(self, apiuser):
""""
Get all users
:param apiuser:
"""
result = []
Refactored API...
r2526 for user in UserModel().get_all():
result.append(user.get_api_data())
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 return result
Nicolas VINOT
Correct code style
r1593 @HasPermissionAllDecorator('hg.admin')
Refactored API...
r2526 def create_user(self, apiuser, username, email, password,
firstname=Optional(None), lastname=Optional(None),
active=Optional(True), admin=Optional(False),
ldap_dn=Optional(None)):
Extended API...
r1500 """
API added explicit method for updating user account
r2002 Create new user
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Extended API...
r1500 :param apiuser:
:param username:
Refactored API...
r2526 :param email:
Extended API...
r1500 :param password:
Refactored API...
r2526 :param firstname:
Extended API...
r1500 :param lastname:
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 :param active:
:param admin:
:param ldap_dn:
Extended API...
r1500 """
Nicolas VINOT
[API] Create groups needed when creating repo
r1589
Refactored API...
r2526 if UserModel().get_by_username(username):
raise JSONRPCError("user `%s` already exist" % username)
if UserModel().get_by_email(email, case_insensitive=True):
raise JSONRPCError("email `%s` already exist" % email)
API: allowed password field to be null when used with ldap_dn ref #362...
r2008
if ldap_dn:
# generate temporary password if ldap_dn
password = PasswordGenerator().gen_password(length=8)
Extended API...
r1500 try:
API, added delete_user method....
r2365 user = UserModel().create_or_update(
Refactored API...
r2526 username=Optional.extract(username),
password=Optional.extract(password),
email=Optional.extract(email),
firstname=Optional.extract(firstname),
lastname=Optional.extract(lastname),
active=Optional.extract(active),
admin=Optional.extract(admin),
ldap_dn=Optional.extract(ldap_dn)
api review...
r1843 )
Refactored API...
r2526 Session().commit()
api review...
r1843 return dict(
Refactored API...
r2526 msg='created new user `%s`' % username,
user=user.get_api_data()
api review...
r1843 )
Extended API...
r1500 except Exception:
Nicolas VINOT
Correct code style
r1593 log.error(traceback.format_exc())
Refactored API...
r2526 raise JSONRPCError('failed to create user `%s`' % username)
Nicolas VINOT
Implement all CRUD API operation for repo
r1587
Nicolas VINOT
Correct code style
r1593 @HasPermissionAllDecorator('hg.admin')
Refactored API...
r2526 def update_user(self, apiuser, userid, username=Optional(None),
email=Optional(None), firstname=Optional(None),
lastname=Optional(None), active=Optional(None),
admin=Optional(None), ldap_dn=Optional(None),
password=Optional(None)):
API added explicit method for updating user account
r2002 """
Updates given user
:param apiuser:
Refactored API...
r2526 :param userid:
API added explicit method for updating user account
r2002 :param username:
:param email:
Refactored API...
r2526 :param firstname:
API added explicit method for updating user account
r2002 :param lastname:
:param active:
:param admin:
:param ldap_dn:
Refactored API...
r2526 :param password:
API added explicit method for updating user account
r2002 """
Refactored API...
r2526
user = get_user_or_error(userid)
fixed api issue with changing username during update_user
r2657 # call function and store only updated arguments
updates = {}
def store_update(attr, name):
if not isinstance(attr, Optional):
updates[name] = attr
API added explicit method for updating user account
r2002
try:
Refactored API...
r2526
fixed api issue with changing username during update_user
r2657 store_update(username, 'username')
store_update(password, 'password')
store_update(email, 'email')
store_update(firstname, 'name')
store_update(lastname, 'lastname')
store_update(active, 'active')
store_update(admin, 'admin')
store_update(ldap_dn, 'ldap_dn')
user = UserModel().update_user(user, **updates)
Refactored API...
r2526 Session().commit()
API added explicit method for updating user account
r2002 return dict(
API: update_user returns new updated user data
r2507 msg='updated user ID:%s %s' % (user.user_id, user.username),
Refactored API...
r2526 user=user.get_api_data()
API added explicit method for updating user account
r2002 )
except Exception:
log.error(traceback.format_exc())
Refactored API...
r2526 raise JSONRPCError('failed to update user `%s`' % userid)
API, added delete_user method....
r2365
@HasPermissionAllDecorator('hg.admin')
def delete_user(self, apiuser, userid):
""""
Deletes an user
:param apiuser:
Refactored API...
r2526 :param userid:
API, added delete_user method....
r2365 """
Refactored API...
r2526 user = get_user_or_error(userid)
API, added delete_user method....
r2365
try:
UserModel().delete(userid)
Refactored API...
r2526 Session().commit()
API, added delete_user method....
r2365 return dict(
Refactored API...
r2526 msg='deleted user ID:%s %s' % (user.user_id, user.username),
user=None
API, added delete_user method....
r2365 )
except Exception:
log.error(traceback.format_exc())
Refactored API...
r2526 raise JSONRPCError('failed to delete ID:%s %s' % (user.user_id,
user.username))
API added explicit method for updating user account
r2002
@HasPermissionAllDecorator('hg.admin')
Refactored API...
r2526 def get_users_group(self, apiuser, usersgroupid):
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 """"
Refactored API...
r2526 Get users group by name or id
Nicolas VINOT
Implement all CRUD API operation for repo
r1587
implements #329...
r1793 :param apiuser:
Refactored API...
r2526 :param usersgroupid:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 """
Refactored API...
r2526 users_group = get_users_group_or_error(usersgroupid)
Nicolas VINOT
Implement all CRUD API operation for repo
r1587
Refactored API...
r2526 data = users_group.get_api_data()
Beginning of API implementation for rhodecode
r1445
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 members = []
for user in users_group.members:
user = user.user
Refactored API...
r2526 members.append(user.get_api_data())
data['members'] = members
return data
Nicolas VINOT
Implement all CRUD API operation for repo
r1587
Nicolas VINOT
Correct code style
r1593 @HasPermissionAllDecorator('hg.admin')
def get_users_groups(self, apiuser):
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 """"
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 Get all users groups
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
implements #329...
r1793 :param apiuser:
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 """
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 result = []
Refactored API...
r2526 for users_group in UsersGroupModel().get_all():
result.append(users_group.get_api_data())
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 return result
Nicolas VINOT
Correct code style
r1593 @HasPermissionAllDecorator('hg.admin')
Refactored API...
r2526 def create_users_group(self, apiuser, group_name, active=Optional(True)):
Extended API...
r1500 """
Creates an new usergroup
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Refactored API...
r2526 :param apiuser:
api review...
r1843 :param group_name:
Extended API...
r1500 :param active:
"""
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
Refactored API...
r2526 if UsersGroupModel().get_by_name(group_name):
raise JSONRPCError("users group `%s` already exist" % group_name)
Nicolas VINOT
[API] Create groups needed when creating repo
r1589
Extended API...
r1500 try:
Refactored API...
r2526 active = Optional.extract(active)
api review...
r1843 ug = UsersGroupModel().create(name=group_name, active=active)
Refactored API...
r2526 Session().commit()
return dict(
msg='created new users group `%s`' % group_name,
users_group=ug.get_api_data()
)
Extended API...
r1500 except Exception:
Nicolas VINOT
Correct code style
r1593 log.error(traceback.format_exc())
Refactored API...
r2526 raise JSONRPCError('failed to create group `%s`' % group_name)
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Correct code style
r1593 @HasPermissionAllDecorator('hg.admin')
Refactored API...
r2526 def add_user_to_users_group(self, apiuser, usersgroupid, userid):
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 """"
Api docs fixes...
r2146 Add a user to a users group
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
implements #329...
r1793 :param apiuser:
Refactored API...
r2526 :param usersgroupid:
:param userid:
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 """
Refactored API...
r2526 user = get_user_or_error(userid)
users_group = get_users_group_or_error(usersgroupid)
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 try:
Nicolas VINOT
Correct code style
r1593 ugm = UsersGroupModel().add_user_to_group(users_group, user)
API changes...
r1989 success = True if ugm != True else False
Refactored API...
r2526 msg = 'added member `%s` to users group `%s`' % (
user.username, users_group.users_group_name
)
API changes...
r1989 msg = msg if success else 'User is already in that group'
Refactored API...
r2526 Session().commit()
API changes...
r1989
return dict(
success=success,
msg=msg
)
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 except Exception:
Nicolas VINOT
Correct code style
r1593 log.error(traceback.format_exc())
Refactored API...
r2526 raise JSONRPCError(
'failed to add member to users group `%s`' % (
users_group.users_group_name
)
)
API changes...
r1989
@HasPermissionAllDecorator('hg.admin')
Refactored API...
r2526 def remove_user_from_users_group(self, apiuser, usersgroupid, userid):
API changes...
r1989 """
Remove user from a group
Refactored API...
r2526 :param apiuser:
:param usersgroupid:
:param userid:
API changes...
r1989 """
Refactored API...
r2526 user = get_user_or_error(userid)
users_group = get_users_group_or_error(usersgroupid)
API changes...
r1989
try:
Refactored API...
r2526 success = UsersGroupModel().remove_user_from_group(users_group,
user)
msg = 'removed member `%s` from users group `%s`' % (
user.username, users_group.users_group_name
)
API changes...
r1989 msg = msg if success else "User wasn't in group"
Refactored API...
r2526 Session().commit()
API changes...
r1989 return dict(success=success, msg=msg)
except Exception:
log.error(traceback.format_exc())
Refactored API...
r2526 raise JSONRPCError(
'failed to remove member from users group `%s`' % (
users_group.users_group_name
)
)
Nicolas VINOT
Implement all CRUD API operation for repo
r1587
Nicolas VINOT
Correct code style
r1593 @HasPermissionAnyDecorator('hg.admin')
API get_user and get_repo methods can fetch by id or names
r2010 def get_repo(self, apiuser, repoid):
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 """"
Get repository by name
implements #329...
r1793 :param apiuser:
Refactored API...
r2526 :param repoid:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 """
Refactored API...
r2526 repo = get_repo_or_error(repoid)
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 members = []
for user in repo.repo_to_perm:
perm = user.permission.permission_name
user = user.user
Refactored API...
r2526 user_data = user.get_api_data()
user_data['type'] = "user"
user_data['permission'] = perm
members.append(user_data)
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 for users_group in repo.users_group_to_perm:
perm = users_group.permission.permission_name
users_group = users_group.users_group
Refactored API...
r2526 users_group_data = users_group.get_api_data()
users_group_data['type'] = "users_group"
users_group_data['permission'] = perm
members.append(users_group_data)
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Refactored API...
r2526 data = repo.get_api_data()
data['members'] = members
return data
Nicolas VINOT
Implement all CRUD API operation for repo
r1587
Nicolas VINOT
Correct code style
r1593 @HasPermissionAnyDecorator('hg.admin')
def get_repos(self, apiuser):
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 """"
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 Get all repositories
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
implements #329...
r1793 :param apiuser:
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 """
Nicolas VINOT
Implement all CRUD API operation for repo
r1587
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 result = []
Refactored API...
r2526 for repo in RepoModel().get_all():
result.append(repo.get_api_data())
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 return result
implements #330 api method for listing nodes at particular revision...
r1810 @HasPermissionAnyDecorator('hg.admin')
Refactored API...
r2526 def get_repo_nodes(self, apiuser, repoid, revision, root_path,
implements #330 api method for listing nodes at particular revision...
r1810 ret_type='all'):
"""
returns a list of nodes and it's children
for a given path at given revision. It's possible to specify ret_type
to show only files or dirs
:param apiuser:
Refactored API...
r2526 :param repoid: name or id of repository
implements #330 api method for listing nodes at particular revision...
r1810 :param revision: revision for which listing should be done
:param root_path: path from which start displaying
:param ret_type: return type 'all|files|dirs' nodes
"""
Refactored API...
r2526 repo = get_repo_or_error(repoid)
implements #330 api method for listing nodes at particular revision...
r1810 try:
Refactored API...
r2526 _d, _f = ScmModel().get_nodes(repo, revision, root_path,
implements #330 api method for listing nodes at particular revision...
r1810 flat=False)
_map = {
'all': _d + _f,
'files': _f,
'dirs': _d,
}
return _map[ret_type]
except KeyError:
raise JSONRPCError('ret_type must be one of %s' % _map.keys())
Refactored API...
r2526 except Exception:
log.error(traceback.format_exc())
raise JSONRPCError(
'failed to get repo: `%s` nodes' % repo.repo_name
)
implements #330 api method for listing nodes at particular revision...
r1810
Nicolas VINOT
Correct code style
r1593 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
Refactored API...
r2526 def create_repo(self, apiuser, repo_name, owner, repo_type,
description=Optional(''), private=Optional(False),
clone_uri=Optional(None), landing_rev=Optional('tip')):
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 """
added clone_uri to API method for creating users
r2006 Create repository, if clone_url is given it makes a remote clone
Refactored API...
r2526 if repo_name is withina group name the groups will be created
automatically if they aren't present
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
implements #329...
r1793 :param apiuser:
api review...
r1843 :param repo_name:
Refactored API...
r2526 :param onwer:
:param repo_type:
implements #329...
r1793 :param description:
:param private:
added clone_uri to API method for creating users
r2006 :param clone_uri:
Refactored API...
r2526 :param landing_rev:
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 """
Refactored API...
r2526 owner = get_user_or_error(owner)
if RepoModel().get_by_repo_name(repo_name):
raise JSONRPCError("repo `%s` already exist" % repo_name)
private = Optional.extract(private)
clone_uri = Optional.extract(clone_uri)
description = Optional.extract(description)
landing_rev = Optional.extract(landing_rev)
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 try:
Refactored API...
r2526 # create structure of groups and return the last group
Resolve error occurring during recursive group creation in API create-repo function
r2120 group = map_groups(repo_name)
Nicolas VINOT
[API] Create groups needed when creating repo
r1589
Refactored API...
r2526 repo = RepoModel().create_repo(
repo_name=repo_name,
repo_type=repo_type,
description=description,
owner=owner,
private=private,
clone_uri=clone_uri,
repos_group=group,
landing_rev=landing_rev,
implements #329...
r1793 )
Refactored API...
r2526
Session().commit()
api review...
r1843
return dict(
Refactored API...
r2526 msg="Created new repository `%s`" % (repo.repo_name),
repo=repo.get_api_data()
api review...
r1843 )
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 except Exception:
Nicolas VINOT
Correct code style
r1593 log.error(traceback.format_exc())
Refactored API...
r2526 raise JSONRPCError('failed to create repository `%s`' % repo_name)
Implemented #354 added forking option to API
r2653 @HasPermissionAnyDecorator('hg.admin')
def fork_repo(self, apiuser, repoid, fork_name, owner,
description=Optional(''), copy_permissions=Optional(False),
private=Optional(False), landing_rev=Optional('tip')):
repo = get_repo_or_error(repoid)
repo_name = repo.repo_name
owner = get_user_or_error(owner)
_repo = RepoModel().get_by_repo_name(fork_name)
if _repo:
type_ = 'fork' if _repo.fork else 'repo'
raise JSONRPCError("%s `%s` already exist" % (type_, fork_name))
try:
# create structure of groups and return the last group
group = map_groups(fork_name)
form_data = dict(
repo_name=fork_name,
repo_name_full=fork_name,
repo_group=group,
repo_type=repo.repo_type,
description=Optional.extract(description),
private=Optional.extract(private),
copy_permissions=Optional.extract(copy_permissions),
landing_rev=Optional.extract(landing_rev),
update_after_clone=False,
fork_parent_id=repo.repo_id,
)
RepoModel().create_fork(form_data, cur_user=owner)
return dict(
msg='Created fork of `%s` as `%s`' % (repo.repo_name,
fork_name),
success=True # cannot return the repo data here since fork
# cann be done async
)
except Exception:
log.error(traceback.format_exc())
raise JSONRPCError(
'failed to fork repository `%s` as `%s`' % (repo_name,
fork_name)
)
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Correct code style
r1593 @HasPermissionAnyDecorator('hg.admin')
Refactored API...
r2526 def delete_repo(self, apiuser, repoid):
implements #361 API method for deleting repositories
r2003 """
Deletes a given repository
Refactored API...
r2526 :param apiuser:
:param repoid:
implements #361 API method for deleting repositories
r2003 """
Refactored API...
r2526 repo = get_repo_or_error(repoid)
implements #361 API method for deleting repositories
r2003 try:
Refactored API...
r2526 RepoModel().delete(repo)
Session().commit()
implements #361 API method for deleting repositories
r2003 return dict(
Refactored API...
r2526 msg='Deleted repository `%s`' % repo.repo_name,
success=True
implements #361 API method for deleting repositories
r2003 )
except Exception:
log.error(traceback.format_exc())
Refactored API...
r2526 raise JSONRPCError(
'failed to delete repository `%s`' % repo.repo_name
)
implements #361 API method for deleting repositories
r2003
@HasPermissionAnyDecorator('hg.admin')
Refactored API...
r2526 def grant_user_permission(self, apiuser, repoid, userid, perm):
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 """
#227 Initial version of repository groups permissions system...
r1982 Grant permission for user on given repository, or update existing one
if found
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Refactored API...
r2526 :param repoid:
:param userid:
implements #329...
r1793 :param perm:
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 """
Refactored API...
r2526 repo = get_repo_or_error(repoid)
user = get_user_or_error(userid)
perm = get_perm_or_error(perm)
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
try:
#227 Initial version of repository groups permissions system...
r1982 RepoModel().grant_user_permission(repo=repo, user=user, perm=perm)
Refactored API...
r2526 Session().commit()
implements #329...
r1793 return dict(
Refactored API...
r2526 msg='Granted perm: `%s` for user: `%s` in repo: `%s`' % (
perm.permission_name, user.username, repo.repo_name
),
success=True
implements #329...
r1793 )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 except Exception:
Nicolas VINOT
Correct code style
r1593 log.error(traceback.format_exc())
implements #329...
r1793 raise JSONRPCError(
Refactored API...
r2526 'failed to edit permission for user: `%s` in repo: `%s`' % (
userid, repoid
implements #329...
r1793 )
)
@HasPermissionAnyDecorator('hg.admin')
Refactored API...
r2526 def revoke_user_permission(self, apiuser, repoid, userid):
#227 Initial version of repository groups permissions system...
r1982 """
Revoke permission for user on given repository
Refactored API...
r2526 :param apiuser:
:param repoid:
:param userid:
implements #329...
r1793 """
#227 Initial version of repository groups permissions system...
r1982
Refactored API...
r2526 repo = get_repo_or_error(repoid)
user = get_user_or_error(userid)
#227 Initial version of repository groups permissions system...
r1982 try:
Refactored API...
r2526
RepoModel().revoke_user_permission(repo=repo, user=user)
#227 Initial version of repository groups permissions system...
r1982
Refactored API...
r2526 Session().commit()
#227 Initial version of repository groups permissions system...
r1982 return dict(
Refactored API...
r2526 msg='Revoked perm for user: `%s` in repo: `%s`' % (
user.username, repo.repo_name
),
success=True
#227 Initial version of repository groups permissions system...
r1982 )
except Exception:
log.error(traceback.format_exc())
raise JSONRPCError(
Refactored API...
r2526 'failed to edit permission for user: `%s` in repo: `%s`' % (
userid, repoid
#227 Initial version of repository groups permissions system...
r1982 )
)
@HasPermissionAnyDecorator('hg.admin')
Refactored API...
r2526 def grant_users_group_permission(self, apiuser, repoid, usersgroupid,
perm):
#227 Initial version of repository groups permissions system...
r1982 """
Grant permission for users group on given repository, or update
existing one if found
Refactored API...
r2526 :param apiuser:
:param repoid:
:param usersgroupid:
implements #329...
r1793 :param perm:
"""
Refactored API...
r2526 repo = get_repo_or_error(repoid)
perm = get_perm_or_error(perm)
users_group = get_users_group_or_error(usersgroupid)
implements #329...
r1793
try:
Refactored API...
r2526 RepoModel().grant_users_group_permission(repo=repo,
group_name=users_group,
#227 Initial version of repository groups permissions system...
r1982 perm=perm)
Refactored API...
r2526 Session().commit()
implements #329...
r1793 return dict(
Refactored API...
r2526 msg='Granted perm: `%s` for users group: `%s` in '
'repo: `%s`' % (
perm.permission_name, users_group.users_group_name,
repo.repo_name
),
success=True
)
except Exception:
print traceback.format_exc()
log.error(traceback.format_exc())
raise JSONRPCError(
'failed to edit permission for users group: `%s` in '
'repo: `%s`' % (
usersgroupid, repo.repo_name
implements #329...
r1793 )
)
Refactored API...
r2526
@HasPermissionAnyDecorator('hg.admin')
def revoke_users_group_permission(self, apiuser, repoid, usersgroupid):
"""
Revoke permission for users group on given repository
:param apiuser:
:param repoid:
:param usersgroupid:
"""
repo = get_repo_or_error(repoid)
users_group = get_users_group_or_error(usersgroupid)
try:
RepoModel().revoke_users_group_permission(repo=repo,
group_name=users_group)
Session().commit()
return dict(
msg='Revoked perm for users group: `%s` in repo: `%s`' % (
users_group.users_group_name, repo.repo_name
),
success=True
)
implements #329...
r1793 except Exception:
log.error(traceback.format_exc())
raise JSONRPCError(
Refactored API...
r2526 'failed to edit permission for users group: `%s` in '
'repo: `%s`' % (
users_group.users_group_name, repo.repo_name
implements #329...
r1793 )
)