LCOV - code coverage report
Current view: top level - sshd/commands - BanCommitCommand.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 17 19 89.5 %
Date: 2022-11-19 15:00:39 Functions: 3 3 100.0 %

          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.sshd.commands;
      16             : 
      17             : import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER;
      18             : 
      19             : import com.google.common.base.Joiner;
      20             : import com.google.common.collect.Lists;
      21             : import com.google.gerrit.extensions.api.projects.BanCommitInput;
      22             : import com.google.gerrit.server.project.ProjectResource;
      23             : import com.google.gerrit.server.project.ProjectState;
      24             : import com.google.gerrit.server.restapi.project.BanCommit;
      25             : import com.google.gerrit.server.restapi.project.BanCommit.BanResultInfo;
      26             : import com.google.gerrit.sshd.CommandMetaData;
      27             : import com.google.gerrit.sshd.SshCommand;
      28             : import com.google.inject.Inject;
      29             : import java.util.ArrayList;
      30             : import java.util.List;
      31             : import org.eclipse.jgit.lib.ObjectId;
      32             : import org.kohsuke.args4j.Argument;
      33             : import org.kohsuke.args4j.Option;
      34             : 
      35             : @CommandMetaData(
      36             :     name = "ban-commit",
      37             :     description = "Ban a commit from a project's repository",
      38             :     runsAt = MASTER)
      39           1 : public class BanCommitCommand extends SshCommand {
      40             :   @Option(
      41             :       name = "--reason",
      42             :       aliases = {"-r"},
      43             :       metaVar = "REASON",
      44             :       usage = "reason for banning the commit")
      45             :   private String reason;
      46             : 
      47             :   @Argument(
      48             :       index = 0,
      49             :       required = true,
      50             :       metaVar = "PROJECT",
      51             :       usage = "name of the project for which the commit should be banned")
      52             :   private ProjectState projectState;
      53             : 
      54           1 :   @Argument(
      55             :       index = 1,
      56             :       required = true,
      57             :       multiValued = true,
      58             :       metaVar = "COMMIT",
      59             :       usage = "commit(s) that should be banned")
      60             :   private List<ObjectId> commitsToBan = new ArrayList<>();
      61             : 
      62             :   @Inject private BanCommit banCommit;
      63             : 
      64             :   @Override
      65             :   protected void run() throws Failure {
      66           1 :     enableGracefulStop();
      67             :     try {
      68           1 :       BanCommitInput input =
      69           1 :           BanCommitInput.fromCommits(Lists.transform(commitsToBan, ObjectId::getName));
      70           1 :       input.reason = reason;
      71             : 
      72           1 :       BanResultInfo r = banCommit.apply(new ProjectResource(projectState, user), input).value();
      73           1 :       printCommits(r.newlyBanned, "The following commits were banned");
      74           1 :       printCommits(r.alreadyBanned, "The following commits were already banned");
      75           1 :       printCommits(r.ignored, "The following ids do not represent commits and were ignored");
      76           0 :     } catch (Exception e) {
      77           0 :       throw die(e);
      78           1 :     }
      79           1 :   }
      80             : 
      81             :   private void printCommits(List<String> commits, String message) {
      82           1 :     if (commits != null && !commits.isEmpty()) {
      83           1 :       stdout.print(message + ":\n");
      84           1 :       stdout.print(Joiner.on(",\n").join(commits));
      85           1 :       stdout.print("\n\n");
      86             :     }
      87           1 :   }
      88             : }

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