Changeset - 25c51511c8eb
[Not reviewed]
default
0 6 1
Mads Kiilerich (mads) - 5 years ago 2021-01-11 00:23:20
mads@kiilerich.com
hooks: put repo_size and update hooks in kallithea namespace

Keep things separate.

Include missing migration steps for 642847355a10.
7 files changed with 69 insertions and 12 deletions:
0 comments (0 inline, 0 general)
kallithea/alembic/versions/7ba0d2cad930_hooks_migrate_internal_hooks_to_.py
Show inline comments
 
new file 100644
 
# 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, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# 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, see <http://www.gnu.org/licenses/>.
 

	
 
"""hooks: migrate internal hooks to kallithea namespace
 

	
 
Revision ID: 7ba0d2cad930
 
Revises: f62826179f39
 
Create Date: 2021-01-11 00:10:13.576586
 

	
 
"""
 

	
 
# The following opaque hexadecimal identifiers ("revisions") are used
 
# by Alembic to track this migration script and its relations to others.
 
revision = '7ba0d2cad930'
 
down_revision = 'f62826179f39'
 
branch_labels = None
 
depends_on = None
 

	
 
from alembic import op
 
from sqlalchemy import MetaData, Table
 

	
 
from kallithea.model import db
 

	
 

	
 
meta = MetaData()
 

	
 

	
 
def upgrade():
 
    meta.bind = op.get_bind()
 
    ui = Table(db.Ui.__tablename__, meta, autoload=True)
 

	
 
    ui.update(values={
 
        'ui_key': 'changegroup.kallithea_update',
 
        'ui_value': 'python:',  # value in db isn't used
 
    }).where(ui.c.ui_key == 'changegroup.update').execute()
 
    ui.update(values={
 
        'ui_key': 'changegroup.kallithea_repo_size',
 
        'ui_value': 'python:',  # value in db isn't used
 
    }).where(ui.c.ui_key == 'changegroup.repo_size').execute()
 

	
 
    # 642847355a10 moved these hooks out of db - remove old entries
 
    ui.delete().where(ui.c.ui_key == 'changegroup.push_logger').execute()
 
    ui.delete().where(ui.c.ui_key == 'outgoing.pull_logger').execute()
 

	
 

	
 
def downgrade():
 
    pass
kallithea/bin/vcs_hooks.py
Show inline comments
 
@@ -39,46 +39,46 @@ from kallithea.lib import hooks, webutil
 
from kallithea.lib.utils2 import HookEnvironmentError, ascii_str, get_hook_environment, safe_bytes, safe_str
 
from kallithea.lib.vcs.backends.base import EmptyChangeset
 
from kallithea.lib.vcs.utils.helpers import get_scm_size
 
from kallithea.model import db
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
def repo_size(ui, repo, hooktype=None, **kwargs):
 
    """Show size of Mercurial repository.
 

	
 
    Called as Mercurial hook changegroup.repo_size after push.
 
    Called as Mercurial hook changegroup.kallithea_repo_size after push.
 
    """
 
    size_hg, size_root = get_scm_size('.hg', safe_str(repo.root))
 

	
 
    last_cs = repo[len(repo) - 1]
 

	
 
    msg = ('Repository size .hg: %s Checkout: %s Total: %s\n'
 
           'Last revision is now r%s:%s\n') % (
 
        webutils.format_byte_size(size_hg),
 
        webutils.format_byte_size(size_root),
 
        webutils.format_byte_size(size_hg + size_root),
 
        last_cs.rev(),
 
        ascii_str(last_cs.hex())[:12],
 
    )
 
    ui.status(safe_bytes(msg))
 

	
 

	
 
def update(ui, repo, hooktype=None, **kwargs):
 
    """Update repo after push. The equivalent to 'hg update' but using the same
 
    Mercurial as everything else.
 

	
 
    Called as Mercurial hook changegroup.update after push.
 
    Called as Mercurial hook changegroup.kallithea_update after push.
 
    """
 
    try:
 
        ui.pushbuffer(error=True, subproc=True)
 
        rev = brev = None
 
        mercurial.hg.updatetotally(ui, repo, rev, brev)
 
    finally:
 
        s = ui.popbuffer()  # usually just "x files updated, x files merged, x files removed, x files unresolved"
 
        log.info('%s update hook output: %s', safe_str(repo.root), safe_str(s).rstrip())
 

	
 

	
 
def pull_action(ui, repo, **kwargs):
 
    """Logs user pull action
kallithea/controllers/admin/settings.py
Show inline comments
 
@@ -91,28 +91,28 @@ class SettingsController(base.BaseContro
 
                     errors=errors.error_dict or {},
 
                     prefix_error=False,
 
                     encoding="UTF-8",
 
                     force_defaults=False)
 

	
 
            try:
 
                if c.visual.allow_repo_location_change:
 
                    sett = db.Ui.get_by_key('paths', '/')
 
                    sett.ui_value = form_result['paths_root_path']
 

	
 
                # HOOKS
 
                sett = db.Ui.get_by_key('hooks', db.Ui.HOOK_UPDATE)
 
                sett.ui_active = form_result['hooks_changegroup_update']
 
                sett.ui_active = form_result['hooks_changegroup_kallithea_update']
 

	
 
                sett = db.Ui.get_by_key('hooks', db.Ui.HOOK_REPO_SIZE)
 
                sett.ui_active = form_result['hooks_changegroup_repo_size']
 
                sett.ui_active = form_result['hooks_changegroup_kallithea_repo_size']
 

	
 
                ## EXTENSIONS
 
                sett = db.Ui.get_or_create('extensions', 'largefiles')
 
                sett.ui_active = form_result['extensions_largefiles']
 

	
 
#                sett = db.Ui.get_or_create('extensions', 'hggit')
 
#                sett.ui_active = form_result['extensions_hggit']
 

	
 
                meta.Session().commit()
 

	
 
                webutils.flash(_('Updated VCS settings'), category='success')
 

	
kallithea/model/db.py
Show inline comments
 
@@ -321,26 +321,26 @@ class Setting(meta.Base, BaseDbModel):
 
        }
 
        return info
 

	
 

	
 
class Ui(meta.Base, BaseDbModel):
 
    __tablename__ = 'ui'
 
    __table_args__ = (
 
        Index('ui_ui_section_ui_key_idx', 'ui_section', 'ui_key'),
 
        UniqueConstraint('ui_section', 'ui_key'),
 
        _table_args_default_dict,
 
    )
 

	
 
    HOOK_UPDATE = 'changegroup.update'
 
    HOOK_REPO_SIZE = 'changegroup.repo_size'
 
    HOOK_UPDATE = 'changegroup.kallithea_update'
 
    HOOK_REPO_SIZE = 'changegroup.kallithea_repo_size'
 

	
 
    ui_id = Column(Integer(), primary_key=True)
 
    ui_section = Column(String(255), nullable=False)
 
    ui_key = Column(String(255), nullable=False)
 
    ui_value = Column(String(255), nullable=True) # FIXME: not nullable?
 
    ui_active = Column(Boolean(), nullable=False, default=True)
 

	
 
    @classmethod
 
    def get_by_key(cls, section, key):
 
        """ Return specified Ui object, or None if not found. """
 
        return cls.query().filter_by(ui_section=section, ui_key=key).scalar()
 

	
kallithea/model/forms.py
Show inline comments
 
@@ -375,26 +375,26 @@ def ApplicationVisualisationForm():
 

	
 
    return _ApplicationVisualisationForm
 

	
 

	
 
def ApplicationUiSettingsForm():
 
    class _ApplicationUiSettingsForm(formencode.Schema):
 
        allow_extra_fields = True
 
        filter_extra_fields = False
 
        paths_root_path = All(
 
            v.ValidPath(),
 
            v.UnicodeString(strip=True, min=1, not_empty=True)
 
        )
 
        hooks_changegroup_update = v.StringBoolean(if_missing=False)
 
        hooks_changegroup_repo_size = v.StringBoolean(if_missing=False)
 
        hooks_changegroup_kallithea_update = v.StringBoolean(if_missing=False)
 
        hooks_changegroup_kallithea_repo_size = v.StringBoolean(if_missing=False)
 

	
 
        extensions_largefiles = v.StringBoolean(if_missing=False)
 
        extensions_hggit = v.StringBoolean(if_missing=False)
 

	
 
    return _ApplicationUiSettingsForm
 

	
 

	
 
def DefaultPermissionsForm(repo_perms_choices, group_perms_choices,
 
                           user_group_perms_choices, create_choices,
 
                           user_group_create_choices, fork_choices,
 
                           register_choices, extern_activate_choices):
 
    class _DefaultPermissionsForm(formencode.Schema):
kallithea/templates/admin/settings/settings_vcs.html
Show inline comments
 
${h.form(url('admin_settings'), method='post')}
 
    <div class="form">
 
            <div class="form-group">
 
                <label class="control-label">${_('Hooks')}:</label>
 
                <div>
 
                    <div class="checkbox">
 
                        <label>
 
                            ${h.checkbox('hooks_changegroup_repo_size','True')}
 
                            ${h.checkbox('hooks_changegroup_kallithea_repo_size','True')}
 
                            ${_('Show repository size after push')}
 
                        </label>
 
                    </div>
 
                    <div class="checkbox">
 
                        <label>
 
                            ${h.checkbox('hooks_changegroup_update','True')}
 
                            ${h.checkbox('hooks_changegroup_kallithea_update','True')}
 
                            ${_('Update repository after push (hg update)')}
 
                        </label>
 
                    </div>
 
                </div>
 
            </div>
 
            <div class="form-group">
 
                <label class="control-label">${_('Mercurial extensions')}:</label>
 
                <div>
 
                    <div class="checkbox">
 
                        <label>
 
                            ${h.checkbox('extensions_largefiles','True')}
 
                            ${_('Enable largefiles extension')}
kallithea/tests/functional/test_admin_settings.py
Show inline comments
 
@@ -82,31 +82,31 @@ class TestAdminSettingsController(base.T
 

	
 
        hook_id = db.Ui.get_by_key('hooks', 'test_hooks_2').ui_id
 
        ## delete
 
        self.app.post(base.url('admin_settings_hooks'),
 
                        params=dict(hook_id=hook_id, _session_csrf_secret_token=self.session_csrf_secret_token()))
 
        response = self.app.get(base.url('admin_settings_hooks'))
 
        response.mustcontain(no=['test_hooks_2'])
 
        response.mustcontain(no=['cd %s2' % base.TESTS_TMP_PATH])
 

	
 
    def test_add_existing_builtin_hook(self):
 
        self.log_user()
 
        response = self.app.post(base.url('admin_settings_hooks'),
 
                                params=dict(new_hook_ui_key='changegroup.update',
 
                                params=dict(new_hook_ui_key='changegroup.kallithea_update',
 
                                            new_hook_ui_value='attempted_new_value',
 
                                            _session_csrf_secret_token=self.session_csrf_secret_token()))
 

	
 
        self.checkSessionFlash(response, 'Builtin hooks are read-only')
 
        response = response.follow()
 
        response.mustcontain('changegroup.update')
 
        response.mustcontain('changegroup.kallithea_update')
 

	
 
    def test_index_search(self):
 
        self.log_user()
 
        response = self.app.get(base.url('admin_settings_search'))
 

	
 
    def test_index_system(self):
 
        self.log_user()
 
        response = self.app.get(base.url('admin_settings_system'))
 

	
 
    def test_ga_code_active(self):
 
        self.log_user()
 
        old_title = 'Kallithea'
0 comments (0 inline, 0 general)