Line data Source code
1 : // Copyright (C) 2022 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.project; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.gerrit.extensions.common.SubmitRequirementInfo; 19 : import com.google.gerrit.extensions.restapi.AuthException; 20 : import com.google.gerrit.extensions.restapi.Response; 21 : import com.google.gerrit.extensions.restapi.RestReadView; 22 : import com.google.gerrit.server.CurrentUser; 23 : import com.google.gerrit.server.permissions.PermissionBackend; 24 : import com.google.gerrit.server.permissions.PermissionBackendException; 25 : import com.google.gerrit.server.permissions.ProjectPermission; 26 : import com.google.gerrit.server.project.ProjectResource; 27 : import com.google.gerrit.server.project.ProjectState; 28 : import com.google.gerrit.server.project.SubmitRequirementJson; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Provider; 31 : import java.util.ArrayList; 32 : import java.util.List; 33 : import org.kohsuke.args4j.Option; 34 : 35 : /** List submit requirements in a project. */ 36 : public class ListSubmitRequirements implements RestReadView<ProjectResource> { 37 : private final Provider<CurrentUser> user; 38 : private final PermissionBackend permissionBackend; 39 : 40 : @Inject 41 2 : public ListSubmitRequirements(Provider<CurrentUser> user, PermissionBackend permissionBackend) { 42 2 : this.user = user; 43 2 : this.permissionBackend = permissionBackend; 44 2 : } 45 : 46 : @Option(name = "--inherited", usage = "to include inherited submit requirements") 47 : private boolean inherited; 48 : 49 : public ListSubmitRequirements withInherited(boolean inherited) { 50 1 : this.inherited = inherited; 51 1 : return this; 52 : } 53 : 54 : @Override 55 : public Response<List<SubmitRequirementInfo>> apply(ProjectResource rsrc) 56 : throws AuthException, PermissionBackendException { 57 2 : if (!user.get().isIdentifiedUser()) { 58 1 : throw new AuthException("Authentication required"); 59 : } 60 : 61 2 : if (inherited) { 62 1 : List<SubmitRequirementInfo> allSubmitRequirements = new ArrayList<>(); 63 1 : for (ProjectState projectState : rsrc.getProjectState().treeInOrder()) { 64 : try { 65 1 : permissionBackend 66 1 : .currentUser() 67 1 : .project(projectState.getNameKey()) 68 1 : .check(ProjectPermission.READ_CONFIG); 69 1 : } catch (AuthException e) { 70 1 : throw new AuthException(projectState.getNameKey() + ": " + e.getMessage(), e); 71 1 : } 72 1 : allSubmitRequirements.addAll(listSubmitRequirements(projectState)); 73 1 : } 74 1 : return Response.ok(allSubmitRequirements); 75 : } 76 : 77 2 : permissionBackend.currentUser().project(rsrc.getNameKey()).check(ProjectPermission.READ_CONFIG); 78 2 : return Response.ok(listSubmitRequirements(rsrc.getProjectState())); 79 : } 80 : 81 : private ImmutableList<SubmitRequirementInfo> listSubmitRequirements(ProjectState projectState) { 82 2 : return projectState.getConfig().getSubmitRequirementSections().values().stream() 83 2 : .map(SubmitRequirementJson::format) 84 2 : .collect(ImmutableList.toImmutableList()); 85 : } 86 : }