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.extensions.common.InputWithCommitMessage; 19 : import com.google.gerrit.extensions.restapi.AuthException; 20 : import com.google.gerrit.extensions.restapi.IdString; 21 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 22 : import com.google.gerrit.extensions.restapi.Response; 23 : import com.google.gerrit.extensions.restapi.RestModifyView; 24 : import com.google.gerrit.server.CurrentUser; 25 : import com.google.gerrit.server.git.meta.MetaDataUpdate; 26 : import com.google.gerrit.server.permissions.PermissionBackend; 27 : import com.google.gerrit.server.permissions.PermissionBackendException; 28 : import com.google.gerrit.server.permissions.ProjectPermission; 29 : import com.google.gerrit.server.project.LabelResource; 30 : import com.google.gerrit.server.project.ProjectCache; 31 : import com.google.gerrit.server.project.ProjectConfig; 32 : import com.google.inject.Inject; 33 : import com.google.inject.Provider; 34 : import com.google.inject.Singleton; 35 : import java.io.IOException; 36 : import org.eclipse.jgit.errors.ConfigInvalidException; 37 : 38 : @Singleton 39 : public class DeleteLabel implements RestModifyView<LabelResource, InputWithCommitMessage> { 40 : private final Provider<CurrentUser> user; 41 : private final PermissionBackend permissionBackend; 42 : private final MetaDataUpdate.User updateFactory; 43 : private final ProjectConfig.Factory projectConfigFactory; 44 : private final ProjectCache projectCache; 45 : 46 : @Inject 47 : public DeleteLabel( 48 : Provider<CurrentUser> user, 49 : PermissionBackend permissionBackend, 50 : MetaDataUpdate.User updateFactory, 51 : ProjectConfig.Factory projectConfigFactory, 52 146 : ProjectCache projectCache) { 53 146 : this.user = user; 54 146 : this.permissionBackend = permissionBackend; 55 146 : this.updateFactory = updateFactory; 56 146 : this.projectConfigFactory = projectConfigFactory; 57 146 : this.projectCache = projectCache; 58 146 : } 59 : 60 : @Override 61 : public Response<?> apply(LabelResource rsrc, InputWithCommitMessage input) 62 : throws AuthException, ResourceNotFoundException, PermissionBackendException, IOException, 63 : ConfigInvalidException { 64 2 : if (!user.get().isIdentifiedUser()) { 65 0 : throw new AuthException("Authentication required"); 66 : } 67 : 68 2 : permissionBackend 69 2 : .currentUser() 70 2 : .project(rsrc.getProject().getNameKey()) 71 2 : .check(ProjectPermission.WRITE_CONFIG); 72 : 73 2 : if (input == null) { 74 1 : input = new InputWithCommitMessage(); 75 : } 76 : 77 2 : try (MetaDataUpdate md = updateFactory.create(rsrc.getProject().getNameKey())) { 78 2 : ProjectConfig config = projectConfigFactory.read(md); 79 : 80 2 : if (!deleteLabel(config, rsrc.getLabelType().getName())) { 81 0 : throw new ResourceNotFoundException(IdString.fromDecoded(rsrc.getLabelType().getName())); 82 : } 83 : 84 2 : if (input.commitMessage != null) { 85 1 : md.setMessage(Strings.emptyToNull(input.commitMessage.trim())); 86 : } else { 87 2 : md.setMessage("Delete label"); 88 : } 89 : 90 2 : config.commit(md); 91 : } 92 : 93 2 : projectCache.evictAndReindex(rsrc.getProject().getProjectState().getProject()); 94 : 95 2 : return Response.none(); 96 : } 97 : 98 : /** 99 : * Delete the given label from the given project config. 100 : * 101 : * @param config the project config from which the label should be deleted 102 : * @param labelName the name of the label that should be deleted 103 : * @return {@code true} if the label was deleted, {@code false} if the label was not found 104 : */ 105 : public boolean deleteLabel(ProjectConfig config, String labelName) { 106 3 : if (!config.getLabelSections().containsKey(labelName)) { 107 1 : return false; 108 : } 109 : 110 3 : config.getLabelSections().remove(labelName); 111 3 : return true; 112 : } 113 : }