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