Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Helper functions for component-related servlets.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import collections |
| 11 | import logging |
| 12 | import re |
| 13 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 14 | from mrproto import tracker_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 15 | from tracker import tracker_bizobj |
| 16 | |
| 17 | |
| 18 | ParsedComponentDef = 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 | |
| 25 | def 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 35 | uname.strip() |
| 36 | for uname in re.split(r'[,;\s]+', post_data['admins']) |
| 37 | if uname.strip() |
| 38 | ] |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 39 | cc_usernames = [ |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 40 | uname.strip() |
| 41 | for uname in re.split(r'[,;\s]+', post_data['cc']) |
| 42 | if uname.strip() |
| 43 | ] |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 44 | 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 54 | admin_ids.append(admin_id) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 55 | |
| 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 66 | lab.strip() |
| 67 | for lab in re.split(r'[,;\s]+', post_data['labels']) |
| 68 | if lab.strip() |
| 69 | ] |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 70 | |
| 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 | |
| 80 | def 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 | |
| 91 | def 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 |