##// END OF EJS Templates
api: code fixes / cleanups for python3
super-admin -
r5047:4fb1c779 default
parent child Browse files
Show More
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -1,4 +1,3 b''
1 # -*- coding: utf-8 -*-
2
1
3 # Copyright (C) 2010-2020 RhodeCode GmbH
2 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
3 #
@@ -1,4 +1,3 b''
1 # -*- coding: utf-8 -*-
2
1
3 # Copyright (C) 2010-2020 RhodeCode GmbH
2 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
3 #
@@ -1,4 +1,3 b''
1 # -*- coding: utf-8 -*-
2
1
3 # Copyright (C) 2010-2020 RhodeCode GmbH
2 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
3 #
@@ -1,4 +1,3 b''
1 # -*- coding: utf-8 -*-
2
1
3 # Copyright (C) 2010-2020 RhodeCode GmbH
2 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
3 #
@@ -1,4 +1,3 b''
1 # -*- coding: utf-8 -*-
2
1
3 # Copyright (C) 2010-2020 RhodeCode GmbH
2 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
3 #
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2017-2020 RhodeCode GmbH
3 # Copyright (C) 2017-2020 RhodeCode GmbH
4 #
4 #
@@ -1,4 +1,3 b''
1 # -*- coding: utf-8 -*-
2
1
3 # Copyright (C) 2010-2020 RhodeCode GmbH
2 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
3 #
@@ -1,4 +1,3 b''
1 # -*- coding: utf-8 -*-
2
1
3 # Copyright (C) 2010-2020 RhodeCode GmbH
2 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
3 #
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2014-2020 RhodeCode GmbH
3 # Copyright (C) 2014-2020 RhodeCode GmbH
4 #
4 #
@@ -28,7 +28,7 b' import logging'
28 from rhodecode.api.exc import JSONRPCError
28 from rhodecode.api.exc import JSONRPCError
29 from rhodecode.lib.auth import (
29 from rhodecode.lib.auth import (
30 HasPermissionAnyApi, HasRepoPermissionAnyApi, HasRepoGroupPermissionAnyApi)
30 HasPermissionAnyApi, HasRepoPermissionAnyApi, HasRepoGroupPermissionAnyApi)
31 from rhodecode.lib.utils import safe_unicode
31 from rhodecode.lib.str_utils import safe_str
32 from rhodecode.lib.vcs.exceptions import RepositoryError
32 from rhodecode.lib.vcs.exceptions import RepositoryError
33 from rhodecode.lib.view_utils import get_commit_from_ref_name
33 from rhodecode.lib.view_utils import get_commit_from_ref_name
34 from rhodecode.lib.utils2 import str2bool
34 from rhodecode.lib.utils2 import str2bool
@@ -368,7 +368,7 b' def build_commit_data(rhodecode_vcs_repo'
368 from rhodecode.lib import diffs
368 from rhodecode.lib import diffs
369
369
370 _diff = rhodecode_vcs_repo.get_diff(commit1, commit2,)
370 _diff = rhodecode_vcs_repo.get_diff(commit1, commit2,)
371 diff_processor = diffs.DiffProcessor(_diff, format='newdiff', show_full_diff=True)
371 diff_processor = diffs.DiffProcessor(_diff, diff_format='newdiff', show_full_diff=True)
372
372
373 for dp in diff_processor.prepare():
373 for dp in diff_processor.prepare():
374 del dp['stats']['ops']
374 del dp['stats']['ops']
@@ -446,7 +446,7 b' def _get_commit_dict('
446 "deleted": None
446 "deleted": None
447 }
447 }
448 return {
448 return {
449 "filename": safe_unicode(filename),
449 "filename": safe_str(filename),
450 "op": op,
450 "op": op,
451
451
452 # extra details
452 # extra details
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2015-2020 RhodeCode GmbH
3 # Copyright (C) 2015-2020 RhodeCode GmbH
4 #
4 #
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -48,14 +48,16 b' def get_gist(request, apiuser, gistid, c'
48
48
49 gist = get_gist_or_error(gistid)
49 gist = get_gist_or_error(gistid)
50 content = Optional.extract(content)
50 content = Optional.extract(content)
51
51 if not has_superadmin_permission(apiuser):
52 if not has_superadmin_permission(apiuser):
52 if gist.gist_owner != apiuser.user_id:
53 if gist.gist_owner != apiuser.user_id:
53 raise JSONRPCError('gist `%s` does not exist' % (gistid,))
54 raise JSONRPCError('gist `%s` does not exist' % (gistid,))
54 data = gist.get_api_data()
55 data = gist.get_api_data()
56
55 if content:
57 if content:
56 from rhodecode.model.gist import GistModel
58 from rhodecode.model.gist import GistModel
57 rev, gist_files = GistModel().get_gist_files(gistid)
59 rev, gist_files = GistModel().get_gist_files(gistid)
58 data['content'] = dict([(x.path, x.content) for x in gist_files])
60 data['content'] = dict([(x.path, x.str_content) for x in gist_files])
59 return data
61 return data
60
62
61
63
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -525,9 +525,7 b' def comment_pull_request('
525 raise JSONRPCError('userid is not the same as your user')
525 raise JSONRPCError('userid is not the same as your user')
526
526
527 if pull_request.is_closed():
527 if pull_request.is_closed():
528 raise JSONRPCError(
528 raise JSONRPCError(f'pull request `{pullrequestid}` comment failed, pull request is closed')
529 'pull request `%s` comment failed, pull request is closed' % (
530 pullrequestid,))
531
529
532 if not PullRequestModel().check_user_read(
530 if not PullRequestModel().check_user_read(
533 pull_request, apiuser, api=True):
531 pull_request, apiuser, api=True):
@@ -545,34 +543,29 b' def comment_pull_request('
545 'Both message and status parameters are missing. '
543 'Both message and status parameters are missing. '
546 'At least one is required.')
544 'At least one is required.')
547
545
548 if (status not in (st[0] for st in ChangesetStatus.STATUSES) and
546 if status and status not in (st[0] for st in ChangesetStatus.STATUSES):
549 status is not None):
547 raise JSONRPCError(f'Unknown comment status: `{status}`')
550 raise JSONRPCError('Unknown comment status: `%s`' % status)
551
548
552 if commit_id and commit_id not in pull_request.revisions:
549 if commit_id and commit_id not in pull_request.revisions:
553 raise JSONRPCError(
550 raise JSONRPCError(f'Invalid commit_id `{commit_id}` for this pull request.')
554 'Invalid commit_id `%s` for this pull request.' % commit_id)
555
551
556 allowed_to_change_status = PullRequestModel().check_user_change_status(
552 allowed_to_change_status = PullRequestModel().check_user_change_status(
557 pull_request, apiuser)
553 pull_request, apiuser)
558
554
559 # if commit_id is passed re-validated if user is allowed to change status
555 # if commit_id is passed re-validated if user is allowed to change status
560 # based on latest commit_id from the PR
556 # based on the latest commit_id from the PR
561 if commit_id:
557 if commit_id:
562 commit_idx = pull_request.revisions.index(commit_id)
558 commit_idx = pull_request.revisions.index(commit_id)
563 if commit_idx != 0:
559 if commit_idx != 0:
560 log.warning('Resetting allowed_to_change_status = False because commit is NOT the latest in pull-request')
564 allowed_to_change_status = False
561 allowed_to_change_status = False
565
562
566 if resolves_comment_id:
563 if resolves_comment_id:
567 comment = ChangesetComment.get(resolves_comment_id)
564 comment = ChangesetComment.get(resolves_comment_id)
568 if not comment:
565 if not comment:
569 raise JSONRPCError(
566 raise JSONRPCError(f'Invalid resolves_comment_id `{resolves_comment_id}` for this pull request.')
570 'Invalid resolves_comment_id `%s` for this pull request.'
571 % resolves_comment_id)
572 if comment.comment_type != ChangesetComment.COMMENT_TYPE_TODO:
567 if comment.comment_type != ChangesetComment.COMMENT_TYPE_TODO:
573 raise JSONRPCError(
568 raise JSONRPCError(f'Comment `{resolves_comment_id}` is wrong type for setting status to resolved.')
574 'Comment `%s` is wrong type for setting status to resolved.'
575 % resolves_comment_id)
576
569
577 text = message
570 text = message
578 status_label = ChangesetStatus.get_status_lbl(status)
571 status_label = ChangesetStatus.get_status_lbl(status)
@@ -602,6 +595,7 b' def comment_pull_request('
602 extra_recipients=extra_recipients,
595 extra_recipients=extra_recipients,
603 send_email=send_email
596 send_email=send_email
604 )
597 )
598
605 is_inline = comment.is_inline
599 is_inline = comment.is_inline
606
600
607 if allowed_to_change_status and status:
601 if allowed_to_change_status and status:
@@ -648,6 +642,7 b' def comment_pull_request('
648
642
649 return data
643 return data
650
644
645
651 def _reviewers_validation(obj_list):
646 def _reviewers_validation(obj_list):
652 schema = ReviewerListSchema()
647 schema = ReviewerListSchema()
653 try:
648 try:
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -36,7 +36,7 b' from rhodecode.lib.auth import ('
36 HasRepoPermissionAnyApi)
36 HasRepoPermissionAnyApi)
37 from rhodecode.lib.celerylib.utils import get_task_id
37 from rhodecode.lib.celerylib.utils import get_task_id
38 from rhodecode.lib.utils2 import (
38 from rhodecode.lib.utils2 import (
39 str2bool, time_to_datetime, safe_str, safe_int, safe_unicode)
39 str2bool, time_to_datetime, safe_str, safe_int)
40 from rhodecode.lib.ext_json import json
40 from rhodecode.lib.ext_json import json
41 from rhodecode.lib.exceptions import (
41 from rhodecode.lib.exceptions import (
42 StatusChangeOnClosedPullRequestError, CommentVersionMismatch)
42 StatusChangeOnClosedPullRequestError, CommentVersionMismatch)
@@ -204,6 +204,7 b' def get_repo(request, apiuser, repoid, c'
204 data = repo.get_api_data(include_secrets=include_secrets)
204 data = repo.get_api_data(include_secrets=include_secrets)
205 data['permissions'] = permissions
205 data['permissions'] = permissions
206 data['followers'] = following_users
206 data['followers'] = following_users
207
207 return data
208 return data
208
209
209
210
@@ -474,7 +475,9 b' def get_repo_nodes(request, apiuser, rep'
474
475
475 _extended_types = ['basic', 'full']
476 _extended_types = ['basic', 'full']
476 if details not in _extended_types:
477 if details not in _extended_types:
477 raise JSONRPCError('ret_type must be one of %s' % (','.join(_extended_types)))
478 ret_types = ','.join(_extended_types)
479 raise JSONRPCError(f'ret_type must be one of {ret_types}')
480
478 extended_info = False
481 extended_info = False
479 content = False
482 content = False
480 if details == 'basic':
483 if details == 'basic':
@@ -500,20 +503,19 b' def get_repo_nodes(request, apiuser, rep'
500 'files': _f,
503 'files': _f,
501 'dirs': _d,
504 'dirs': _d,
502 }
505 }
506
503 return _map[ret_type]
507 return _map[ret_type]
504 except KeyError:
508 except KeyError:
505 raise JSONRPCError(
509 keys = ','.join(sorted(_map.keys()))
506 'ret_type must be one of %s' % (','.join(sorted(_map.keys()))))
510 raise JSONRPCError(f'ret_type must be one of {keys}')
507 except Exception:
511 except Exception:
508 log.exception("Exception occurred while trying to get repo nodes")
512 log.exception("Exception occurred while trying to get repo nodes")
509 raise JSONRPCError(
513 raise JSONRPCError(f'failed to get repo: `{repo.repo_name}` nodes')
510 'failed to get repo: `%s` nodes' % repo.repo_name
511 )
512
514
513
515
514 @jsonrpc_method()
516 @jsonrpc_method()
515 def get_repo_file(request, apiuser, repoid, commit_id, file_path,
517 def get_repo_file(request, apiuser, repoid, commit_id, file_path,
516 max_file_bytes=Optional(None), details=Optional('basic'),
518 max_file_bytes=Optional(0), details=Optional('basic'),
517 cache=Optional(True)):
519 cache=Optional(True)):
518 """
520 """
519 Returns a single file from repository at given revision.
521 Returns a single file from repository at given revision.
@@ -564,10 +566,12 b' def get_repo_file(request, apiuser, repo'
564
566
565 cache = Optional.extract(cache, binary=True)
567 cache = Optional.extract(cache, binary=True)
566 details = Optional.extract(details)
568 details = Optional.extract(details)
569 max_file_bytes = Optional.extract(max_file_bytes)
570
567 _extended_types = ['minimal', 'minimal+search', 'basic', 'full']
571 _extended_types = ['minimal', 'minimal+search', 'basic', 'full']
568 if details not in _extended_types:
572 if details not in _extended_types:
569 raise JSONRPCError(
573 ret_types = ','.join(_extended_types)
570 'ret_type must be one of %s, got %s' % (','.join(_extended_types)), details)
574 raise JSONRPCError(f'ret_type must be one of %s, got {ret_types}', details)
571 extended_info = False
575 extended_info = False
572 content = False
576 content = False
573
577
@@ -580,7 +584,7 b' def get_repo_file(request, apiuser, repo'
580 elif details == 'full':
584 elif details == 'full':
581 extended_info = content = True
585 extended_info = content = True
582
586
583 file_path = safe_unicode(file_path)
587 file_path = safe_str(file_path)
584 try:
588 try:
585 # check if repo is not empty by any chance, skip quicker if it is.
589 # check if repo is not empty by any chance, skip quicker if it is.
586 _scm = repo.scm_instance()
590 _scm = repo.scm_instance()
@@ -590,14 +594,14 b' def get_repo_file(request, apiuser, repo'
590 node = ScmModel().get_node(
594 node = ScmModel().get_node(
591 repo, commit_id, file_path, extended_info=extended_info,
595 repo, commit_id, file_path, extended_info=extended_info,
592 content=content, max_file_bytes=max_file_bytes, cache=cache)
596 content=content, max_file_bytes=max_file_bytes, cache=cache)
597
593 except NodeDoesNotExistError:
598 except NodeDoesNotExistError:
594 raise JSONRPCError(u'There is no file in repo: `{}` at path `{}` for commit: `{}`'.format(
599 raise JSONRPCError(
595 repo.repo_name, file_path, commit_id))
600 f'There is no file in repo: `{repo.repo_name}` at path `{file_path}` for commit: `{commit_id}`')
596 except Exception:
601 except Exception:
597 log.exception(u"Exception occurred while trying to get repo %s file",
602 log.exception("Exception occurred while trying to get repo %s file",
598 repo.repo_name)
603 repo.repo_name)
599 raise JSONRPCError(u'failed to get repo: `{}` file at path {}'.format(
604 raise JSONRPCError(f'failed to get repo: `{repo.repo_name}` file at path {file_path}')
600 repo.repo_name, file_path))
601
605
602 return node
606 return node
603
607
@@ -619,11 +623,11 b' def get_repo_fts_tree(request, apiuser, '
619 validate_repo_permissions(apiuser, repoid, repo, _perms)
623 validate_repo_permissions(apiuser, repoid, repo, _perms)
620
624
621 repo_id = repo.repo_id
625 repo_id = repo.repo_id
622 cache_seconds = safe_int(rhodecode.CONFIG.get('rc_cache.cache_repo.expiration_time'))
626 cache_seconds = rhodecode.ConfigGet().get_int('rc_cache.cache_repo.expiration_time')
623 cache_on = cache_seconds > 0
627 cache_on = cache_seconds > 0
624
628
625 cache_namespace_uid = 'cache_repo.{}'.format(repo_id)
629 cache_namespace_uid = 'repo.{}'.format(repo_id)
626 region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid)
630 rc_cache.get_or_create_region('cache_repo', cache_namespace_uid)
627
631
628 def compute_fts_tree(cache_ver, repo_id, commit_id, root_path):
632 def compute_fts_tree(cache_ver, repo_id, commit_id, root_path):
629 return ScmModel().get_fts_data(repo_id, commit_id, root_path)
633 return ScmModel().get_fts_data(repo_id, commit_id, root_path)
@@ -631,11 +635,11 b' def get_repo_fts_tree(request, apiuser, '
631 try:
635 try:
632 # check if repo is not empty by any chance, skip quicker if it is.
636 # check if repo is not empty by any chance, skip quicker if it is.
633 _scm = repo.scm_instance()
637 _scm = repo.scm_instance()
634 if _scm.is_empty():
638 if not _scm or _scm.is_empty():
635 return []
639 return []
636 except RepositoryError:
640 except RepositoryError:
637 log.exception("Exception occurred while trying to get repo nodes")
641 log.exception("Exception occurred while trying to get repo nodes")
638 raise JSONRPCError('failed to get repo: `%s` nodes' % repo.repo_name)
642 raise JSONRPCError(f'failed to get repo: `{repo.repo_name}` nodes')
639
643
640 try:
644 try:
641 # we need to resolve commit_id to a FULL sha for cache to work correctly.
645 # we need to resolve commit_id to a FULL sha for cache to work correctly.
@@ -647,6 +651,7 b' def get_repo_fts_tree(request, apiuser, '
647 repo_id, commit_id, cache_on, cache_seconds or 0))
651 repo_id, commit_id, cache_on, cache_seconds or 0))
648
652
649 tree_files = compute_fts_tree(rc_cache.FILE_TREE_CACHE_VER, repo_id, commit_id, root_path)
653 tree_files = compute_fts_tree(rc_cache.FILE_TREE_CACHE_VER, repo_id, commit_id, root_path)
654
650 return tree_files
655 return tree_files
651
656
652 except Exception:
657 except Exception:
@@ -1869,7 +1874,11 b' def edit_comment(request, apiuser, messa'
1869 raise JSONRPCError('userid is not the same as your user')
1874 raise JSONRPCError('userid is not the same as your user')
1870
1875
1871 comment_author = comment.author.user_id == auth_user.user_id
1876 comment_author = comment.author.user_id == auth_user.user_id
1872 if not (comment.immutable is False and (is_super_admin or is_repo_admin) or comment_author):
1877
1878 if comment.immutable:
1879 raise JSONRPCError("Immutable comment cannot be edited")
1880
1881 if not (is_super_admin or is_repo_admin or comment_author):
1873 raise JSONRPCError("you don't have access to edit this comment")
1882 raise JSONRPCError("you don't have access to edit this comment")
1874
1883
1875 try:
1884 try:
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -342,8 +342,9 b' def get_method(request, apiuser, pattern'
342 matches = find_methods(request.registry.jsonrpc_methods, pattern)
342 matches = find_methods(request.registry.jsonrpc_methods, pattern)
343
343
344 args_desc = []
344 args_desc = []
345 if len(matches) == 1:
345 matches_keys = list(matches.keys())
346 func = matches[matches.keys()[0]]
346 if len(matches_keys) == 1:
347 func = matches[matches_keys[0]]
347
348
348 argspec = inspect.getargspec(func)
349 argspec = inspect.getargspec(func)
349 arglist = argspec[0]
350 arglist = argspec[0]
@@ -356,7 +357,7 b' def get_method(request, apiuser, pattern'
356 reversed(arglist), reversed(defaults), fillvalue=default_empty))
357 reversed(arglist), reversed(defaults), fillvalue=default_empty))
357 args_desc.append(func_kwargs)
358 args_desc.append(func_kwargs)
358
359
359 return matches.keys() + args_desc
360 return matches_keys + args_desc
360
361
361
362
362 @jsonrpc_method()
363 @jsonrpc_method()
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
@@ -1,4 +1,4 b''
1 # -*- coding: utf-8 -*-
1
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
General Comments 0
You need to be logged in to leave comments. Login now