OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered 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.""" |
| 7 |
| 8 import collections |
| 9 import logging |
| 10 import re |
| 11 |
| 12 from tracker import tracker_bizobj |
| 13 |
| 14 |
| 15 ParsedComponentDef = collections.namedtuple( |
| 16 'ParsedComponentDef', |
| 17 'leaf_name, docstring, deprecated, ' |
| 18 'admin_usernames, cc_usernames, admin_ids, cc_ids') |
| 19 |
| 20 |
| 21 def ParseComponentRequest(mr, post_data, user_service): |
| 22 """Parse the user's request to create or update a component definition. |
| 23 |
| 24 If an error is encountered then this function populates mr.errors |
| 25 """ |
| 26 leaf_name = post_data.get('leaf_name', '') |
| 27 docstring = post_data.get('docstring', '') |
| 28 deprecated = 'deprecated' in post_data |
| 29 |
| 30 admin_usernames = [ |
| 31 uname.strip() for uname in re.split('[,;\s]+', post_data['admins']) |
| 32 if uname.strip()] |
| 33 cc_usernames = [ |
| 34 uname.strip() for uname in re.split('[,;\s]+', post_data['cc']) |
| 35 if uname.strip()] |
| 36 all_user_ids = user_service.LookupUserIDs( |
| 37 mr.cnxn, admin_usernames + cc_usernames, autocreate=True) |
| 38 |
| 39 admin_ids = [] |
| 40 for admin_name in admin_usernames: |
| 41 if admin_name not in all_user_ids: |
| 42 mr.errors.member_admins = '%s unrecognized' % admin_name |
| 43 continue |
| 44 admin_id = all_user_ids[admin_name] |
| 45 if admin_id not in admin_ids: |
| 46 admin_ids.append(admin_id) |
| 47 |
| 48 cc_ids = [] |
| 49 for cc_name in cc_usernames: |
| 50 if cc_name not in all_user_ids: |
| 51 mr.errors.member_cc = '%s unrecognized' % cc_name |
| 52 continue |
| 53 cc_id = all_user_ids[cc_name] |
| 54 if cc_id not in cc_ids: |
| 55 cc_ids.append(cc_id) |
| 56 |
| 57 return ParsedComponentDef( |
| 58 leaf_name, docstring, deprecated, |
| 59 admin_usernames, cc_usernames, admin_ids, cc_ids) |
| 60 |
| 61 |
| 62 def GetComponentCcIDs(issue, config): |
| 63 """Return auto-cc'd users for any component or ancestor the issue is in.""" |
| 64 result = set() |
| 65 for component_id in issue.component_ids: |
| 66 cd = tracker_bizobj.FindComponentDefByID(component_id, config) |
| 67 if cd: |
| 68 result.update(GetCcIDsForComponentAndAncestors(config, cd)) |
| 69 |
| 70 return result |
| 71 |
| 72 |
| 73 def GetCcIDsForComponentAndAncestors(config, cd): |
| 74 """Return auto-cc'd user IDs for the given component and ancestors.""" |
| 75 result = set(cd.cc_ids) |
| 76 ancestors = tracker_bizobj.FindAncestorComponents(config, cd) |
| 77 for anc_cd in ancestors: |
| 78 result.update(anc_cd.cc_ids) |
| 79 |
| 80 return result |
OLD | NEW |