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