LCOV - code coverage report
Current view: top level - server/args4j - ChangeIdHandler.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 0 27 0.0 %
Date: 2022-11-19 15:00:39 Functions: 0 4 0.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.server.args4j;
      16             : 
      17             : import static com.google.gerrit.util.cli.Localizable.localizable;
      18             : 
      19             : import com.google.common.base.Splitter;
      20             : import com.google.common.flogger.FluentLogger;
      21             : import com.google.gerrit.entities.BranchNameKey;
      22             : import com.google.gerrit.entities.Change;
      23             : import com.google.gerrit.entities.Project;
      24             : import com.google.gerrit.exceptions.StorageException;
      25             : import com.google.gerrit.server.query.change.ChangeData;
      26             : import com.google.gerrit.server.query.change.InternalChangeQuery;
      27             : import com.google.inject.Inject;
      28             : import com.google.inject.Provider;
      29             : import com.google.inject.assistedinject.Assisted;
      30             : import java.util.List;
      31             : import org.kohsuke.args4j.CmdLineException;
      32             : import org.kohsuke.args4j.CmdLineParser;
      33             : import org.kohsuke.args4j.OptionDef;
      34             : import org.kohsuke.args4j.spi.OptionHandler;
      35             : import org.kohsuke.args4j.spi.Parameters;
      36             : import org.kohsuke.args4j.spi.Setter;
      37             : 
      38             : public class ChangeIdHandler extends OptionHandler<Change.Id> {
      39           0 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      40             : 
      41             :   private final Provider<InternalChangeQuery> queryProvider;
      42             : 
      43             :   @Inject
      44             :   public ChangeIdHandler(
      45             :       // TODO(dborowitz): Not sure whether this is injectable here.
      46             :       Provider<InternalChangeQuery> queryProvider,
      47             :       @Assisted CmdLineParser parser,
      48             :       @Assisted OptionDef option,
      49             :       @Assisted Setter<Change.Id> setter) {
      50           0 :     super(parser, option, setter);
      51           0 :     this.queryProvider = queryProvider;
      52           0 :   }
      53             : 
      54             :   @Override
      55             :   public final int parseArguments(Parameters params) throws CmdLineException {
      56           0 :     String token = params.getParameter(0);
      57           0 :     List<String> tokens = Splitter.on(',').splitToList(token);
      58           0 :     if (tokens.size() != 3) {
      59           0 :       throw new CmdLineException(
      60           0 :           owner, localizable("change should be specified as <project>,<branch>,<change-id>"));
      61             :     }
      62             : 
      63             :     try {
      64           0 :       Change.Key key = Change.Key.parse(tokens.get(2));
      65           0 :       Project.NameKey project = Project.nameKey(tokens.get(0));
      66           0 :       BranchNameKey branch = BranchNameKey.create(project, tokens.get(1));
      67           0 :       List<ChangeData> changes = queryProvider.get().byBranchKey(branch, key);
      68           0 :       if (!changes.isEmpty()) {
      69           0 :         if (changes.size() > 1) {
      70           0 :           String msg = "\"%s\": resolves to multiple changes";
      71           0 :           logger.atSevere().log(msg, token);
      72           0 :           throw new CmdLineException(owner, localizable(msg), token);
      73             :         }
      74           0 :         setter.addValue(changes.get(0).getId());
      75           0 :         return 1;
      76             :       }
      77           0 :     } catch (IllegalArgumentException e) {
      78           0 :       throw new CmdLineException(owner, localizable("Change-Id is not valid: %s"), e.getMessage());
      79           0 :     } catch (StorageException e) {
      80           0 :       throw new CmdLineException(owner, localizable("Database error: %s"), e.getMessage());
      81           0 :     }
      82             : 
      83           0 :     throw new CmdLineException(owner, localizable("\"%s\": change not found"), token);
      84             :   }
      85             : 
      86             :   @Override
      87             :   public final String getDefaultMetaVariable() {
      88           0 :     return "CHANGE";
      89             :   }
      90             : }

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