hooks: expose refs on push.

- allows easier pull request creation
This commit is contained in:
Marcin Kuzminski 2017-05-17 22:17:09 +02:00
parent 726bc5aba8
commit b942ed82f2
8 changed files with 208 additions and 7 deletions

View file

@ -319,6 +319,7 @@ class DbManage(object):
(RhodeCodeUi.HOOK_PRE_PUSH, 'python:vcsserver.hooks.pre_push'),
(RhodeCodeUi.HOOK_PRETX_PUSH, 'python:vcsserver.hooks.pre_push'),
(RhodeCodeUi.HOOK_PUSH, 'python:vcsserver.hooks.log_push_action'),
(RhodeCodeUi.HOOK_PUSH_KEY, 'python:vcsserver.hooks.key_push'),
]

View file

@ -232,8 +232,20 @@ def post_push(extras):
# 2xx Codes don't raise exceptions
output += _http_ret.title
output += 'RhodeCode: push completed\n'
if extras.new_refs:
tmpl = \
extras.server_url + '/' + \
extras.repository + \
"/pull-request/new?{ref_type}={ref_name}"
for branch_name in extras.new_refs['branches']:
output += 'RhodeCode: open pull request link: {}\n'.format(
tmpl.format(ref_type='branch', ref_name=branch_name))
for book_name in extras.new_refs['bookmarks']:
output += 'RhodeCode: open pull request link: {}\n'.format(
tmpl.format(ref_type='bookmark', ref_name=book_name))
output += 'RhodeCode: push completed\n'
return HookResponse(0, output)

View file

@ -428,6 +428,7 @@ def config_data_from_db(clear_session=True, repo=None):
if 'push' not in enabled_hook_classes:
skip_entries.append(('hooks', RhodeCodeUi.HOOK_PRE_PUSH))
skip_entries.append(('hooks', RhodeCodeUi.HOOK_PRETX_PUSH))
skip_entries.append(('hooks', RhodeCodeUi.HOOK_PUSH_KEY))
config = [entry for entry in config if entry[:2] not in skip_entries]

View file

@ -52,7 +52,8 @@ class SettingsModel(BaseModel):
BUILTIN_HOOKS = (
RhodeCodeUi.HOOK_REPO_SIZE, RhodeCodeUi.HOOK_PUSH,
RhodeCodeUi.HOOK_PRE_PUSH, RhodeCodeUi.HOOK_PRETX_PUSH,
RhodeCodeUi.HOOK_PULL, RhodeCodeUi.HOOK_PRE_PULL)
RhodeCodeUi.HOOK_PULL, RhodeCodeUi.HOOK_PRE_PULL,
RhodeCodeUi.HOOK_PUSH_KEY,)
HOOKS_SECTION = 'hooks'
def __init__(self, sa=None, repo=None):

View file

@ -86,22 +86,24 @@ HOOK_PUSH = db.RhodeCodeUi.HOOK_PUSH
HOOK_PRE_PULL = db.RhodeCodeUi.HOOK_PRE_PULL
HOOK_PULL = db.RhodeCodeUi.HOOK_PULL
HOOK_REPO_SIZE = db.RhodeCodeUi.HOOK_REPO_SIZE
HOOK_PUSH_KEY = db.RhodeCodeUi.HOOK_PUSH_KEY
HG_HOOKS = frozenset(
(HOOK_PRE_PULL, HOOK_PULL, HOOK_PRE_PUSH, HOOK_PRETX_PUSH, HOOK_PUSH,
HOOK_REPO_SIZE))
HOOK_REPO_SIZE, HOOK_PUSH_KEY))
@pytest.mark.parametrize('disabled_hooks,expected_hooks', [
([], HG_HOOKS),
(HG_HOOKS, []),
([HOOK_PRE_PUSH, HOOK_PRETX_PUSH, HOOK_REPO_SIZE], [HOOK_PRE_PULL, HOOK_PULL, HOOK_PUSH]),
([HOOK_PRE_PUSH, HOOK_PRETX_PUSH, HOOK_REPO_SIZE, HOOK_PUSH_KEY], [HOOK_PRE_PULL, HOOK_PULL, HOOK_PUSH]),
# When a pull/push hook is disabled, its pre-pull/push counterpart should
# be disabled too.
([HOOK_PUSH], [HOOK_PRE_PULL, HOOK_PULL, HOOK_REPO_SIZE]),
([HOOK_PULL], [HOOK_PRE_PUSH, HOOK_PRETX_PUSH, HOOK_PUSH, HOOK_REPO_SIZE]),
([HOOK_PULL], [HOOK_PRE_PUSH, HOOK_PRETX_PUSH, HOOK_PUSH, HOOK_REPO_SIZE,
HOOK_PUSH_KEY]),
])
def test_make_db_config_hg_hooks(pylonsapp, request, disabled_hooks,
expected_hooks):

View file

@ -131,6 +131,13 @@ def _check_proper_git_push(
assert "Setting default branch" not in stderr
def _check_proper_hg_push(stdout, stderr, branch='default'):
assert 'pushing to' in stdout
assert 'searching for changes' in stdout
assert 'abort:' not in stderr
def _check_proper_clone(stdout, stderr, vcs):
if vcs == 'hg':
assert 'requesting all changes' in stdout

View file

@ -84,6 +84,9 @@ class RcWebServer(object):
_url = 'http://%(user)s:%(passwd)s@%(host)s/%(cloned_repo)s' % params
return _url
def host_url(self):
return 'http://' + get_host_url(self.pylons_config)
@pytest.fixture(scope="module")
def rcextensions(request, pylonsapp, tmpdir_factory):

View file

@ -34,6 +34,7 @@ import time
import pytest
from rhodecode.lib.vcs.backends.git.repository import GitRepository
from rhodecode.lib.vcs.backends.hg.repository import MercurialRepository
from rhodecode.lib.vcs.nodes import FileNode
from rhodecode.model.auth_token import AuthTokenModel
from rhodecode.model.db import Repository, UserIpMap, CacheKey
@ -42,7 +43,8 @@ from rhodecode.model.user import UserModel
from rhodecode.tests import (GIT_REPO, HG_REPO, TEST_USER_ADMIN_LOGIN)
from rhodecode.tests.other.vcs_operations import (
Command, _check_proper_clone, _check_proper_git_push, _add_files_and_push,
Command, _check_proper_clone, _check_proper_git_push,
_check_proper_hg_push, _add_files_and_push,
HG_REPO_WITH_GROUP, GIT_REPO_WITH_GROUP)
@ -396,7 +398,6 @@ class TestVCSOperations(object):
'hg clone', clone_url, tmpdir.strpath)
assert 'abort: authorization failed' in stderr
def test_clone_by_auth_token_with_scope(
self, rc_web_server, tmpdir, user_util, enable_auth_plugins):
enable_auth_plugins(['egg:rhodecode-enterprise-ce#token',
@ -479,3 +480,176 @@ def test_git_fetches_from_remote_repository_with_annotated_tags(
source_repo = backend_git['annotated-tag']
target_vcs_repo = backend_git.create_repo().scm_instance()
target_vcs_repo.fetch(rc_web_server.repo_clone_url(source_repo.repo_name))
def test_git_push_shows_pull_request_refs(backend_git, rc_web_server, tmpdir):
"""
test if remote info about refs is visible
"""
empty_repo = backend_git.create_repo()
clone_url = rc_web_server.repo_clone_url(empty_repo.repo_name)
cmd = Command(tmpdir.strpath)
cmd.execute('git clone', clone_url)
repo = GitRepository(os.path.join(tmpdir.strpath, empty_repo.repo_name))
repo.in_memory_commit.add(FileNode('readme.md', content='## Hello'))
repo.in_memory_commit.commit(
message='Commit on branch Master',
author='Automatic test',
branch='master')
repo_cmd = Command(repo.path)
stdout, stderr = repo_cmd.execute('git push --verbose origin master')
_check_proper_git_push(stdout, stderr, branch='master')
ref = '{}/{}/pull-request/new?branch=master'.format(
rc_web_server.host_url(), empty_repo.repo_name)
assert 'remote: RhodeCode: open pull request link: {}'.format(ref) in stderr
assert 'remote: RhodeCode: push completed' in stderr
# push on the same branch
repo = GitRepository(os.path.join(tmpdir.strpath, empty_repo.repo_name))
repo.in_memory_commit.add(FileNode('setup.py', content='print\n'))
repo.in_memory_commit.commit(
message='Commit2 on branch Master',
author='Automatic test2',
branch='master')
repo_cmd = Command(repo.path)
stdout, stderr = repo_cmd.execute('git push --verbose origin master')
_check_proper_git_push(stdout, stderr, branch='master')
assert 'remote: RhodeCode: open pull request link: {}'.format(ref) in stderr
assert 'remote: RhodeCode: push completed' in stderr
# new Branch
repo = GitRepository(os.path.join(tmpdir.strpath, empty_repo.repo_name))
repo.in_memory_commit.add(FileNode('feature1.py', content='## Hello world'))
repo.in_memory_commit.commit(
message='Commit on branch feature',
author='Automatic test',
branch='feature')
repo_cmd = Command(repo.path)
stdout, stderr = repo_cmd.execute('git push --verbose origin feature')
_check_proper_git_push(stdout, stderr, branch='feature')
ref = '{}/{}/pull-request/new?branch=feature'.format(
rc_web_server.host_url(), empty_repo.repo_name)
assert 'remote: RhodeCode: open pull request link: {}'.format(ref) in stderr
assert 'remote: RhodeCode: push completed' in stderr
def test_hg_push_shows_pull_request_refs(backend_hg, rc_web_server, tmpdir):
empty_repo = backend_hg.create_repo()
clone_url = rc_web_server.repo_clone_url(empty_repo.repo_name)
cmd = Command(tmpdir.strpath)
cmd.execute('hg clone', clone_url)
repo = MercurialRepository(os.path.join(tmpdir.strpath, empty_repo.repo_name))
repo.in_memory_commit.add(FileNode(u'readme.md', content=u'## Hello'))
repo.in_memory_commit.commit(
message=u'Commit on branch default',
author=u'Automatic test',
branch='default')
repo_cmd = Command(repo.path)
repo_cmd.execute('hg checkout default')
stdout, stderr = repo_cmd.execute('hg push --verbose', clone_url)
_check_proper_hg_push(stdout, stderr, branch='default')
ref = '{}/{}/pull-request/new?branch=default'.format(
rc_web_server.host_url(), empty_repo.repo_name)
assert 'remote: RhodeCode: open pull request link: {}'.format(ref) in stdout
assert 'remote: RhodeCode: push completed' in stdout
# push on the same branch
repo = MercurialRepository(os.path.join(tmpdir.strpath, empty_repo.repo_name))
repo.in_memory_commit.add(FileNode(u'setup.py', content=u'print\n'))
repo.in_memory_commit.commit(
message=u'Commit2 on branch default',
author=u'Automatic test2',
branch=u'default')
repo_cmd = Command(repo.path)
repo_cmd.execute('hg checkout default')
stdout, stderr = repo_cmd.execute('hg push --verbose', clone_url)
_check_proper_hg_push(stdout, stderr, branch='default')
assert 'remote: RhodeCode: open pull request link: {}'.format(ref) in stdout
assert 'remote: RhodeCode: push completed' in stdout
# new Branch
repo = MercurialRepository(os.path.join(tmpdir.strpath, empty_repo.repo_name))
repo.in_memory_commit.add(FileNode(u'feature1.py', content=u'## Hello world'))
repo.in_memory_commit.commit(
message=u'Commit on branch feature',
author=u'Automatic test',
branch=u'feature')
repo_cmd = Command(repo.path)
repo_cmd.execute('hg checkout feature')
stdout, stderr = repo_cmd.execute('hg push --new-branch --verbose', clone_url)
_check_proper_hg_push(stdout, stderr, branch='feature')
ref = '{}/{}/pull-request/new?branch=feature'.format(
rc_web_server.host_url(), empty_repo.repo_name)
assert 'remote: RhodeCode: open pull request link: {}'.format(ref) in stdout
assert 'remote: RhodeCode: push completed' in stdout
def test_hg_push_shows_pull_request_refs_book(backend_hg, rc_web_server, tmpdir):
empty_repo = backend_hg.create_repo()
clone_url = rc_web_server.repo_clone_url(empty_repo.repo_name)
cmd = Command(tmpdir.strpath)
cmd.execute('hg clone', clone_url)
repo = MercurialRepository(os.path.join(tmpdir.strpath, empty_repo.repo_name))
repo.in_memory_commit.add(FileNode(u'readme.md', content=u'## Hello'))
repo.in_memory_commit.commit(
message=u'Commit on branch default',
author=u'Automatic test',
branch='default')
repo_cmd = Command(repo.path)
repo_cmd.execute('hg checkout default')
stdout, stderr = repo_cmd.execute('hg push --verbose', clone_url)
_check_proper_hg_push(stdout, stderr, branch='default')
ref = '{}/{}/pull-request/new?branch=default'.format(
rc_web_server.host_url(), empty_repo.repo_name)
assert 'remote: RhodeCode: open pull request link: {}'.format(ref) in stdout
assert 'remote: RhodeCode: push completed' in stdout
# add bookmark
repo = MercurialRepository(os.path.join(tmpdir.strpath, empty_repo.repo_name))
repo.in_memory_commit.add(FileNode(u'setup.py', content=u'print\n'))
repo.in_memory_commit.commit(
message=u'Commit2 on branch default',
author=u'Automatic test2',
branch=u'default')
repo_cmd = Command(repo.path)
repo_cmd.execute('hg checkout default')
repo_cmd.execute('hg bookmark feature2')
stdout, stderr = repo_cmd.execute('hg push -B feature2 --verbose', clone_url)
_check_proper_hg_push(stdout, stderr, branch='default')
ref = '{}/{}/pull-request/new?branch=default'.format(
rc_web_server.host_url(), empty_repo.repo_name)
assert 'remote: RhodeCode: open pull request link: {}'.format(ref) in stdout
ref = '{}/{}/pull-request/new?bookmark=feature2'.format(
rc_web_server.host_url(), empty_repo.repo_name)
assert 'remote: RhodeCode: open pull request link: {}'.format(ref) in stdout
assert 'remote: RhodeCode: push completed' in stdout
assert 'exporting bookmark feature2' in stdout