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

          Line data    Source code
       1             : // Copyright (C) 2014 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.collect.Lists;
      18             : import com.google.gerrit.common.Nullable;
      19             : import com.google.gerrit.extensions.api.projects.BanCommitInput;
      20             : import com.google.gerrit.extensions.restapi.Response;
      21             : import com.google.gerrit.extensions.restapi.RestApiException;
      22             : import com.google.gerrit.extensions.restapi.RestModifyView;
      23             : import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
      24             : import com.google.gerrit.server.git.BanCommitResult;
      25             : import com.google.gerrit.server.permissions.PermissionBackendException;
      26             : import com.google.gerrit.server.project.ProjectResource;
      27             : import com.google.gerrit.server.update.UpdateException;
      28             : import com.google.inject.Inject;
      29             : import com.google.inject.Singleton;
      30             : import java.io.IOException;
      31             : import java.util.ArrayList;
      32             : import java.util.List;
      33             : import org.eclipse.jgit.lib.ObjectId;
      34             : 
      35             : /** The REST endpoint that marks commits as banned in a project. */
      36             : @Singleton
      37             : public class BanCommit implements RestModifyView<ProjectResource, BanCommitInput> {
      38             :   private final com.google.gerrit.server.git.BanCommit banCommit;
      39             : 
      40             :   @Inject
      41         144 :   BanCommit(com.google.gerrit.server.git.BanCommit banCommit) {
      42         144 :     this.banCommit = banCommit;
      43         144 :   }
      44             : 
      45             :   @Override
      46             :   public Response<BanResultInfo> apply(ProjectResource rsrc, BanCommitInput input)
      47             :       throws RestApiException, UpdateException, IOException, PermissionBackendException {
      48           3 :     BanResultInfo r = new BanResultInfo();
      49           3 :     if (input != null && input.commits != null && !input.commits.isEmpty()) {
      50           2 :       List<ObjectId> commitsToBan = new ArrayList<>(input.commits.size());
      51           2 :       for (String c : input.commits) {
      52             :         try {
      53           2 :           commitsToBan.add(ObjectId.fromString(c));
      54           0 :         } catch (IllegalArgumentException e) {
      55           0 :           throw new UnprocessableEntityException(e.getMessage());
      56           2 :         }
      57           2 :       }
      58             : 
      59           2 :       BanCommitResult result =
      60           2 :           banCommit.ban(rsrc.getNameKey(), rsrc.getUser(), commitsToBan, input.reason);
      61           2 :       r.newlyBanned = transformCommits(result.getNewlyBannedCommits());
      62           2 :       r.alreadyBanned = transformCommits(result.getAlreadyBannedCommits());
      63           2 :       r.ignored = transformCommits(result.getIgnoredObjectIds());
      64             :     }
      65           3 :     return Response.ok(r);
      66             :   }
      67             : 
      68             :   @Nullable
      69             :   private static List<String> transformCommits(List<ObjectId> commits) {
      70           2 :     if (commits == null || commits.isEmpty()) {
      71           2 :       return null;
      72             :     }
      73           2 :     return Lists.transform(commits, ObjectId::getName);
      74             :   }
      75             : 
      76           3 :   public static class BanResultInfo {
      77             :     public List<String> newlyBanned;
      78             :     public List<String> alreadyBanned;
      79             :     public List<String> ignored;
      80             :   }
      81             : }

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