LCOV - code coverage report
Current view: top level - sshd - ChangeArgumentParser.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 37 48 77.1 %
Date: 2022-11-19 15:00:39 Functions: 6 7 85.7 %

          Line data    Source code
       1             : // Copyright (C) 2016 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;
      16             : 
      17             : import com.google.gerrit.common.Nullable;
      18             : import com.google.gerrit.entities.Change;
      19             : import com.google.gerrit.entities.Project;
      20             : import com.google.gerrit.extensions.restapi.AuthException;
      21             : import com.google.gerrit.extensions.restapi.RestApiException;
      22             : import com.google.gerrit.server.change.ChangeFinder;
      23             : import com.google.gerrit.server.change.ChangeResource;
      24             : import com.google.gerrit.server.notedb.ChangeNotes;
      25             : import com.google.gerrit.server.permissions.ChangePermission;
      26             : import com.google.gerrit.server.permissions.GlobalPermission;
      27             : import com.google.gerrit.server.permissions.PermissionBackend;
      28             : import com.google.gerrit.server.permissions.PermissionBackendException;
      29             : import com.google.gerrit.server.project.ProjectState;
      30             : import com.google.gerrit.server.restapi.change.ChangesCollection;
      31             : import com.google.gerrit.sshd.BaseCommand.UnloggedFailure;
      32             : import com.google.inject.Inject;
      33             : import java.util.ArrayList;
      34             : import java.util.Arrays;
      35             : import java.util.List;
      36             : import java.util.Map;
      37             : 
      38             : public class ChangeArgumentParser {
      39             :   private final ChangesCollection changesCollection;
      40             :   private final ChangeFinder changeFinder;
      41             :   private final ChangeNotes.Factory changeNotesFactory;
      42             :   private final PermissionBackend permissionBackend;
      43             : 
      44             :   @Inject
      45             :   ChangeArgumentParser(
      46             :       ChangesCollection changesCollection,
      47             :       ChangeFinder changeFinder,
      48             :       ChangeNotes.Factory changeNotesFactory,
      49           1 :       PermissionBackend permissionBackend) {
      50           1 :     this.changesCollection = changesCollection;
      51           1 :     this.changeFinder = changeFinder;
      52           1 :     this.changeNotesFactory = changeNotesFactory;
      53           1 :     this.permissionBackend = permissionBackend;
      54           1 :   }
      55             : 
      56             :   public void addChange(String id, Map<Change.Id, ChangeResource> changes)
      57             :       throws UnloggedFailure, PermissionBackendException {
      58           0 :     addChange(id, changes, null);
      59           0 :   }
      60             : 
      61             :   public void addChange(
      62             :       String id, Map<Change.Id, ChangeResource> changes, @Nullable ProjectState projectState)
      63             :       throws UnloggedFailure, PermissionBackendException {
      64           1 :     addChange(id, changes, projectState, true);
      65           1 :   }
      66             : 
      67             :   public void addChange(
      68             :       String id,
      69             :       Map<Change.Id, ChangeResource> changes,
      70             :       @Nullable ProjectState projectState,
      71             :       boolean useIndex)
      72             :       throws UnloggedFailure, PermissionBackendException {
      73           1 :     List<ChangeNotes> matched = useIndex ? changeFinder.find(id) : changeFromNotesFactory(id);
      74           1 :     List<ChangeNotes> toAdd = new ArrayList<>(changes.size());
      75             :     boolean canMaintainServer;
      76             :     try {
      77           1 :       permissionBackend.currentUser().check(GlobalPermission.MAINTAIN_SERVER);
      78           1 :       canMaintainServer = true;
      79           1 :     } catch (AuthException | PermissionBackendException e) {
      80           1 :       canMaintainServer = false;
      81           1 :     }
      82           1 :     for (ChangeNotes notes : matched) {
      83           1 :       if (!changes.containsKey(notes.getChangeId())
      84           1 :           && inProject(projectState, notes.getProjectName())) {
      85           1 :         if (canMaintainServer) {
      86           1 :           toAdd.add(notes);
      87           1 :           continue;
      88             :         }
      89             : 
      90           1 :         if (projectState != null && !projectState.statePermitsRead()) {
      91           0 :           continue;
      92             :         }
      93             : 
      94             :         try {
      95           1 :           permissionBackend.currentUser().change(notes).check(ChangePermission.READ);
      96           1 :           toAdd.add(notes);
      97           0 :         } catch (AuthException e) {
      98             :           // Do nothing.
      99           1 :         }
     100             :       }
     101           1 :     }
     102             : 
     103           1 :     if (toAdd.isEmpty()) {
     104           0 :       throw new UnloggedFailure(1, "\"" + id + "\" no such change");
     105           1 :     } else if (toAdd.size() > 1) {
     106           0 :       throw new UnloggedFailure(1, "\"" + id + "\" matches multiple changes");
     107             :     }
     108           1 :     Change.Id cId = toAdd.get(0).getChangeId();
     109             :     ChangeResource changeResource;
     110             :     try {
     111           1 :       changeResource = changesCollection.parse(cId);
     112           0 :     } catch (RestApiException e) {
     113           0 :       throw new UnloggedFailure(1, "\"" + id + "\" no such change", e);
     114           1 :     }
     115           1 :     changes.put(cId, changeResource);
     116           1 :   }
     117             : 
     118             :   private List<ChangeNotes> changeFromNotesFactory(String id) throws UnloggedFailure {
     119           1 :     return changeNotesFactory.createUsingIndexLookup(parseId(id));
     120             :   }
     121             : 
     122             :   private List<Change.Id> parseId(String id) throws UnloggedFailure {
     123             :     try {
     124           1 :       return Arrays.asList(Change.id(Integer.parseInt(id)));
     125           0 :     } catch (NumberFormatException e) {
     126           0 :       throw new UnloggedFailure(2, "Invalid change ID " + id, e);
     127             :     }
     128             :   }
     129             : 
     130             :   private boolean inProject(ProjectState projectState, Project.NameKey project) {
     131           1 :     if (projectState != null) {
     132           0 :       return projectState.getNameKey().equals(project);
     133             :     }
     134             : 
     135             :     // No --project option, so they want every project.
     136           1 :     return true;
     137             :   }
     138             : }

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