Changeset - abc29122c7f2
[Not reviewed]
stable
0 6 0
Mads Kiilerich (mads) - 3 years ago 2022-12-10 18:18:05
mads@kiilerich.com
repo group: introduce editing of owner

The repo group owner concept was only partially implemented. Owners were shown
in the repo group listing, but couldn't be changed. Users owning repo groups
couldn't be deleted, with no other solution than deleting owned repo groups.

This also fixes the existing broken update_repo_group API, which tried to use
unimplemented functionality.
6 files changed with 15 insertions and 2 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/admin/repo_groups.py
Show inline comments
 
@@ -67,24 +67,25 @@ class RepoGroupsController(base.BaseCont
 
        c.repo_groups = [rg for rg in repo_groups
 
                         if rg[0] not in exclude_group_ids]
 

	
 
    def __load_data(self, group_id):
 
        """
 
        Load defaults settings for edit, and update
 

	
 
        :param group_id:
 
        """
 
        repo_group = db.RepoGroup.get_or_404(group_id)
 
        data = repo_group.get_dict()
 
        data['group_name'] = repo_group.name
 
        data['owner'] = repo_group.owner.username
 

	
 
        # fill repository group users
 
        for p in repo_group.repo_group_to_perm:
 
            data.update({'u_perm_%s' % p.user.username:
 
                             p.permission.permission_name})
 

	
 
        # fill repository group groups
 
        for p in repo_group.users_group_to_perm:
 
            data.update({'g_perm_%s' % p.users_group.users_group_name:
 
                             p.permission.permission_name})
 

	
 
        return data
 
@@ -137,25 +138,25 @@ class RepoGroupsController(base.BaseCont
 
        self.__load_defaults()
 

	
 
        # permissions for can create group based on parent_id are checked
 
        # here in the Form
 
        repo_group_form = RepoGroupForm(repo_groups=c.repo_groups)
 
        form_result = None
 
        try:
 
            form_result = repo_group_form.to_python(dict(request.POST))
 
            gr = RepoGroupModel().create(
 
                group_name=form_result['group_name'],
 
                group_description=form_result['group_description'],
 
                parent=form_result['parent_group_id'],
 
                owner=request.authuser.user_id, # TODO: make editable
 
                owner=request.authuser.user_id,
 
                copy_permissions=form_result['group_copy_permissions']
 
            )
 
            meta.Session().commit()
 
            # TODO: in future action_logger(, '', '', '')
 
        except formencode.Invalid as errors:
 
            return htmlfill.render(
 
                base.render('admin/repo_groups/repo_group_add.html'),
 
                defaults=errors.value,
 
                errors=errors.error_dict or {},
 
                prefix_error=False,
 
                encoding="UTF-8",
 
                force_defaults=False)
kallithea/model/forms.py
Show inline comments
 
@@ -164,24 +164,25 @@ def RepoGroupForm(edit=False, old_data=N
 
    class _RepoGroupForm(formencode.Schema):
 
        allow_extra_fields = True
 
        filter_extra_fields = False
 

	
 
        group_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
 
                         v.SlugifyName(),
 
                         v.ValidRegex(msg=_('Name must not contain only digits'))(r'(?!^\d+$)^.+$'))
 
        group_description = v.UnicodeString(strip=True, min=1,
 
                                            not_empty=False)
 
        group_copy_permissions = v.StringBoolean(if_missing=False)
 

	
 
        if edit:
 
            owner = All(v.UnicodeString(not_empty=True), v.ValidRepoUser())
 
            # FIXME: do a special check that we cannot move a group to one of
 
            # its children
 
            pass
 

	
 
        parent_group_id = All(v.CanCreateGroup(can_create_in_root),
 
                              v.OneOf(repo_group_ids, hideList=False,
 
                                      testValueList=True,
 
                                      if_missing=None, not_empty=True),
 
                              v.Int(min=-1, not_empty=True))
 
        chained_validators = [v.ValidRepoGroup(edit, old_data)]
 

	
 
    return _RepoGroupForm
kallithea/model/repo_group.py
Show inline comments
 
@@ -272,24 +272,26 @@ class RepoGroupModel(object):
 
            # break the loop and don't proceed with other changes
 
            if recursive not in ['all', 'repos', 'groups']:
 
                break
 

	
 
        return updates
 

	
 
    def update(self, repo_group, repo_group_args):
 
        try:
 
            repo_group = db.RepoGroup.guess_instance(repo_group)
 
            old_path = repo_group.full_path
 

	
 
            # change properties
 
            if 'owner' in repo_group_args:
 
                repo_group.owner = db.User.get_by_username(repo_group_args['owner'])
 
            if 'group_description' in repo_group_args:
 
                repo_group.group_description = repo_group_args['group_description']
 
            if 'parent_group_id' in repo_group_args:
 
                assert repo_group_args['parent_group_id'] != '-1', repo_group_args  # RepoGroupForm should have converted to None
 
                repo_group.parent_group = db.RepoGroup.get(repo_group_args['parent_group_id'])
 
                repo_group.group_name = repo_group.get_new_name(repo_group.name)
 
            if 'group_name' in repo_group_args:
 
                group_name = repo_group_args['group_name']
 
                if kallithea.lib.utils2.repo_name_slug(group_name) != group_name:
 
                    raise Exception('invalid repo group name %s' % group_name)
 
                repo_group.group_name = repo_group.get_new_name(group_name)
 
            new_path = repo_group.full_path
kallithea/templates/admin/repo_groups/repo_group_edit_settings.html
Show inline comments
 
## -*- coding: utf-8 -*-
 
${h.form(url('update_repos_group',group_name=c.repo_group.group_name))}
 
<div class="form">
 
        <div class="form-group">
 
            <label class="control-label" for="group_name">${_('Group name')}:</label>
 
            <div>
 
                ${h.text('group_name',class_='form-control')}
 
            </div>
 
        </div>
 

	
 
        <div class="form-group">
 
            <label class="control-label" for="owner">${_('Owner')}:</label>
 
            <div>
 
               ${h.text('owner',class_='form-control', placeholder=_('Type name of user'))}
 
            </div>
 
        </div>
 

	
 
        <div class="form-group">
 
            <label class="control-label" for="group_description">${_('Description')}:</label>
 
            <div>
 
                ${h.textarea('group_description',cols=23,rows=5,class_='form-control')}
 
            </div>
 
        </div>
 

	
 
        <div class="form-group">
 
            <label class="control-label" for="parent_group_id">${_('Group parent')}:</label>
 
            <div>
 
                ${h.select('parent_group_id','',c.repo_groups,class_='form-control')}
 
            </div>
 
        </div>
 
@@ -38,14 +45,15 @@ ${h.form(url('delete_repo_group', group_
 
                ${h.submit('remove_%s' % c.repo_group.group_name,_('Remove this group'),class_="btn btn-danger",onclick="return confirm('"+_('Confirm to delete this group')+"');")}
 
            </div>
 
        </div>
 
</div>
 
${h.end_form()}
 

	
 
<script>
 
    'use strict';
 
    $(document).ready(function(){
 
        $("#parent_group_id").select2({
 
            'dropdownAutoWidth': true
 
        });
 
        SimpleUserAutoComplete($('#owner'));
 
    });
 
</script>
kallithea/tests/api/api_base.py
Show inline comments
 
@@ -1842,25 +1842,25 @@ class _BaseTestApi(object):
 
        id_, params = _build_data(self.apikey,
 
                                  'revoke_user_group_permission',
 
                                  repoid=self.REPO,
 
                                  usergroupid=TEST_USER_GROUP, )
 
        response = api_call(self, params)
 

	
 
        expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % (
 
            TEST_USER_GROUP, self.REPO
 
        )
 
        self._compare_error(id_, expected, given=response.body)
 

	
 
    @base.parametrize('changing_attr,updates', [
 
        #('owner', {'owner': base.TEST_USER_REGULAR_LOGIN}),  # currently broken
 
        ('owner', {'owner': base.TEST_USER_REGULAR_LOGIN}),
 
        ('description', {'description': 'new description'}),
 
        ('group_name', {'group_name': 'new_repo_name'}),
 
        ('parent', {'parent': 'test_group_for_update'}),
 
    ])
 
    def test_api_update_repo_group(self, changing_attr, updates):
 
        group_name = 'lololo'
 
        repo_group = fixture.create_repo_group(group_name)
 

	
 
        new_group_name = group_name
 
        if changing_attr == 'group_name':
 
            assert repo_group.parent_group_id is None  # lazy assumption for this test
 
            new_group_name = updates['group_name']
kallithea/tests/functional/test_admin_repo_groups.py
Show inline comments
 
@@ -44,24 +44,25 @@ class TestRepoGroupsController(base.Test
 
        response.mustcontain('>lala<')
 

	
 
        # edit with form error
 
        response = self.app.post(base.url('update_repos_group', group_name=group_name),
 
                                         {'group_name': group_name,
 
                                          '_session_csrf_secret_token': self.session_csrf_secret_token()})
 
        response.mustcontain('name="group_name" type="text" value="%s"' % group_name)
 
        response.mustcontain('<!-- for: group_description -->')
 

	
 
        # edit
 
        response = self.app.post(base.url('update_repos_group', group_name=group_name),
 
                                         {'group_name': group_name,
 
                                         'owner': base.TEST_USER_REGULAR2_LOGIN,
 
                                         'group_description': 'lolo',
 
                                          '_session_csrf_secret_token': self.session_csrf_secret_token()})
 
        self.checkSessionFlash(response, 'Updated repository group %s' % group_name)
 
        response = response.follow()
 
        response.mustcontain('name="group_name" type="text" value="%s"' % group_name)
 
        response.mustcontain(no='<!-- for: group_description -->')
 
        response.mustcontain('>lolo<')
 

	
 
        # listing
 
        response = self.app.get(base.url('repos_groups'))
 
        response.mustcontain('raw_name": "%s"' % group_name)
 

	
0 comments (0 inline, 0 general)