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.common.base.Strings; 18 : import com.google.gerrit.entities.LabelType; 19 : import com.google.gerrit.extensions.common.BatchLabelInput; 20 : import com.google.gerrit.extensions.common.LabelDefinitionInput; 21 : import com.google.gerrit.extensions.restapi.AuthException; 22 : import com.google.gerrit.extensions.restapi.BadRequestException; 23 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 24 : import com.google.gerrit.extensions.restapi.Response; 25 : import com.google.gerrit.extensions.restapi.RestCollectionModifyView; 26 : import com.google.gerrit.extensions.restapi.UnprocessableEntityException; 27 : import com.google.gerrit.server.CurrentUser; 28 : import com.google.gerrit.server.git.meta.MetaDataUpdate; 29 : import com.google.gerrit.server.permissions.PermissionBackend; 30 : import com.google.gerrit.server.permissions.PermissionBackendException; 31 : import com.google.gerrit.server.permissions.ProjectPermission; 32 : import com.google.gerrit.server.project.LabelResource; 33 : import com.google.gerrit.server.project.ProjectCache; 34 : import com.google.gerrit.server.project.ProjectConfig; 35 : import com.google.gerrit.server.project.ProjectResource; 36 : import com.google.inject.Inject; 37 : import com.google.inject.Provider; 38 : import com.google.inject.Singleton; 39 : import java.io.IOException; 40 : import java.util.Map; 41 : import org.eclipse.jgit.errors.ConfigInvalidException; 42 : 43 : /** REST endpoint that allows to add, update and delete label definitions in a batch. */ 44 : @Singleton 45 : public class PostLabels 46 : implements RestCollectionModifyView<ProjectResource, LabelResource, BatchLabelInput> { 47 : private final Provider<CurrentUser> user; 48 : private final PermissionBackend permissionBackend; 49 : private final MetaDataUpdate.User updateFactory; 50 : private final ProjectConfig.Factory projectConfigFactory; 51 : private final DeleteLabel deleteLabel; 52 : private final CreateLabel createLabel; 53 : private final SetLabel setLabel; 54 : private final ProjectCache projectCache; 55 : 56 : @Inject 57 : public PostLabels( 58 : Provider<CurrentUser> user, 59 : PermissionBackend permissionBackend, 60 : MetaDataUpdate.User updateFactory, 61 : ProjectConfig.Factory projectConfigFactory, 62 : DeleteLabel deleteLabel, 63 : CreateLabel createLabel, 64 : SetLabel setLabel, 65 146 : ProjectCache projectCache) { 66 146 : this.user = user; 67 146 : this.permissionBackend = permissionBackend; 68 146 : this.updateFactory = updateFactory; 69 146 : this.projectConfigFactory = projectConfigFactory; 70 146 : this.deleteLabel = deleteLabel; 71 146 : this.createLabel = createLabel; 72 146 : this.setLabel = setLabel; 73 146 : this.projectCache = projectCache; 74 146 : } 75 : 76 : @Override 77 : public Response<?> apply(ProjectResource rsrc, BatchLabelInput input) 78 : throws AuthException, UnprocessableEntityException, PermissionBackendException, IOException, 79 : ConfigInvalidException, BadRequestException, ResourceConflictException { 80 2 : if (!user.get().isIdentifiedUser()) { 81 1 : throw new AuthException("Authentication required"); 82 : } 83 : 84 2 : permissionBackend 85 2 : .currentUser() 86 2 : .project(rsrc.getNameKey()) 87 2 : .check(ProjectPermission.WRITE_CONFIG); 88 : 89 2 : if (input == null) { 90 0 : input = new BatchLabelInput(); 91 : } 92 : 93 2 : try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) { 94 2 : boolean dirty = false; 95 : 96 2 : ProjectConfig config = projectConfigFactory.read(md); 97 : 98 2 : if (input.delete != null && !input.delete.isEmpty()) { 99 1 : for (String labelName : input.delete) { 100 1 : if (!deleteLabel.deleteLabel(config, labelName.trim())) { 101 1 : throw new UnprocessableEntityException(String.format("label %s not found", labelName)); 102 : } 103 1 : } 104 1 : dirty = true; 105 : } 106 : 107 2 : if (input.create != null && !input.create.isEmpty()) { 108 1 : for (LabelDefinitionInput labelInput : input.create) { 109 1 : if (labelInput.name == null || labelInput.name.trim().isEmpty()) { 110 1 : throw new BadRequestException("label name is required for new label"); 111 : } 112 1 : if (labelInput.commitMessage != null) { 113 1 : throw new BadRequestException("commit message on label definition input not supported"); 114 : } 115 1 : createLabel.createLabel(config, labelInput.name.trim(), labelInput); 116 1 : } 117 1 : dirty = true; 118 : } 119 : 120 2 : if (input.update != null && !input.update.isEmpty()) { 121 1 : for (Map.Entry<String, LabelDefinitionInput> e : input.update.entrySet()) { 122 1 : LabelType labelType = config.getLabelSections().get(e.getKey().trim()); 123 1 : if (labelType == null) { 124 1 : throw new UnprocessableEntityException(String.format("label %s not found", e.getKey())); 125 : } 126 1 : if (e.getValue().commitMessage != null) { 127 1 : throw new BadRequestException("commit message on label definition input not supported"); 128 : } 129 1 : setLabel.updateLabel(config, labelType, e.getValue()); 130 1 : } 131 1 : dirty = true; 132 : } 133 : 134 2 : if (input.commitMessage != null) { 135 1 : md.setMessage(Strings.emptyToNull(input.commitMessage.trim())); 136 : } else { 137 2 : md.setMessage("Update labels"); 138 : } 139 : 140 2 : if (dirty) { 141 1 : config.commit(md); 142 1 : projectCache.evictAndReindex(rsrc.getProjectState().getProject()); 143 : } 144 : } 145 : 146 2 : return Response.ok(""); 147 : } 148 : }