LCOV - code coverage report
Current view: top level - server/restapi/project - DeleteSubmitRequirement.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 22 27 81.5 %
Date: 2022-11-19 15:00:39 Functions: 3 3 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2022 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.extensions.common.Input;
      18             : import com.google.gerrit.extensions.restapi.AuthException;
      19             : import com.google.gerrit.extensions.restapi.IdString;
      20             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      21             : import com.google.gerrit.extensions.restapi.Response;
      22             : import com.google.gerrit.extensions.restapi.RestModifyView;
      23             : import com.google.gerrit.server.CurrentUser;
      24             : import com.google.gerrit.server.git.meta.MetaDataUpdate;
      25             : import com.google.gerrit.server.permissions.PermissionBackend;
      26             : import com.google.gerrit.server.permissions.ProjectPermission;
      27             : import com.google.gerrit.server.project.ProjectCache;
      28             : import com.google.gerrit.server.project.ProjectConfig;
      29             : import com.google.gerrit.server.project.SubmitRequirementResource;
      30             : import com.google.inject.Inject;
      31             : import com.google.inject.Provider;
      32             : import com.google.inject.Singleton;
      33             : 
      34             : @Singleton
      35             : public class DeleteSubmitRequirement implements RestModifyView<SubmitRequirementResource, Input> {
      36             :   private final Provider<CurrentUser> user;
      37             :   private final PermissionBackend permissionBackend;
      38             :   private final MetaDataUpdate.User updateFactory;
      39             :   private final ProjectConfig.Factory projectConfigFactory;
      40             :   private final ProjectCache projectCache;
      41             : 
      42             :   @Inject
      43             :   public DeleteSubmitRequirement(
      44             :       Provider<CurrentUser> user,
      45             :       PermissionBackend permissionBackend,
      46             :       MetaDataUpdate.User updateFactory,
      47             :       ProjectConfig.Factory projectConfigFactory,
      48         138 :       ProjectCache projectCache) {
      49         138 :     this.user = user;
      50         138 :     this.permissionBackend = permissionBackend;
      51         138 :     this.updateFactory = updateFactory;
      52         138 :     this.projectConfigFactory = projectConfigFactory;
      53         138 :     this.projectCache = projectCache;
      54         138 :   }
      55             : 
      56             :   @Override
      57             :   public Response<?> apply(SubmitRequirementResource rsrc, Input input) throws Exception {
      58           2 :     if (!user.get().isIdentifiedUser()) {
      59           0 :       throw new AuthException("Authentication required");
      60             :     }
      61             : 
      62           2 :     permissionBackend
      63           2 :         .currentUser()
      64           2 :         .project(rsrc.getProject().getNameKey())
      65           2 :         .check(ProjectPermission.WRITE_CONFIG);
      66             : 
      67           2 :     try (MetaDataUpdate md = updateFactory.create(rsrc.getProject().getNameKey())) {
      68           2 :       ProjectConfig config = projectConfigFactory.read(md);
      69             : 
      70           2 :       if (!deleteSubmitRequirement(config, rsrc.getSubmitRequirement().name())) {
      71             :         // This code is unreachable because the exception is thrown when rsrc was parsed
      72           0 :         throw new ResourceNotFoundException(
      73           0 :             String.format(
      74             :                 "Submit requirement '%s' not found",
      75           0 :                 IdString.fromDecoded(rsrc.getSubmitRequirement().name())));
      76             :       }
      77             : 
      78           2 :       md.setMessage("Delete submit requirement");
      79           2 :       config.commit(md);
      80             :     }
      81             : 
      82           2 :     projectCache.evict(rsrc.getProject().getProjectState().getProject().getNameKey());
      83             : 
      84           2 :     return Response.none();
      85             :   }
      86             : 
      87             :   /**
      88             :    * Delete the given submit requirement from the project config.
      89             :    *
      90             :    * @param config the project config from which the submit-requirement should be deleted
      91             :    * @param srName the name of the submit requirement that should be deleted
      92             :    * @return {@code true} if the submit-requirement was deleted, {@code false} if the
      93             :    *     submit-requirement was not found
      94             :    */
      95             :   public boolean deleteSubmitRequirement(ProjectConfig config, String srName) {
      96           2 :     if (!config.getSubmitRequirementSections().containsKey(srName)) {
      97           0 :       return false;
      98             :     }
      99           2 :     config.getSubmitRequirementSections().remove(srName);
     100           2 :     return true;
     101             :   }
     102             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750