blob: a98da55d305d284704539ba6a818bf5909dcb47e [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Helper functions for component-related servlets."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import collections
11import logging
12import re
13
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010014from mrproto import tracker_pb2
Copybara854996b2021-09-07 19:36:02 +000015from tracker import tracker_bizobj
16
17
18ParsedComponentDef = collections.namedtuple(
19 'ParsedComponentDef',
20 'leaf_name, docstring, deprecated, '
21 'admin_usernames, cc_usernames, admin_ids, cc_ids, '
22 'label_strs, label_ids')
23
24
25def ParseComponentRequest(mr, post_data, services):
26 """Parse the user's request to create or update a component definition.
27
28 If an error is encountered then this function populates mr.errors
29 """
30 leaf_name = post_data.get('leaf_name', '')
31 docstring = post_data.get('docstring', '')
32 deprecated = 'deprecated' in post_data
33
34 admin_usernames = [
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010035 uname.strip()
36 for uname in re.split(r'[,;\s]+', post_data['admins'])
37 if uname.strip()
38 ]
Copybara854996b2021-09-07 19:36:02 +000039 cc_usernames = [
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010040 uname.strip()
41 for uname in re.split(r'[,;\s]+', post_data['cc'])
42 if uname.strip()
43 ]
Copybara854996b2021-09-07 19:36:02 +000044 all_user_ids = services.user.LookupUserIDs(
45 mr.cnxn, admin_usernames + cc_usernames, autocreate=True)
46
47 admin_ids = []
48 for admin_name in admin_usernames:
49 if admin_name not in all_user_ids:
50 mr.errors.member_admins = '%s unrecognized' % admin_name
51 continue
52 admin_id = all_user_ids[admin_name]
53 if admin_id not in admin_ids:
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010054 admin_ids.append(admin_id)
Copybara854996b2021-09-07 19:36:02 +000055
56 cc_ids = []
57 for cc_name in cc_usernames:
58 if cc_name not in all_user_ids:
59 mr.errors.member_cc = '%s unrecognized' % cc_name
60 continue
61 cc_id = all_user_ids[cc_name]
62 if cc_id not in cc_ids:
63 cc_ids.append(cc_id)
64
65 label_strs = [
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010066 lab.strip()
67 for lab in re.split(r'[,;\s]+', post_data['labels'])
68 if lab.strip()
69 ]
Copybara854996b2021-09-07 19:36:02 +000070
71 label_ids = services.config.LookupLabelIDs(
72 mr.cnxn, mr.project_id, label_strs, autocreate=True)
73
74 return ParsedComponentDef(
75 leaf_name, docstring, deprecated,
76 admin_usernames, cc_usernames, admin_ids, cc_ids,
77 label_strs, label_ids)
78
79
80def GetComponentCcIDs(issue, config):
81 """Return auto-cc'd users for any component or ancestor the issue is in."""
82 result = set()
83 for component_id in issue.component_ids:
84 cd = tracker_bizobj.FindComponentDefByID(component_id, config)
85 if cd:
86 result.update(GetCcIDsForComponentAndAncestors(config, cd))
87
88 return result
89
90
91def GetCcIDsForComponentAndAncestors(config, cd):
92 """Return auto-cc'd user IDs for the given component and ancestors."""
93 result = set(cd.cc_ids)
94 ancestors = tracker_bizobj.FindAncestorComponents(config, cd)
95 for anc_cd in ancestors:
96 result.update(anc_cd.cc_ids)
97
98 return result