diff --git a/rhodecode/api/__init__.py b/rhodecode/api/__init__.py --- a/rhodecode/api/__init__.py +++ b/rhodecode/api/__init__.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # diff --git a/rhodecode/api/exc.py b/rhodecode/api/exc.py --- a/rhodecode/api/exc.py +++ b/rhodecode/api/exc.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # diff --git a/rhodecode/api/tests/__init__.py b/rhodecode/api/tests/__init__.py --- a/rhodecode/api/tests/__init__.py +++ b/rhodecode/api/tests/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2010-2020 RhodeCode GmbH # diff --git a/rhodecode/api/tests/conftest.py b/rhodecode/api/tests/conftest.py --- a/rhodecode/api/tests/conftest.py +++ b/rhodecode/api/tests/conftest.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2010-2020 RhodeCode GmbH # diff --git a/rhodecode/api/tests/test_add_field_to_repo.py b/rhodecode/api/tests/test_add_field_to_repo.py --- a/rhodecode/api/tests/test_add_field_to_repo.py +++ b/rhodecode/api/tests/test_add_field_to_repo.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2010-2020 RhodeCode GmbH # diff --git a/rhodecode/api/tests/test_add_user_to_user_group.py b/rhodecode/api/tests/test_add_user_to_user_group.py --- a/rhodecode/api/tests/test_add_user_to_user_group.py +++ b/rhodecode/api/tests/test_add_user_to_user_group.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2010-2020 RhodeCode GmbH # diff --git a/rhodecode/api/tests/test_api.py b/rhodecode/api/tests/test_api.py --- a/rhodecode/api/tests/test_api.py +++ b/rhodecode/api/tests/test_api.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2010-2020 RhodeCode GmbH # diff --git a/rhodecode/api/tests/test_cleanup_sessions.py b/rhodecode/api/tests/test_cleanup_sessions.py --- a/rhodecode/api/tests/test_cleanup_sessions.py +++ b/rhodecode/api/tests/test_cleanup_sessions.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2017-2020 RhodeCode GmbH # diff --git a/rhodecode/api/tests/test_close_pull_request.py b/rhodecode/api/tests/test_close_pull_request.py --- a/rhodecode/api/tests/test_close_pull_request.py +++ b/rhodecode/api/tests/test_close_pull_request.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2010-2020 RhodeCode GmbH # diff --git a/rhodecode/api/tests/test_comment_commit.py b/rhodecode/api/tests/test_comment_commit.py --- a/rhodecode/api/tests/test_comment_commit.py +++ b/rhodecode/api/tests/test_comment_commit.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2010-2020 RhodeCode GmbH # diff --git a/rhodecode/api/utils.py b/rhodecode/api/utils.py --- a/rhodecode/api/utils.py +++ b/rhodecode/api/utils.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2014-2020 RhodeCode GmbH # @@ -28,7 +28,7 @@ import logging from rhodecode.api.exc import JSONRPCError from rhodecode.lib.auth import ( HasPermissionAnyApi, HasRepoPermissionAnyApi, HasRepoGroupPermissionAnyApi) -from rhodecode.lib.utils import safe_unicode +from rhodecode.lib.str_utils import safe_str from rhodecode.lib.vcs.exceptions import RepositoryError from rhodecode.lib.view_utils import get_commit_from_ref_name from rhodecode.lib.utils2 import str2bool @@ -368,7 +368,7 @@ def build_commit_data(rhodecode_vcs_repo from rhodecode.lib import diffs _diff = rhodecode_vcs_repo.get_diff(commit1, commit2,) - diff_processor = diffs.DiffProcessor(_diff, format='newdiff', show_full_diff=True) + diff_processor = diffs.DiffProcessor(_diff, diff_format='newdiff', show_full_diff=True) for dp in diff_processor.prepare(): del dp['stats']['ops'] @@ -446,7 +446,7 @@ def _get_commit_dict( "deleted": None } return { - "filename": safe_unicode(filename), + "filename": safe_str(filename), "op": op, # extra details diff --git a/rhodecode/api/views/__init__.py b/rhodecode/api/views/__init__.py --- a/rhodecode/api/views/__init__.py +++ b/rhodecode/api/views/__init__.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2015-2020 RhodeCode GmbH # diff --git a/rhodecode/api/views/deprecated_api.py b/rhodecode/api/views/deprecated_api.py --- a/rhodecode/api/views/deprecated_api.py +++ b/rhodecode/api/views/deprecated_api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # diff --git a/rhodecode/api/views/gist_api.py b/rhodecode/api/views/gist_api.py --- a/rhodecode/api/views/gist_api.py +++ b/rhodecode/api/views/gist_api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # @@ -48,14 +48,16 @@ def get_gist(request, apiuser, gistid, c gist = get_gist_or_error(gistid) content = Optional.extract(content) + if not has_superadmin_permission(apiuser): if gist.gist_owner != apiuser.user_id: raise JSONRPCError('gist `%s` does not exist' % (gistid,)) data = gist.get_api_data() + if content: from rhodecode.model.gist import GistModel rev, gist_files = GistModel().get_gist_files(gistid) - data['content'] = dict([(x.path, x.content) for x in gist_files]) + data['content'] = dict([(x.path, x.str_content) for x in gist_files]) return data diff --git a/rhodecode/api/views/pull_request_api.py b/rhodecode/api/views/pull_request_api.py --- a/rhodecode/api/views/pull_request_api.py +++ b/rhodecode/api/views/pull_request_api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # @@ -525,9 +525,7 @@ def comment_pull_request( raise JSONRPCError('userid is not the same as your user') if pull_request.is_closed(): - raise JSONRPCError( - 'pull request `%s` comment failed, pull request is closed' % ( - pullrequestid,)) + raise JSONRPCError(f'pull request `{pullrequestid}` comment failed, pull request is closed') if not PullRequestModel().check_user_read( pull_request, apiuser, api=True): @@ -545,34 +543,29 @@ def comment_pull_request( 'Both message and status parameters are missing. ' 'At least one is required.') - if (status not in (st[0] for st in ChangesetStatus.STATUSES) and - status is not None): - raise JSONRPCError('Unknown comment status: `%s`' % status) + if status and status not in (st[0] for st in ChangesetStatus.STATUSES): + raise JSONRPCError(f'Unknown comment status: `{status}`') if commit_id and commit_id not in pull_request.revisions: - raise JSONRPCError( - 'Invalid commit_id `%s` for this pull request.' % commit_id) + raise JSONRPCError(f'Invalid commit_id `{commit_id}` for this pull request.') allowed_to_change_status = PullRequestModel().check_user_change_status( pull_request, apiuser) # if commit_id is passed re-validated if user is allowed to change status - # based on latest commit_id from the PR + # based on the latest commit_id from the PR if commit_id: commit_idx = pull_request.revisions.index(commit_id) if commit_idx != 0: + log.warning('Resetting allowed_to_change_status = False because commit is NOT the latest in pull-request') allowed_to_change_status = False if resolves_comment_id: comment = ChangesetComment.get(resolves_comment_id) if not comment: - raise JSONRPCError( - 'Invalid resolves_comment_id `%s` for this pull request.' - % resolves_comment_id) + raise JSONRPCError(f'Invalid resolves_comment_id `{resolves_comment_id}` for this pull request.') if comment.comment_type != ChangesetComment.COMMENT_TYPE_TODO: - raise JSONRPCError( - 'Comment `%s` is wrong type for setting status to resolved.' - % resolves_comment_id) + raise JSONRPCError(f'Comment `{resolves_comment_id}` is wrong type for setting status to resolved.') text = message status_label = ChangesetStatus.get_status_lbl(status) @@ -602,6 +595,7 @@ def comment_pull_request( extra_recipients=extra_recipients, send_email=send_email ) + is_inline = comment.is_inline if allowed_to_change_status and status: @@ -648,6 +642,7 @@ def comment_pull_request( return data + def _reviewers_validation(obj_list): schema = ReviewerListSchema() try: diff --git a/rhodecode/api/views/repo_api.py b/rhodecode/api/views/repo_api.py --- a/rhodecode/api/views/repo_api.py +++ b/rhodecode/api/views/repo_api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # @@ -36,7 +36,7 @@ from rhodecode.lib.auth import ( HasRepoPermissionAnyApi) from rhodecode.lib.celerylib.utils import get_task_id from rhodecode.lib.utils2 import ( - str2bool, time_to_datetime, safe_str, safe_int, safe_unicode) + str2bool, time_to_datetime, safe_str, safe_int) from rhodecode.lib.ext_json import json from rhodecode.lib.exceptions import ( StatusChangeOnClosedPullRequestError, CommentVersionMismatch) @@ -204,6 +204,7 @@ def get_repo(request, apiuser, repoid, c data = repo.get_api_data(include_secrets=include_secrets) data['permissions'] = permissions data['followers'] = following_users + return data @@ -474,7 +475,9 @@ def get_repo_nodes(request, apiuser, rep _extended_types = ['basic', 'full'] if details not in _extended_types: - raise JSONRPCError('ret_type must be one of %s' % (','.join(_extended_types))) + ret_types = ','.join(_extended_types) + raise JSONRPCError(f'ret_type must be one of {ret_types}') + extended_info = False content = False if details == 'basic': @@ -500,20 +503,19 @@ def get_repo_nodes(request, apiuser, rep 'files': _f, 'dirs': _d, } + return _map[ret_type] except KeyError: - raise JSONRPCError( - 'ret_type must be one of %s' % (','.join(sorted(_map.keys())))) + keys = ','.join(sorted(_map.keys())) + raise JSONRPCError(f'ret_type must be one of {keys}') except Exception: log.exception("Exception occurred while trying to get repo nodes") - raise JSONRPCError( - 'failed to get repo: `%s` nodes' % repo.repo_name - ) + raise JSONRPCError(f'failed to get repo: `{repo.repo_name}` nodes') @jsonrpc_method() def get_repo_file(request, apiuser, repoid, commit_id, file_path, - max_file_bytes=Optional(None), details=Optional('basic'), + max_file_bytes=Optional(0), details=Optional('basic'), cache=Optional(True)): """ Returns a single file from repository at given revision. @@ -564,10 +566,12 @@ def get_repo_file(request, apiuser, repo cache = Optional.extract(cache, binary=True) details = Optional.extract(details) + max_file_bytes = Optional.extract(max_file_bytes) + _extended_types = ['minimal', 'minimal+search', 'basic', 'full'] if details not in _extended_types: - raise JSONRPCError( - 'ret_type must be one of %s, got %s' % (','.join(_extended_types)), details) + ret_types = ','.join(_extended_types) + raise JSONRPCError(f'ret_type must be one of %s, got {ret_types}', details) extended_info = False content = False @@ -580,7 +584,7 @@ def get_repo_file(request, apiuser, repo elif details == 'full': extended_info = content = True - file_path = safe_unicode(file_path) + file_path = safe_str(file_path) try: # check if repo is not empty by any chance, skip quicker if it is. _scm = repo.scm_instance() @@ -590,14 +594,14 @@ def get_repo_file(request, apiuser, repo node = ScmModel().get_node( repo, commit_id, file_path, extended_info=extended_info, content=content, max_file_bytes=max_file_bytes, cache=cache) + except NodeDoesNotExistError: - raise JSONRPCError(u'There is no file in repo: `{}` at path `{}` for commit: `{}`'.format( - repo.repo_name, file_path, commit_id)) + raise JSONRPCError( + f'There is no file in repo: `{repo.repo_name}` at path `{file_path}` for commit: `{commit_id}`') except Exception: - log.exception(u"Exception occurred while trying to get repo %s file", + log.exception("Exception occurred while trying to get repo %s file", repo.repo_name) - raise JSONRPCError(u'failed to get repo: `{}` file at path {}'.format( - repo.repo_name, file_path)) + raise JSONRPCError(f'failed to get repo: `{repo.repo_name}` file at path {file_path}') return node @@ -619,11 +623,11 @@ def get_repo_fts_tree(request, apiuser, validate_repo_permissions(apiuser, repoid, repo, _perms) repo_id = repo.repo_id - cache_seconds = safe_int(rhodecode.CONFIG.get('rc_cache.cache_repo.expiration_time')) + cache_seconds = rhodecode.ConfigGet().get_int('rc_cache.cache_repo.expiration_time') cache_on = cache_seconds > 0 - cache_namespace_uid = 'cache_repo.{}'.format(repo_id) - region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid) + cache_namespace_uid = 'repo.{}'.format(repo_id) + rc_cache.get_or_create_region('cache_repo', cache_namespace_uid) def compute_fts_tree(cache_ver, repo_id, commit_id, root_path): return ScmModel().get_fts_data(repo_id, commit_id, root_path) @@ -631,11 +635,11 @@ def get_repo_fts_tree(request, apiuser, try: # check if repo is not empty by any chance, skip quicker if it is. _scm = repo.scm_instance() - if _scm.is_empty(): + if not _scm or _scm.is_empty(): return [] except RepositoryError: log.exception("Exception occurred while trying to get repo nodes") - raise JSONRPCError('failed to get repo: `%s` nodes' % repo.repo_name) + raise JSONRPCError(f'failed to get repo: `{repo.repo_name}` nodes') try: # we need to resolve commit_id to a FULL sha for cache to work correctly. @@ -647,6 +651,7 @@ def get_repo_fts_tree(request, apiuser, repo_id, commit_id, cache_on, cache_seconds or 0)) tree_files = compute_fts_tree(rc_cache.FILE_TREE_CACHE_VER, repo_id, commit_id, root_path) + return tree_files except Exception: @@ -1869,7 +1874,11 @@ def edit_comment(request, apiuser, messa raise JSONRPCError('userid is not the same as your user') comment_author = comment.author.user_id == auth_user.user_id - if not (comment.immutable is False and (is_super_admin or is_repo_admin) or comment_author): + + if comment.immutable: + raise JSONRPCError("Immutable comment cannot be edited") + + if not (is_super_admin or is_repo_admin or comment_author): raise JSONRPCError("you don't have access to edit this comment") try: diff --git a/rhodecode/api/views/repo_group_api.py b/rhodecode/api/views/repo_group_api.py --- a/rhodecode/api/views/repo_group_api.py +++ b/rhodecode/api/views/repo_group_api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # diff --git a/rhodecode/api/views/server_api.py b/rhodecode/api/views/server_api.py --- a/rhodecode/api/views/server_api.py +++ b/rhodecode/api/views/server_api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # @@ -342,8 +342,9 @@ def get_method(request, apiuser, pattern matches = find_methods(request.registry.jsonrpc_methods, pattern) args_desc = [] - if len(matches) == 1: - func = matches[matches.keys()[0]] + matches_keys = list(matches.keys()) + if len(matches_keys) == 1: + func = matches[matches_keys[0]] argspec = inspect.getargspec(func) arglist = argspec[0] @@ -356,7 +357,7 @@ def get_method(request, apiuser, pattern reversed(arglist), reversed(defaults), fillvalue=default_empty)) args_desc.append(func_kwargs) - return matches.keys() + args_desc + return matches_keys + args_desc @jsonrpc_method() diff --git a/rhodecode/api/views/testing_api.py b/rhodecode/api/views/testing_api.py --- a/rhodecode/api/views/testing_api.py +++ b/rhodecode/api/views/testing_api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # diff --git a/rhodecode/api/views/user_api.py b/rhodecode/api/views/user_api.py --- a/rhodecode/api/views/user_api.py +++ b/rhodecode/api/views/user_api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH # diff --git a/rhodecode/api/views/user_group_api.py b/rhodecode/api/views/user_group_api.py --- a/rhodecode/api/views/user_group_api.py +++ b/rhodecode/api/views/user_group_api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # Copyright (C) 2011-2020 RhodeCode GmbH #