Line data Source code
1 : // Copyright (C) 2019 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.gerrit.entities.LabelType; 18 : import com.google.gerrit.extensions.common.LabelDefinitionInfo; 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.LabelDefinitionJson; 27 : import com.google.gerrit.server.project.ProjectResource; 28 : import com.google.gerrit.server.project.ProjectState; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Provider; 31 : import java.util.ArrayList; 32 : import java.util.Collection; 33 : import java.util.Comparator; 34 : import java.util.List; 35 : import org.kohsuke.args4j.Option; 36 : 37 : public class ListLabels implements RestReadView<ProjectResource> { 38 : private final Provider<CurrentUser> user; 39 : private final PermissionBackend permissionBackend; 40 : 41 : @Inject 42 3 : public ListLabels(Provider<CurrentUser> user, PermissionBackend permissionBackend) { 43 3 : this.user = user; 44 3 : this.permissionBackend = permissionBackend; 45 3 : } 46 : 47 : @Option(name = "--inherited", usage = "to include inherited label definitions") 48 : private boolean inherited; 49 : 50 : public ListLabels withInherited(boolean inherited) { 51 2 : this.inherited = inherited; 52 2 : return this; 53 : } 54 : 55 : @Override 56 : public Response<List<LabelDefinitionInfo>> apply(ProjectResource rsrc) 57 : throws AuthException, PermissionBackendException { 58 3 : if (!user.get().isIdentifiedUser()) { 59 1 : throw new AuthException("Authentication required"); 60 : } 61 : 62 3 : if (inherited) { 63 1 : List<LabelDefinitionInfo> allLabels = new ArrayList<>(); 64 1 : for (ProjectState projectState : rsrc.getProjectState().treeInOrder()) { 65 : try { 66 1 : permissionBackend 67 1 : .currentUser() 68 1 : .project(projectState.getNameKey()) 69 1 : .check(ProjectPermission.READ_CONFIG); 70 1 : } catch (AuthException e) { 71 1 : throw new AuthException(projectState.getNameKey() + ": " + e.getMessage(), e); 72 1 : } 73 1 : allLabels.addAll(listLabels(projectState)); 74 1 : } 75 1 : return Response.ok(allLabels); 76 : } 77 : 78 3 : permissionBackend.currentUser().project(rsrc.getNameKey()).check(ProjectPermission.READ_CONFIG); 79 3 : return Response.ok(listLabels(rsrc.getProjectState())); 80 : } 81 : 82 : private List<LabelDefinitionInfo> listLabels(ProjectState projectState) { 83 3 : Collection<LabelType> labelTypes = projectState.getConfig().getLabelSections().values(); 84 3 : List<LabelDefinitionInfo> labels = new ArrayList<>(labelTypes.size()); 85 3 : for (LabelType labelType : labelTypes) { 86 2 : labels.add(LabelDefinitionJson.format(projectState.getNameKey(), labelType)); 87 2 : } 88 3 : labels.sort(Comparator.comparing(l -> l.name)); 89 3 : return labels; 90 : } 91 : }