blob: e315442a134443ff7b5740fee7b6252484598aa5 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Servlet to export a project's config in JSON format.
7"""
8from __future__ import print_function
9from __future__ import division
10from __future__ import absolute_import
11
12import logging
13import time
14
15import ezt
16
17from framework import permissions
18from framework import jsonfeed
19from framework import servlet
20from project import project_helpers
21from tracker import tracker_bizobj
22
23
24class ProjectExport(servlet.Servlet):
25 """Only site admins can export a project"""
26
27 _PAGE_TEMPLATE = 'project/project-export-page.ezt'
28 _MAIN_TAB_MODE = servlet.Servlet.MAIN_TAB_ADMIN
29
30 def AssertBasePermission(self, mr):
31 """Make sure that the logged in user has permission to view this page."""
32 super(ProjectExport, self).AssertBasePermission(mr)
33 if not mr.auth.user_pb.is_site_admin:
34 raise permissions.PermissionException(
35 'Only site admins may export project configuration')
36
37 def GatherPageData(self, mr):
38 """Build up a dictionary of data values to use when rendering the page."""
39
40 return {
41 'admin_tab_mode': None,
42 'page_perms': self.MakePagePerms(mr, None, permissions.CREATE_ISSUE),
43 }
44
45
46class ProjectExportJSON(jsonfeed.JsonFeed):
47 """ProjectExportJSON shows all configuration for a Project in JSON form."""
48
49 # Pretty-print the JSON output.
50 JSON_INDENT = 4
51
52 def AssertBasePermission(self, mr):
53 """Make sure that the logged in user has permission to view this page."""
54 super(ProjectExportJSON, self).AssertBasePermission(mr)
55 if not mr.auth.user_pb.is_site_admin:
56 raise permissions.PermissionException(
57 'Only site admins may export project configuration')
58
59 def HandleRequest(self, mr):
60 """Build up a dictionary of data values to use when rendering the page.
61
62 Args:
63 mr: commonly used info parsed from the request.
64
65 Returns:
66 Dict of values used by EZT for rendering the page.
67 """
68 project = self.services.project.GetProject(mr.cnxn, mr.project.project_id)
69 user_id_set = project_helpers.UsersInvolvedInProject(project)
70
71 config = self.services.config.GetProjectConfig(
72 mr.cnxn, mr.project.project_id)
73 templates = self.services.template.GetProjectTemplates(
74 mr.cnxn, config.project_id)
75 involved_users = self.services.config.UsersInvolvedInConfig(
76 config, templates)
77 user_id_set.update(involved_users)
78
79 # The value 0 indicates "no user", e.g., that an issue has no owner.
80 # We don't need to create a User row to represent that.
81 user_id_set.discard(0)
82 email_dict = self.services.user.LookupUserEmails(mr.cnxn, user_id_set)
83
84 project_json = self._MakeProjectJSON(project, email_dict)
85 config_json = self._MakeConfigJSON(config, email_dict, templates)
86
87 json_data = {
88 'metadata': {
89 'version': 1,
90 'when': int(time.time()),
91 'who': mr.auth.email,
92 },
93 'project': project_json,
94 'config': config_json,
95 # This list could be derived from the others, but we provide it for
96 # ease of processing.
97 'emails': list(email_dict.values()),
98 }
99 return json_data
100
101 def _MakeProjectJSON(self, project, email_dict):
102 project_json = {
103 'name': project.project_name,
104 'summary': project.summary,
105 'description': project.description,
106 'state': project.state.name,
107 'access': project.access.name,
108 'owners': [email_dict.get(user) for user in project.owner_ids],
109 'committers': [email_dict.get(user) for user in project.committer_ids],
110 'contributors': [
111 email_dict.get(user) for user in project.contributor_ids],
112 'perms': [self._MakePermJSON(perm, email_dict)
113 for perm in project.extra_perms],
114 'issue_notify_address': project.issue_notify_address,
115 'attachment_bytes': project.attachment_bytes_used,
116 'attachment_quota': project.attachment_quota,
117 'recent_activity': project.recent_activity,
118 'process_inbound_email': project.process_inbound_email,
119 'only_owners_remove_restrictions':
120 project.only_owners_remove_restrictions,
121 'only_owners_see_contributors': project.only_owners_see_contributors,
122 'revision_url_format': project.revision_url_format,
123 'read_only_reason': project.read_only_reason,
124 }
125 return project_json
126
127 def _MakePermJSON(self, perm, email_dict):
128 perm_json = {
129 'member': email_dict.get(perm.member_id),
130 'perms': [p for p in perm.perms],
131 }
132 return perm_json
133
134 def _MakeConfigJSON(self, config, email_dict, project_templates):
135 config_json = {
136 'statuses':
137 [self._MakeStatusJSON(status)
138 for status in config.well_known_statuses],
139 'statuses_offer_merge':
140 [status for status in config.statuses_offer_merge],
141 'labels':
142 [self._MakeLabelJSON(label) for label in config.well_known_labels],
143 'exclusive_label_prefixes':
144 [label for label in config.exclusive_label_prefixes],
145 # TODO(http://crbug.com/monorail/7217): Export the project's FieldDefs.
146 'components':
147 [self._MakeComponentJSON(component, email_dict)
148 for component in config.component_defs],
149 'templates':
150 [self._MakeTemplateJSON(template, email_dict)
151 for template in project_templates],
152 'developer_template': config.default_template_for_developers,
153 'user_template': config.default_template_for_users,
154 'list_cols': config.default_col_spec,
155 'list_spec': config.default_sort_spec,
156 'grid_x': config.default_x_attr,
157 'grid_y': config.default_y_attr,
158 'only_known_values': config.restrict_to_known,
159 }
160 if config.custom_issue_entry_url:
161 config_json.update({'issue_entry_url': config.custom_issue_entry_url})
162 return config_json
163
164 def _MakeTemplateJSON(self, template, email_dict):
165 template_json = {
166 'name': template.name,
167 'summary': template.summary,
168 'content': template.content,
169 'summary_must_be_edited': template.summary_must_be_edited,
170 'owner': email_dict.get(template.owner_id),
171 'status': template.status,
172 'labels': [label for label in template.labels],
173 # TODO(http://crbug.com/monorail/7217): Export the template's Fields.
174 'members_only': template.members_only,
175 'owner_defaults_to_member': template.owner_defaults_to_member,
176 'component_required': template.component_required,
177 'admins': [email_dict(user) for user in template.admin_ids],
178 }
179 return template_json
180
181 def _MakeStatusJSON(self, status):
182 status_json = {
183 'status': status.status,
184 'open': status.means_open,
185 'docstring': status.status_docstring,
186 }
187 return status_json
188
189 def _MakeLabelJSON(self, label):
190 label_json = {
191 'label': label.label,
192 'docstring': label.label_docstring,
193 }
194 return label_json
195
196 def _MakeComponentJSON(self, component, email_dict):
197 component_json = {
198 'path': component.path,
199 'docstring': component.docstring,
200 'admins': [email_dict.get(user) for user in component.admin_ids],
201 'ccs': [email_dict.get(user) for user in component.cc_ids],
202 }
203 return component_json