Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2020 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 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | from api import resource_name_converters as rnc |
| 11 | from api.v3 import monorail_servicer |
| 12 | from api.v3.api_proto import frontend_pb2 |
| 13 | from api.v3.api_proto import project_objects_pb2 |
| 14 | from api.v3.api_proto import frontend_prpc_pb2 |
| 15 | from businesslogic import work_env |
| 16 | |
| 17 | |
| 18 | class FrontendServicer(monorail_servicer.MonorailServicer): |
| 19 | """Handle frontend specific API requests. |
| 20 | Each API request is implemented with a method as defined in the |
| 21 | .proto file. Each method does any request-specific validation, uses work_env |
| 22 | to safely operate on business objects, and returns a response proto. |
| 23 | """ |
| 24 | |
| 25 | DESCRIPTION = frontend_prpc_pb2.FrontendServiceDescription |
| 26 | |
| 27 | @monorail_servicer.PRPCMethod |
| 28 | def GatherProjectEnvironment(self, mc, request): |
| 29 | # type: (MonorailContext, GatherProjectEnvironmentRequest) -> |
| 30 | # GatherProjectEnvironmentResponse |
| 31 | """pRPC API method that implements GatherProjectEnvironment. |
| 32 | |
| 33 | Raises: |
| 34 | InputException if the project resource name provided is invalid. |
| 35 | NoSuchProjectException if the parent project is not found. |
| 36 | PermissionException if user is not allowed to view this project. |
| 37 | """ |
| 38 | |
| 39 | project_id = rnc.IngestProjectName(mc.cnxn, request.parent, self.services) |
| 40 | |
| 41 | with work_env.WorkEnv(mc, self.services) as we: |
| 42 | project = we.GetProject(project_id) |
| 43 | project_config = we.GetProjectConfig(project_id) |
| 44 | |
| 45 | api_project = self.converter.ConvertProject(project) |
| 46 | api_project_config = self.converter.ConvertProjectConfig(project_config) |
| 47 | api_status_defs = self.converter.ConvertStatusDefs( |
| 48 | project_config.well_known_statuses, project_id) |
| 49 | api_label_defs = self.converter.ConvertLabelDefs( |
| 50 | project_config.well_known_labels, project_id) |
| 51 | api_component_defs = self.converter.ConvertComponentDefs( |
| 52 | project_config.component_defs, project_id) |
| 53 | api_field_defs = self.converter.ConvertFieldDefs( |
| 54 | project_config.field_defs, project_id) |
| 55 | api_approval_defs = self.converter.ConvertApprovalDefs( |
| 56 | project_config.approval_defs, project_id) |
| 57 | saved_queries = self.services.features.GetCannedQueriesByProjectID( |
| 58 | mc.cnxn, project_id) |
| 59 | api_sqs = self.converter.ConvertProjectSavedQueries( |
| 60 | saved_queries, project_id) |
| 61 | |
| 62 | return frontend_pb2.GatherProjectEnvironmentResponse( |
| 63 | project=api_project, |
| 64 | project_config=api_project_config, |
| 65 | statuses=api_status_defs, |
| 66 | well_known_labels=api_label_defs, |
| 67 | components=api_component_defs, |
| 68 | fields=api_field_defs, |
| 69 | approval_fields=api_approval_defs, |
| 70 | saved_queries=api_sqs) |
| 71 | |
| 72 | @monorail_servicer.PRPCMethod |
| 73 | def GatherProjectMembershipsForUser(self, mc, request): |
| 74 | # type: (MonorailContext, GatherProjectMembershipsForUserRequest) -> |
| 75 | # GatherProjectMembershipsForUserResponse |
| 76 | """pRPC API method that implements GatherProjectMembershipsForUser. |
| 77 | |
| 78 | Raises: |
| 79 | NoSuchUserException if the user is not found. |
| 80 | InputException if the user resource name is invalid. |
| 81 | """ |
| 82 | |
| 83 | user_id = rnc.IngestUserName(mc.cnxn, request.user, self.services) |
| 84 | |
| 85 | project_memberships = [] |
| 86 | |
| 87 | with work_env.WorkEnv(mc, self.services) as we: |
| 88 | owner, committer, contributor = we.GatherProjectMembershipsForUser( |
| 89 | user_id) |
| 90 | |
| 91 | for project_id in owner: |
| 92 | project_member = self.converter.CreateProjectMember( |
| 93 | mc.cnxn, project_id, user_id, 'OWNER') |
| 94 | project_memberships.append(project_member) |
| 95 | |
| 96 | for project_id in committer: |
| 97 | project_member = self.converter.CreateProjectMember( |
| 98 | mc.cnxn, project_id, user_id, 'COMMITTER') |
| 99 | project_memberships.append(project_member) |
| 100 | |
| 101 | for project_id in contributor: |
| 102 | project_member = self.converter.CreateProjectMember( |
| 103 | mc.cnxn, project_id, user_id, 'CONTRIBUTOR') |
| 104 | project_memberships.append(project_member) |
| 105 | |
| 106 | return frontend_pb2.GatherProjectMembershipsForUserResponse( |
| 107 | project_memberships=project_memberships) |