blob: 1e0289b13a7f29e1a4ec5835be9135d51b352a55 [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 import a file of issues in JSON format.
7"""
8from __future__ import print_function
9from __future__ import division
10from __future__ import absolute_import
11
12import collections
13import json
14import logging
15import time
16
17import ezt
18
19from features import filterrules_helpers
20from framework import framework_helpers
21from framework import jsonfeed
22from framework import permissions
23from framework import servlet
24from framework import urls
25from proto import tracker_pb2
26
27
28ParserState = collections.namedtuple(
29 'ParserState',
30 'user_id_dict, nonexist_emails, issue_list, comments_dict, starrers_dict, '
31 'relations_dict')
32
33
34class IssueImport(servlet.Servlet):
35 """IssueImport loads a file of issues in JSON format."""
36
37 _PAGE_TEMPLATE = 'tracker/issue-import-page.ezt'
38 _MAIN_TAB_MODE = servlet.Servlet.MAIN_TAB_ISSUES
39
40 def AssertBasePermission(self, mr):
41 """Make sure that the logged in user has permission to view this page."""
42 super(IssueImport, self).AssertBasePermission(mr)
43 if not mr.auth.user_pb.is_site_admin:
44 raise permissions.PermissionException(
45 'Only site admins may import issues')
46
47 def GatherPageData(self, mr):
48 """Build up a dictionary of data values to use when rendering the page."""
49 return {
50 'issue_tab_mode': None,
51 'page_perms': self.MakePagePerms(mr, None, permissions.CREATE_ISSUE),
52 'import_errors': [],
53 }
54
55 def ProcessFormData(self, mr, post_data):
56 """Process the issue entry form.
57
58 Args:
59 mr: commonly used info parsed from the request.
60 post_data: The post_data dict for the current request.
61
62 Returns:
63 String URL to redirect the user to after processing.
64 """
65 import_errors = []
66 json_data = None
67
68 pre_check_only = 'pre_check_only' in post_data
69
70 uploaded_file = post_data.get('jsonfile')
71 if uploaded_file is None:
72 import_errors.append('No file uploaded')
73 else:
74 try:
75 json_str = uploaded_file.value
76 if json_str.startswith(jsonfeed.XSSI_PREFIX):
77 json_str = json_str[len(jsonfeed.XSSI_PREFIX):]
78 json_data = json.loads(json_str)
79 except ValueError:
80 import_errors.append('error parsing JSON in file')
81
82 if uploaded_file and not json_data:
83 import_errors.append('JSON file was empty')
84
85 # Note that the project must already exist in order to even reach
86 # this servlet because it is hosted in the context of a project.
87 if json_data and mr.project_name != json_data['metadata']['project']:
88 import_errors.append(
89 'Project name does not match. '
90 'Edit the file if you want to import into this project anyway.')
91
92 if import_errors:
93 return self.PleaseCorrect(mr, import_errors=import_errors)
94
95 event_log = [] # We accumulate a list of messages to display to the user.
96
97 try:
98 # First we parse the JSON into objects, but we don't have DB IDs yet.
99 state = self._ParseObjects(mr.cnxn, mr.project_id, json_data, event_log)
100 # If that worked, go ahead and start saving the data to the DB.
101 if not pre_check_only:
102 self._SaveObjects(mr.cnxn, mr.project_id, state, event_log)
103 except JSONImportError:
104 # just report it to the user by displaying event_log
105 event_log.append('Aborted import processing')
106
107 # This is a little bit of a hack because it always uses the form validation
108 # error message display logic to show the results of this import run,
109 # which may include errors or not.
110 return self.PleaseCorrect(mr, import_errors=event_log)
111
112 def _ParseObjects(self, cnxn, project_id, json_data, event_log):
113 """Examine JSON data and return a parser state for further processing."""
114 # Decide which users need to be created.
115 needed_emails = json_data['emails']
116 user_id_dict = self.services.user.LookupExistingUserIDs(cnxn, needed_emails)
117 nonexist_emails = [email for email in needed_emails
118 if email not in user_id_dict]
119
120 event_log.append('Need to create %d users: %r' %
121 (len(nonexist_emails), nonexist_emails))
122 user_id_dict.update({
123 email.lower(): framework_helpers.MurmurHash3_x86_32(email.lower())
124 for email in nonexist_emails})
125
126 num_comments = 0
127 num_stars = 0
128 issue_list = []
129 comments_dict = collections.defaultdict(list)
130 starrers_dict = collections.defaultdict(list)
131 relations_dict = collections.defaultdict(list)
132 for issue_json in json_data.get('issues', []):
133 issue, comment_list, starrer_list, relation_list = self._ParseIssue(
134 cnxn, project_id, user_id_dict, issue_json, event_log)
135 issue_list.append(issue)
136 comments_dict[issue.local_id] = comment_list
137 starrers_dict[issue.local_id] = starrer_list
138 relations_dict[issue.local_id] = relation_list
139 num_comments += len(comment_list)
140 num_stars += len(starrer_list)
141
142 event_log.append(
143 'Found info for %d issues: %r' %
144 (len(issue_list), sorted([issue.local_id for issue in issue_list])))
145
146 event_log.append(
147 'Found %d total comments for %d issues' %
148 (num_comments, len(comments_dict)))
149
150 event_log.append(
151 'Found %d total stars for %d issues' %
152 (num_stars, len(starrers_dict)))
153
154 event_log.append(
155 'Found %d total relationships.' %
156 sum((len(dsts) for dsts in relations_dict.values())))
157
158 event_log.append('Parsing phase finished OK')
159 return ParserState(
160 user_id_dict, nonexist_emails, issue_list,
161 comments_dict, starrers_dict, relations_dict)
162
163 def _ParseIssue(self, cnxn, project_id, user_id_dict, issue_json, event_log):
164 issue = tracker_pb2.Issue(
165 project_id=project_id,
166 local_id=issue_json['local_id'],
167 reporter_id=user_id_dict[issue_json['reporter']],
168 summary=issue_json['summary'],
169 opened_timestamp=issue_json['opened'],
170 modified_timestamp=issue_json['modified'],
171 cc_ids=[user_id_dict[cc_email]
172 for cc_email in issue_json.get('cc', [])
173 if cc_email in user_id_dict],
174 status=issue_json.get('status', ''),
175 labels=issue_json.get('labels', []),
176 field_values=[self._ParseFieldValue(cnxn, project_id, user_id_dict, field)
177 for field in issue_json.get('fields', [])])
178 if issue_json.get('owner'):
179 issue.owner_id = user_id_dict[issue_json['owner']]
180 if issue_json.get('closed'):
181 issue.closed_timestamp = issue_json['closed']
182 comments = [self._ParseComment(
183 project_id, user_id_dict, comment_json, event_log)
184 for comment_json in issue_json.get('comments', [])]
185
186 starrers = [user_id_dict[starrer] for starrer in issue_json['starrers']]
187
188 relations = []
189 relations.extend(
190 [(i, 'blockedon') for i in issue_json.get('blocked_on', [])])
191 relations.extend(
192 [(i, 'blocking') for i in issue_json.get('blocking', [])])
193 if 'merged_into' in issue_json:
194 relations.append((issue_json['merged_into'], 'mergedinto'))
195
196 return issue, comments, starrers, relations
197
198 def _ParseFieldValue(self, cnxn, project_id, user_id_dict, field_json):
199 field = tracker_pb2.FieldValue(
200 field_id=self.services.config.LookupFieldID(cnxn, project_id,
201 field_json['field']))
202 if 'int_value' in field_json:
203 field.int_value = field_json['int_value']
204 if 'str_value' in field_json:
205 field.str_value = field_json['str_value']
206 if 'user_value' in field_json:
207 field.user_value = user_id_dict.get(field_json['user_value'])
208
209 return field
210
211 def _ParseComment(self, project_id, user_id_dict, comment_json, event_log):
212 comment = tracker_pb2.IssueComment(
213 # Note: issue_id is filled in after the issue is saved.
214 project_id=project_id,
215 timestamp=comment_json['timestamp'],
216 user_id=user_id_dict[comment_json['commenter']],
217 content=comment_json.get('content'))
218
219 for amendment in comment_json['amendments']:
220 comment.amendments.append(
221 self._ParseAmendment(amendment, user_id_dict, event_log))
222
223 for attachment in comment_json['attachments']:
224 comment.attachments.append(
225 self._ParseAttachment(attachment, event_log))
226
227 if comment_json['description_num']:
228 comment.is_description = True
229
230 return comment
231
232 def _ParseAmendment(self, amendment_json, user_id_dict, _event_log):
233 amendment = tracker_pb2.Amendment(
234 field=tracker_pb2.FieldID(amendment_json['field']))
235
236 if 'new_value' in amendment_json:
237 amendment.newvalue = amendment_json['new_value']
238 if 'custom_field_name' in amendment_json:
239 amendment.custom_field_name = amendment_json['custom_field_name']
240 if 'added_users' in amendment_json:
241 amendment.added_user_ids.extend(
242 [user_id_dict[email] for email in amendment_json['added_users']])
243 if 'removed_users' in amendment_json:
244 amendment.removed_user_ids.extend(
245 [user_id_dict[email] for email in amendment_json['removed_users']])
246
247 return amendment
248
249 def _ParseAttachment(self, attachment_json, _event_log):
250 attachment = tracker_pb2.Attachment(
251 filename=attachment_json['name'],
252 filesize=attachment_json['size'],
253 mimetype=attachment_json['mimetype'],
254 gcs_object_id=attachment_json['gcs_object_id']
255 )
256 return attachment
257
258 def _SaveObjects(self, cnxn, project_id, state, event_log):
259 """Examine JSON data and create users, issues, and comments."""
260
261 created_user_ids = self.services.user.LookupUserIDs(
262 cnxn, state.nonexist_emails, autocreate=True)
263 for created_email, created_id in created_user_ids.items():
264 if created_id != state.user_id_dict[created_email]:
265 event_log.append('Mismatched user_id for %r' % created_email)
266 raise JSONImportError()
267 event_log.append('Created %d users' % len(state.nonexist_emails))
268
269 total_comments = 0
270 total_stars = 0
271 config = self.services.config.GetProjectConfig(cnxn, project_id)
272 for issue in state.issue_list:
273 # TODO(jrobbins): renumber issues if there is a local_id conflict.
274 if issue.local_id not in state.starrers_dict:
275 # Issues with stars will have filter rules applied in SetStar().
276 filterrules_helpers.ApplyFilterRules(
277 cnxn, self.services, issue, config)
278 issue_id = self.services.issue.InsertIssue(cnxn, issue)
279 for comment in state.comments_dict[issue.local_id]:
280 total_comments += 1
281 comment.issue_id = issue_id
282 self.services.issue.InsertComment(cnxn, comment)
283 self.services.issue_star.SetStarsBatch(
284 cnxn, self.services, config, issue_id,
285 state.starrers_dict[issue.local_id], True)
286 total_stars += len(state.starrers_dict[issue.local_id])
287
288 event_log.append('Created %d issues' % len(state.issue_list))
289 event_log.append('Created %d comments for %d issues' % (
290 total_comments, len(state.comments_dict)))
291 event_log.append('Set %d stars on %d issues' % (
292 total_stars, len(state.starrers_dict)))
293
294 global_relations_dict = collections.defaultdict(list)
295 for issue, rels in state.relations_dict.items():
296 src_iid = self.services.issue.GetIssueByLocalID(
297 cnxn, project_id, issue).issue_id
298 dst_iids = [i.issue_id for i in self.services.issue.GetIssuesByLocalIDs(
299 cnxn, project_id, [rel[0] for rel in rels])]
300 kinds = [rel[1] for rel in rels]
301 global_relations_dict[src_iid] = list(zip(dst_iids, kinds))
302 self.services.issue.RelateIssues(cnxn, global_relations_dict)
303
304 self.services.issue.SetUsedLocalID(cnxn, project_id)
305 event_log.append('Finished import')
306
307
308class JSONImportError(Exception):
309 """Exception to raise if imported JSON is invalid."""
310 pass