How to check inside a Mercurial hook if the user is registered?

Hello,

I have a Mercurial hook that verifies whether the username in all commits matches registered users in RhodeCode. The hook is similar to this:

from mercurial.node import bin
from mercurial.encoding import fromlocal
import rhodecode.model.db
User = rhodecode.model.db.User


def hook(ui, repo, node=None, **kwargs):
    n = bin(node)
    changelog = repo.changelog
    start = changelog.rev(n)
    end = len(changelog)
    for rev in range(start, end):
        n = changelog.node(rev)
        ctx = repo[n]
        commit_username = fromlocal(ctx.user()).decode()
        registered_rhode_user = User.get_by_username(commit_username)
        if not registered_rhode_user:
            msg = "* check_user: Author of revision (%s) corresponds to non-existing login (%s)\n" % (node[:12], commit_username)
            ui.warn(msg.encode())
            return True
    return False

This hook works with RhodeCode 1.7.2 CE.

I want to upgrade the hook to work with the latest version of RhodeCode CE. It seems that the approach using User.get_by_username no longer functions in VCS hooks.

How can I check (inside a Mercurial hook) if the user with username is registered in RhodeCode?

I would appreciate your suggestions.

Hi,

latest release does not allow to do this, you should use rcextensions instead.
Here are some examples how to use a written functions of rcextensions, basically it\s a plugin system on top of hooks (like mercurial hooks)

it passes a serialized information about commits, for example push information, commit authors etc. Then you can write a logic to validate things yourself there.

https://code.rhodecode.com/rhodecode-enterprise-ce/files/default/rhodecode/config/rcextensions?at=default

https://code.rhodecode.com/rhodecode-enterprise-ce/files/default/rhodecode/config/rcextensions/examples/validate_commit_message_author.py?at=default

Thanks for the quick response! I’ll try to use rcextensions.