@@ 67,15 67,16 @@ Usage:
```
$ root:code ~ # repoaccess.py -h
-usage: repoaccess.py [-h] repo_type repository username perms
+usage: repoaccess.py [-h] repo_type repository user_perm [user_perm ...]
Give access to hg or git repositories.
positional arguments:
repo_type Repository type (hg or git)
repository Repository name (~username/repo_name)
- username Username of user you're giving access to.
- perms Permissions being granted (ro or rw)
+ user_perm Username and permission of the user you're giving access to. You
+ can specify multiple users/perms at once. Permissions are: ro
+ (read-only), rw (read-write) i.e., username1:rw username2:ro
optional arguments:
-h, --help show this help message and exit
@@ 2,7 2,7 @@
"""
Simple script to give repo access to a specific user.
- $ python3 repoaccess.py <repo type> <~username/repo_name> <username_to_give_access_to> <permission>
+ $ python3 repoaccess.py <repo type> <~username/repo_name> <username:permission> [<username:permission> ...]
repo types are "hg" and "git"
@@ 10,6 10,8 @@
This needs to be run on the same system that the particular service
(repo_type) is being served from.
+
+ You can give multiple user:perm's
"""
import os
import re
@@ 26,10 28,11 @@ class RepoHelper:
access_model = None
repo_model = None
- def __init__(self, repo_type):
+ def __init__(self, repo_type, verbose=True):
_db = DbSession(cfg(f'{repo_type}.sr.ht', 'connection-string'))
_db.init()
getattr(self, f'set_models_{repo_type}')()
+ self.verbose = verbose
def set_models_hg(self):
from hgsrht.types import User, Access, Repository
@@ 58,20 61,9 @@ class RepoHelper:
name=repo_name,
).first()
- def grant_access(self, repo_full, to_user, perm):
- """ repo_full should be in format: ~username/repo_name
- to_user is the user to give access to
- """
- owner, repo = repo_full.split('/')
- repo = self.get_repo(owner, repo)
- if not repo:
- sys.stderr.write(f'Repository {repo_full} not found.\n')
- return 1
- user = self.get_user(to_user)
- if not user:
- sys.stderr.write(f'User {to_user} not found.\n')
- return 1
-
+ def grant_access(self, repo, user, perm):
+ to_user = user.username
+ repo_full = '/'.join(repo.path.split('/')[-2:])
# Check for existing access
access = self.access_model.query.filter_by(
repo_id=repo.id,
@@ 80,11 72,16 @@ class RepoHelper:
if access:
# scmsrht.repos.access.AccessMode - Enum
if access.mode.value == perm:
- print(f'{to_user} already has {perm} access to {repo_full}.')
- return 0
+ if self.verbose:
+ print(
+ f'{to_user} already has {perm} access to {repo_full}.'
+ )
else:
access.mode = getattr(AccessMode, perm)
- print(f'Updated {repo_full} access for {to_user} to {perm}.')
+ if self.verbose:
+ print(
+ f'Updated {repo_full} access for {to_user} to {perm}.'
+ )
else:
# Grant access
grant = self.access_model()
@@ 92,10 89,29 @@ class RepoHelper:
grant.user_id = user.id
grant.mode = getattr(AccessMode, perm)
db.session.add(grant)
- print(f'Granted {perm} access to user {to_user} for {repo_full}.')
+ if self.verbose:
+ print(
+ f'Granted {perm} access to user {to_user} for {repo_full}.'
+ )
+
- db.session.commit()
- return 0
+def process_users(repo_helper, repo_full, users):
+ owner, repo = repo_full.split('/')
+ repo = repo_helper.get_repo(owner, repo)
+ if not repo:
+ sys.stderr.write(f'Repository {repo_full} not found.\n')
+ return 1
+
+ for _user in users:
+ to_user, perm = _user.split(':')
+ user = repo_helper.get_user(to_user)
+ if not user:
+ sys.stderr.write(f'User {to_user} not found.\n')
+ return 1
+ repo_helper.grant_access(repo, user, perm)
+
+ db.session.commit()
+ return 0
def main():
@@ 109,8 125,8 @@ def main():
raise ValueError
return value
- def _perms(value):
- if value not in ['ro', 'rw']:
+ def _user_perm(value):
+ if not re.search(r'^[-\w]+:ro|rw$', value):
raise ValueError
return value
@@ 128,19 144,20 @@ def main():
help='Repository name (~username/repo_name)',
)
parser.add_argument(
- 'username',
- type=str,
- help='Username of user you\'re giving access to.',
- )
- parser.add_argument(
- 'perms',
- type=_perms,
- help='Permissions being granted (ro or rw)',
+ 'user_perm',
+ help=(
+ 'Username and permission of the user you\'re giving access to. '
+ 'You can specify multiple users/perms at once. '
+ 'Permissions are: ro (read-only), rw (read-write) '
+ 'i.e., username1:rw username2:ro'
+ ),
+ nargs='+',
+ type=_user_perm,
)
args = parser.parse_args()
repo = RepoHelper(args.repo_type)
- sys.exit(repo.grant_access(args.repository, args.username, args.perms))
+ sys.exit(process_users(repo, args.repository, args.user_perm))
if __name__ == '__main__':