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