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.DashboardInfo; 20 : import com.google.gerrit.extensions.api.projects.SetDashboardInput; 21 : import com.google.gerrit.extensions.restapi.BadRequestException; 22 : import com.google.gerrit.extensions.restapi.IdString; 23 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 24 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 25 : import com.google.gerrit.extensions.restapi.Response; 26 : import com.google.gerrit.extensions.restapi.RestApiException; 27 : import com.google.gerrit.extensions.restapi.RestModifyView; 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.DashboardResource; 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 java.io.IOException; 39 : import org.eclipse.jgit.errors.ConfigInvalidException; 40 : import org.eclipse.jgit.errors.RepositoryNotFoundException; 41 : import org.kohsuke.args4j.Option; 42 : 43 : class SetDefaultDashboard implements RestModifyView<DashboardResource, SetDashboardInput> { 44 : private final ProjectCache cache; 45 : private final MetaDataUpdate.Server updateFactory; 46 : private final DashboardsCollection dashboards; 47 : private final Provider<GetDashboard> get; 48 : private final PermissionBackend permissionBackend; 49 : private final ProjectConfig.Factory projectConfigFactory; 50 : 51 : @Option(name = "--inherited", usage = "set dashboard inherited by children") 52 : boolean inherited; 53 : 54 : @Inject 55 : SetDefaultDashboard( 56 : ProjectCache cache, 57 : MetaDataUpdate.Server updateFactory, 58 : DashboardsCollection dashboards, 59 : Provider<GetDashboard> get, 60 : PermissionBackend permissionBackend, 61 2 : ProjectConfig.Factory projectConfigFactory) { 62 2 : this.cache = cache; 63 2 : this.updateFactory = updateFactory; 64 2 : this.dashboards = dashboards; 65 2 : this.get = get; 66 2 : this.permissionBackend = permissionBackend; 67 2 : this.projectConfigFactory = projectConfigFactory; 68 2 : } 69 : 70 : @Override 71 : public Response<DashboardInfo> apply(DashboardResource rsrc, SetDashboardInput input) 72 : throws RestApiException, IOException, PermissionBackendException { 73 2 : if (input == null) { 74 0 : input = new SetDashboardInput(); // Delete would set input to null. 75 : } 76 2 : input.id = Strings.emptyToNull(input.id); 77 : 78 2 : permissionBackend 79 2 : .user(rsrc.getUser()) 80 2 : .project(rsrc.getProjectState().getNameKey()) 81 2 : .check(ProjectPermission.WRITE_CONFIG); 82 : 83 2 : DashboardResource target = null; 84 2 : if (input.id != null) { 85 : try { 86 1 : target = 87 1 : dashboards.parse( 88 1 : new ProjectResource(rsrc.getProjectState(), rsrc.getUser()), 89 1 : IdString.fromUrl(input.id)); 90 0 : } catch (ResourceNotFoundException e) { 91 0 : throw new BadRequestException("dashboard " + input.id + " not found", e); 92 0 : } catch (ConfigInvalidException e) { 93 0 : throw new ResourceConflictException(e.getMessage()); 94 1 : } 95 : } 96 : 97 2 : try (MetaDataUpdate md = updateFactory.create(rsrc.getProjectState().getNameKey())) { 98 2 : ProjectConfig config = projectConfigFactory.read(md); 99 2 : String id = input.id; 100 2 : if (inherited) { 101 0 : config.updateProject(p -> p.setDefaultDashboard(id)); 102 : } else { 103 2 : config.updateProject(p -> p.setLocalDefaultDashboard(id)); 104 : } 105 : 106 2 : String msg = 107 2 : MoreObjects.firstNonNull( 108 2 : Strings.emptyToNull(input.commitMessage), 109 2 : input.id == null 110 2 : ? "Removed default dashboard.\n" 111 1 : : String.format("Changed default dashboard to %s.\n", input.id)); 112 2 : if (!msg.endsWith("\n")) { 113 0 : msg += "\n"; 114 : } 115 2 : md.setAuthor(rsrc.getUser().asIdentifiedUser()); 116 2 : md.setMessage(msg); 117 2 : config.commit(md); 118 2 : cache.evictAndReindex(rsrc.getProjectState().getProject()); 119 : 120 2 : if (target != null) { 121 1 : Response<DashboardInfo> response = get.get().apply(target); 122 1 : response.value().isDefault = true; 123 1 : return response; 124 : } 125 2 : return Response.none(); 126 1 : } catch (RepositoryNotFoundException notFound) { 127 0 : throw new ResourceNotFoundException(rsrc.getProjectState().getProject().getName(), notFound); 128 0 : } catch (ConfigInvalidException e) { 129 0 : throw new ResourceConflictException( 130 0 : String.format("invalid project.config: %s", e.getMessage())); 131 : } 132 : } 133 : }