# HG changeset patch # User Marcin Kuzminski # Date 2017-04-28 19:21:05 # Node ID 7173cb6f116de905dff2607ee883e4277bdfc1a2 # Parent 48d816cec44e0d78f65d1ae71e9cad9a6671569d hooks: expose user agent in the variables submitted to pull/push hooks. diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -162,6 +162,10 @@ def get_access_path(environ): return path +def get_user_agent(environ): + return environ.get('HTTP_USER_AGENT') + + def vcs_operation_context( environ, repo_name, username, action, scm, check_locking=True, is_shadow_repo=False): @@ -200,6 +204,7 @@ def vcs_operation_context( 'make_lock': make_lock, 'locked_by': locked_by, 'server_url': utils2.get_server_url(environ), + 'user_agent': get_user_agent(environ), 'hooks': get_enabled_hook_classes(ui_settings), 'is_shadow_repo': is_shadow_repo, } diff --git a/rhodecode/tests/events/test_repo.py b/rhodecode/tests/events/test_repo.py --- a/rhodecode/tests/events/test_repo.py +++ b/rhodecode/tests/events/test_repo.py @@ -43,6 +43,7 @@ def scm_extras(user_regular, repo_stub): 'config': '', 'server_url': 'http://example.com', 'make_lock': None, + 'user-agent': 'some-client', 'locked_by': [None], 'commit_ids': ['a' * 40] * 3, 'is_shadow_repo': False, diff --git a/rhodecode/tests/lib/test_base.py b/rhodecode/tests/lib/test_base.py --- a/rhodecode/tests/lib/test_base.py +++ b/rhodecode/tests/lib/test_base.py @@ -34,8 +34,9 @@ from rhodecode.model import db ('scm', 'stub_scm'), ('hooks', ['stub_hook']), ('config', 'stub_ini_filename'), - ('ip', 'fake_ip'), + ('ip', '1.2.3.4'), ('server_url', 'https://example.com'), + ('user_agent', 'client-text-v1.1'), # TODO: johbo: Commpare locking parameters with `_get_rc_scm_extras` # in hooks_utils. ('make_lock', None), @@ -71,7 +72,6 @@ def test_vcs_operation_context_can_skip_ @patch.object( base, 'get_enabled_hook_classes', Mock(return_value=['stub_hook'])) -@patch.object(base, 'get_ip_addr', Mock(return_value="fake_ip")) @patch('rhodecode.lib.utils2.get_server_url', Mock(return_value='https://example.com')) def call_vcs_operation_context(**kwargs_override): @@ -87,7 +87,9 @@ def call_vcs_operation_context(**kwargs_ 'rhodecode.CONFIG', {'__file__': 'stub_ini_filename'}) settings_patch = patch.object(base, 'VcsSettingsModel') with config_file_patch, settings_patch as settings_mock: - result = base.vcs_operation_context(environ={}, **kwargs) + result = base.vcs_operation_context( + environ={'HTTP_USER_AGENT': 'client-text-v1.1', + 'REMOTE_ADDR': '1.2.3.4'}, **kwargs) settings_mock.assert_called_once_with(repo='stub_repo_name') return result diff --git a/rhodecode/tests/lib/test_hooks_base.py b/rhodecode/tests/lib/test_hooks_base.py --- a/rhodecode/tests/lib/test_hooks_base.py +++ b/rhodecode/tests/lib/test_hooks_base.py @@ -39,6 +39,7 @@ def test_post_push_truncates_commits(use 'config': '', 'server_url': 'http://example.com', 'make_lock': None, + 'user_agent': 'some-client', 'locked_by': [None], 'commit_ids': ['abcde12345' * 4] * 30000, 'is_shadow_repo': False, @@ -72,6 +73,7 @@ def hook_extras(user_regular, repo_stub) 'config': '', 'server_url': 'http://example.com', 'make_lock': None, + 'user_agent': 'some-client', 'locked_by': [None], 'commit_ids': [], 'is_shadow_repo': False,