##// END OF EJS Templates
reviewers: fixed serialization problems from returned map object
super-admin -
r5119:34dd81a5 default
parent child Browse files
Show More
@@ -1,111 +1,111 b''
1 1 # Copyright (C) 2016-2023 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 from rhodecode.lib import helpers as h, rc_cache
20 20 from rhodecode.lib.utils2 import safe_int
21 21 from rhodecode.model.pull_request import get_diff_info
22 22 from rhodecode.model.db import PullRequestReviewers
23 23 # V3 - Reviewers, with default rules data
24 24 # v4 - Added observers metadata
25 25 # v5 - pr_author/commit_author include/exclude logic
26 26 REVIEWER_API_VERSION = 'V5'
27 27
28 28
29 29 def reviewer_as_json(user, reasons=None, role=None, mandatory=False, rules=None, user_group=None):
30 30 """
31 31 Returns json struct of a reviewer for frontend
32 32
33 33 :param user: the reviewer
34 34 :param reasons: list of strings of why they are reviewers
35 35 :param mandatory: bool, to set user as mandatory
36 36 """
37 37 role = role or PullRequestReviewers.ROLE_REVIEWER
38 38 if role not in PullRequestReviewers.ROLES:
39 39 raise ValueError('role is not one of %s', PullRequestReviewers.ROLES)
40 40
41 41 return {
42 42 'user_id': user.user_id,
43 43 'reasons': reasons or [],
44 44 'rules': rules or [],
45 45 'role': role,
46 46 'mandatory': mandatory,
47 47 'user_group': user_group,
48 48 'username': user.username,
49 49 'first_name': user.first_name,
50 50 'last_name': user.last_name,
51 51 'user_link': h.link_to_user(user),
52 52 'gravatar_link': h.gravatar_url(user.email, 14),
53 53 }
54 54
55 55
56 56 def to_reviewers(e):
57 57 if isinstance(e, (tuple, list)):
58 return map(reviewer_as_json, e)
58 return list(map(reviewer_as_json, e))
59 59 else:
60 60 return reviewer_as_json(e)
61 61
62 62
63 63 def get_default_reviewers_data(current_user, source_repo, source_ref, target_repo, target_ref,
64 64 include_diff_info=True):
65 65 """
66 66 Return json for default reviewers of a repository
67 67 """
68 68
69 69 diff_info = {}
70 70 if include_diff_info:
71 71 diff_info = get_diff_info(
72 72 source_repo, source_ref.commit_id, target_repo, target_ref.commit_id)
73 73
74 74 reasons = ['Default reviewer', 'Repository owner']
75 75 json_reviewers = [reviewer_as_json(
76 76 user=target_repo.user, reasons=reasons, mandatory=False, rules=None, role=None)]
77 77
78 78 compute_key = rc_cache.utils.compute_key_from_params(
79 79 current_user.user_id, source_repo.repo_id, source_ref.type, source_ref.name,
80 80 source_ref.commit_id, target_repo.repo_id, target_ref.type, target_ref.name,
81 81 target_ref.commit_id)
82 82
83 83 return {
84 84 'api_ver': REVIEWER_API_VERSION, # define version for later possible schema upgrade
85 85 'compute_key': compute_key,
86 86 'diff_info': diff_info,
87 87 'reviewers': json_reviewers,
88 88 'rules': {},
89 89 'rules_data': {},
90 90 'rules_humanized': [],
91 91 }
92 92
93 93
94 94 def validate_default_reviewers(review_members, reviewer_rules):
95 95 """
96 96 Function to validate submitted reviewers against the saved rules
97 97 """
98 98 reviewers = []
99 99 reviewer_by_id = {}
100 100 for r in review_members:
101 101 reviewer_user_id = safe_int(r['user_id'])
102 102 entry = (reviewer_user_id, r['reasons'], r['mandatory'], r['role'], r['rules'])
103 103
104 104 reviewer_by_id[reviewer_user_id] = entry
105 105 reviewers.append(entry)
106 106
107 107 return reviewers
108 108
109 109
110 110 def validate_observers(observer_members, reviewer_rules):
111 111 return {}
General Comments 0
You need to be logged in to leave comments. Login now