blob: 786ab96667d5dad7ee839433c9b0ee9699f59ba3 [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"""Helper functions for component-related servlets."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import collections
12import logging
13import re
14
15from proto import tracker_pb2
16from tracker import tracker_bizobj
17
18
19ParsedComponentDef = collections.namedtuple(
20 'ParsedComponentDef',
21 'leaf_name, docstring, deprecated, '
22 'admin_usernames, cc_usernames, admin_ids, cc_ids, '
23 'label_strs, label_ids')
24
25
26def ParseComponentRequest(mr, post_data, services):
27 """Parse the user's request to create or update a component definition.
28
29 If an error is encountered then this function populates mr.errors
30 """
31 leaf_name = post_data.get('leaf_name', '')
32 docstring = post_data.get('docstring', '')
33 deprecated = 'deprecated' in post_data
34
35 admin_usernames = [
36 uname.strip() for uname in re.split('[,;\s]+', post_data['admins'])
37 if uname.strip()]
38 cc_usernames = [
39 uname.strip() for uname in re.split('[,;\s]+', post_data['cc'])
40 if uname.strip()]
41 all_user_ids = services.user.LookupUserIDs(
42 mr.cnxn, admin_usernames + cc_usernames, autocreate=True)
43
44 admin_ids = []
45 for admin_name in admin_usernames:
46 if admin_name not in all_user_ids:
47 mr.errors.member_admins = '%s unrecognized' % admin_name
48 continue
49 admin_id = all_user_ids[admin_name]
50 if admin_id not in admin_ids:
51 admin_ids.append(admin_id)
52
53 cc_ids = []
54 for cc_name in cc_usernames:
55 if cc_name not in all_user_ids:
56 mr.errors.member_cc = '%s unrecognized' % cc_name
57 continue
58 cc_id = all_user_ids[cc_name]
59 if cc_id not in cc_ids:
60 cc_ids.append(cc_id)
61
62 label_strs = [
63 lab.strip() for lab in re.split('[,;\s]+', post_data['labels'])
64 if lab.strip()]
65
66 label_ids = services.config.LookupLabelIDs(
67 mr.cnxn, mr.project_id, label_strs, autocreate=True)
68
69 return ParsedComponentDef(
70 leaf_name, docstring, deprecated,
71 admin_usernames, cc_usernames, admin_ids, cc_ids,
72 label_strs, label_ids)
73
74
75def GetComponentCcIDs(issue, config):
76 """Return auto-cc'd users for any component or ancestor the issue is in."""
77 result = set()
78 for component_id in issue.component_ids:
79 cd = tracker_bizobj.FindComponentDefByID(component_id, config)
80 if cd:
81 result.update(GetCcIDsForComponentAndAncestors(config, cd))
82
83 return result
84
85
86def GetCcIDsForComponentAndAncestors(config, cd):
87 """Return auto-cc'd user IDs for the given component and ancestors."""
88 result = set(cd.cc_ids)
89 ancestors = tracker_bizobj.FindAncestorComponents(config, cd)
90 for anc_cd in ancestors:
91 result.update(anc_cd.cc_ids)
92
93 return result