Skip to content
Snippets Groups Projects
Commit 5f58ee03 authored by Georges Racinet's avatar Georges Racinet
Browse files

check_publish: moved to hooks.perm

It's nicer and clearer to invoke it
as `heptapod.hooks.perm.check_publish`
parent a9d9308d
No related branches found
No related tags found
1 merge request!6New hook to check write permissions outside of HTTP contexts
from mercurial import (
scmutil,
configitems,
phases,
)
from . import perm
if 'allow-publish' not in configitems.coreitems['web']:
configitems._register(configitems.coreitems,
section='web',
name='allow-publish',
alias=[('web', 'allow_publish')],
default=lambda: list(['*']),
)
def check_publish(repo, *args, **kwargs):
perm.validate_hook_type('pretxnclose', **kwargs)
remoteuser = perm.get_remote_user(repo)
if remoteuser is not None:
repo.ui.debug(
'check_publish detected push from user: %r\n' % remoteuser)
if perm.allowed(repo, remoteuser, 'web', 'allow-publish'):
# we have nothing more to check
return 0
tr = repo.currenttransaction()
assert tr is not None and tr.running()
phaseschanges = tr.changes.get("phases", {})
publishing = set(rev for rev, (old, new) in phaseschanges.iteritems()
if new == phases.public and old != phases.public)
if publishing:
node = repo.changelog.node
nodes = [node(r) for r in sorted(publishing)]
nodes = scmutil.nodesummaries(repo, nodes)
msg = 'user "%s" is not authorised to publish changesets: %s\n'
repo.ui.warn(msg % (remoteuser, nodes))
return 1
return 0
# flake8: noqa
"""Compatibility reexport."""
from .perm import check_publish
from mercurial import (
configitems,
error,
......@@ -2,3 +3,5 @@
error,
scmutil,
phases,
)
......@@ -3,5 +6,13 @@
)
if 'allow-publish' not in configitems.coreitems['web']:
configitems._register(configitems.coreitems,
section='web',
name='allow-publish',
alias=[('web', 'allow_publish')],
default=lambda: list(['*']),
)
def validate_hook_type(expected, hooktype=None, **kw):
"""Check that the hook has the expected type, and raise if not.
......@@ -72,3 +83,30 @@
return True
return False
def check_publish(repo, *args, **kwargs):
validate_hook_type('pretxnclose', **kwargs)
remoteuser = get_remote_user(repo)
if remoteuser is not None:
repo.ui.debug(
'check_publish detected push from user: %r\n' % remoteuser)
if allowed(repo, remoteuser, 'web', 'allow-publish'):
# we have nothing more to check
return 0
tr = repo.currenttransaction()
assert tr is not None and tr.running()
phaseschanges = tr.changes.get("phases", {})
publishing = set(rev for rev, (old, new) in phaseschanges.iteritems()
if new == phases.public and old != phases.public)
if publishing:
node = repo.changelog.node
nodes = [node(r) for r in sorted(publishing)]
nodes = scmutil.nodesummaries(repo, nodes)
msg = 'user "%s" is not authorised to publish changesets: %s\n'
repo.ui.warn(msg % (remoteuser, nodes))
return 1
return 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment