LCOV - code coverage report
Current view: top level - server/restapi/config - IndexChanges.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 18 25 72.0 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2019 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.config;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.common.data.GlobalCapability;
      19             : import com.google.gerrit.entities.Change;
      20             : import com.google.gerrit.extensions.annotations.RequiresCapability;
      21             : import com.google.gerrit.extensions.restapi.Response;
      22             : import com.google.gerrit.extensions.restapi.RestModifyView;
      23             : import com.google.gerrit.server.change.ChangeFinder;
      24             : import com.google.gerrit.server.config.ConfigResource;
      25             : import com.google.gerrit.server.index.change.ChangeIndexer;
      26             : import com.google.gerrit.server.notedb.ChangeNotes;
      27             : import com.google.gerrit.server.query.change.ChangeData;
      28             : import com.google.gerrit.server.restapi.config.IndexChanges.Input;
      29             : import com.google.inject.Inject;
      30             : import com.google.inject.Singleton;
      31             : import java.util.List;
      32             : import java.util.Optional;
      33             : import java.util.Set;
      34             : 
      35             : @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
      36             : @Singleton
      37             : public class IndexChanges implements RestModifyView<ConfigResource, Input> {
      38         138 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      39             : 
      40           2 :   public static class Input {
      41             :     public Set<String> changes;
      42             :     boolean deleteMissing;
      43             :   }
      44             : 
      45             :   private final ChangeFinder changeFinder;
      46             :   private final ChangeData.Factory changeDataFactory;
      47             :   private final ChangeIndexer indexer;
      48             : 
      49             :   @Inject
      50             :   IndexChanges(
      51         138 :       ChangeFinder changeFinder, ChangeData.Factory changeDataFactory, ChangeIndexer indexer) {
      52         138 :     this.changeFinder = changeFinder;
      53         138 :     this.changeDataFactory = changeDataFactory;
      54         138 :     this.indexer = indexer;
      55         138 :   }
      56             : 
      57             :   @Override
      58             :   public Response<String> apply(ConfigResource resource, Input input) {
      59           2 :     if (input == null || input.changes == null) {
      60           1 :       return Response.ok("Nothing to index");
      61             :     }
      62             : 
      63           1 :     for (String id : input.changes) {
      64           1 :       List<ChangeNotes> notes = changeFinder.find(id);
      65             : 
      66           1 :       if (notes.isEmpty()) {
      67           0 :         logger.atWarning().log("Change %s missing in NoteDb", id);
      68           0 :         if (input.deleteMissing) {
      69           0 :           Optional<Change.Id> changeId = Change.Id.tryParse(id);
      70           0 :           if (changeId.isPresent()) {
      71           0 :             logger.atWarning().log("Deleting change %s from index", changeId.get());
      72           0 :             indexer.delete(changeId.get());
      73             :           }
      74           0 :         }
      75             :         continue;
      76             :       }
      77             : 
      78           1 :       for (ChangeNotes n : notes) {
      79           1 :         indexer.index(changeDataFactory.create(n));
      80           1 :         logger.atFine().log("Indexed change %s", id);
      81           1 :       }
      82           1 :     }
      83             : 
      84           1 :     return Response.ok("Indexed changes " + input.changes);
      85             :   }
      86             : }

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