Line data Source code
1 : // Copyright (C) 2012 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.MoreObjects; 18 : import com.google.common.base.Strings; 19 : import com.google.gerrit.extensions.api.projects.DescriptionInput; 20 : import com.google.gerrit.extensions.restapi.AuthException; 21 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 22 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 23 : import com.google.gerrit.extensions.restapi.Response; 24 : import com.google.gerrit.extensions.restapi.RestModifyView; 25 : import com.google.gerrit.server.IdentifiedUser; 26 : import com.google.gerrit.server.git.meta.MetaDataUpdate; 27 : import com.google.gerrit.server.permissions.PermissionBackend; 28 : import com.google.gerrit.server.permissions.PermissionBackendException; 29 : import com.google.gerrit.server.permissions.ProjectPermission; 30 : import com.google.gerrit.server.project.ProjectCache; 31 : import com.google.gerrit.server.project.ProjectConfig; 32 : import com.google.gerrit.server.project.ProjectResource; 33 : import com.google.inject.Inject; 34 : import com.google.inject.Provider; 35 : import com.google.inject.Singleton; 36 : import java.io.IOException; 37 : import org.eclipse.jgit.errors.ConfigInvalidException; 38 : import org.eclipse.jgit.errors.RepositoryNotFoundException; 39 : 40 : @Singleton 41 : public class PutDescription implements RestModifyView<ProjectResource, DescriptionInput> { 42 : private final ProjectCache cache; 43 : private final Provider<MetaDataUpdate.Server> updateFactory; 44 : private final PermissionBackend permissionBackend; 45 : private final ProjectConfig.Factory projectConfigFactory; 46 : 47 : @Inject 48 : PutDescription( 49 : ProjectCache cache, 50 : Provider<MetaDataUpdate.Server> updateFactory, 51 : PermissionBackend permissionBackend, 52 146 : ProjectConfig.Factory projectConfigFactory) { 53 146 : this.cache = cache; 54 146 : this.updateFactory = updateFactory; 55 146 : this.permissionBackend = permissionBackend; 56 146 : this.projectConfigFactory = projectConfigFactory; 57 146 : } 58 : 59 : @Override 60 : public Response<String> apply(ProjectResource resource, DescriptionInput input) 61 : throws AuthException, ResourceConflictException, ResourceNotFoundException, IOException, 62 : PermissionBackendException { 63 2 : if (input == null) { 64 1 : input = new DescriptionInput(); // Delete would set description to null. 65 : } 66 : 67 2 : IdentifiedUser user = resource.getUser().asIdentifiedUser(); 68 2 : permissionBackend 69 2 : .user(user) 70 2 : .project(resource.getNameKey()) 71 2 : .check(ProjectPermission.WRITE_CONFIG); 72 : 73 2 : try (MetaDataUpdate md = updateFactory.get().create(resource.getNameKey())) { 74 2 : ProjectConfig config = projectConfigFactory.read(md); 75 2 : String desc = input.description; 76 2 : config.updateProject(p -> p.setDescription(Strings.emptyToNull(desc))); 77 : 78 2 : String msg = 79 2 : MoreObjects.firstNonNull( 80 2 : Strings.emptyToNull(input.commitMessage), "Update description\n"); 81 2 : if (!msg.endsWith("\n")) { 82 0 : msg += "\n"; 83 : } 84 2 : md.setAuthor(user); 85 2 : md.setMessage(msg); 86 2 : config.commit(md); 87 2 : cache.evictAndReindex(resource.getProjectState().getProject()); 88 2 : md.getRepository().setGitwebDescription(config.getProject().getDescription()); 89 : 90 2 : return Strings.isNullOrEmpty(config.getProject().getDescription()) 91 2 : ? Response.none() 92 2 : : Response.ok(config.getProject().getDescription()); 93 0 : } catch (RepositoryNotFoundException notFound) { 94 0 : throw new ResourceNotFoundException(resource.getName(), notFound); 95 0 : } catch (ConfigInvalidException e) { 96 0 : throw new ResourceConflictException( 97 0 : String.format("invalid project.config: %s", e.getMessage())); 98 : } 99 : } 100 : }