LCOV - code coverage report
Current view: top level - server/update - SuperprojectUpdateSubmissionListener.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 28 92.9 %
Date: 2022-11-19 15:00:39 Functions: 8 9 88.9 %

          Line data    Source code
       1             : // Copyright (C) 2020 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.update;
      16             : 
      17             : import com.google.common.collect.ImmutableList;
      18             : import com.google.common.flogger.FluentLogger;
      19             : import com.google.gerrit.entities.BranchNameKey;
      20             : import com.google.gerrit.extensions.restapi.RestApiException;
      21             : import com.google.gerrit.server.submit.MergeOpRepoManager;
      22             : import com.google.gerrit.server.submit.SubmoduleOp;
      23             : import com.google.inject.AbstractModule;
      24             : import com.google.inject.Provides;
      25             : import java.util.Collection;
      26             : import java.util.HashMap;
      27             : import java.util.Map;
      28             : import java.util.Optional;
      29             : import javax.inject.Inject;
      30             : import org.eclipse.jgit.transport.ReceiveCommand;
      31             : 
      32             : /** Update superprojects after submission is done */
      33             : public class SuperprojectUpdateSubmissionListener implements SubmissionListener {
      34             : 
      35         102 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      36             : 
      37             :   private final SubmoduleOp.Factory subOpFactory;
      38         102 :   private final Map<BranchNameKey, ReceiveCommand> updatedBranches = new HashMap<>();
      39         102 :   private ImmutableList<BatchUpdate> batchUpdates = ImmutableList.of();
      40             :   private boolean dryrun;
      41             : 
      42         152 :   public static class SuperprojectUpdateSubmissionListenerModule extends AbstractModule {
      43             :     @Provides
      44             :     @SuperprojectUpdateOnSubmission
      45             :     ImmutableList<SubmissionListener> provideSubmissionListeners(
      46             :         SuperprojectUpdateSubmissionListener listener) {
      47         102 :       return ImmutableList.of(listener);
      48             :     }
      49             :   }
      50             : 
      51             :   @Inject
      52         102 :   public SuperprojectUpdateSubmissionListener(SubmoduleOp.Factory subOpFactory) {
      53         102 :     this.subOpFactory = subOpFactory;
      54         102 :   }
      55             : 
      56             :   @Override
      57             :   public void setDryrun() {
      58           0 :     this.dryrun = true;
      59           0 :   }
      60             : 
      61             :   @Override
      62             :   public void beforeBatchUpdates(Collection<BatchUpdate> updates) {
      63          69 :     if (!batchUpdates.isEmpty()) {
      64             :       // This is a retry. Save previous updates, as they are not in the new BatchUpdate.
      65           8 :       collectSuccessfullUpdates();
      66             :     }
      67          69 :     this.batchUpdates = ImmutableList.copyOf(updates);
      68          69 :   }
      69             : 
      70             :   @Override
      71             :   public void afterSubmission(MergeOpRepoManager orm) {
      72          69 :     collectSuccessfullUpdates();
      73             :     // Update superproject gitlinks if required.
      74          69 :     if (!updatedBranches.isEmpty()) {
      75             :       try {
      76          68 :         SubmoduleOp op = subOpFactory.create(updatedBranches, orm);
      77          68 :         op.updateSuperProjects(dryrun);
      78           1 :       } catch (RestApiException e) {
      79           1 :         logger.atWarning().withCause(e).log("Can't update the superprojects");
      80          68 :       }
      81             :     }
      82          69 :   }
      83             : 
      84             :   @Override
      85             :   public Optional<BatchUpdateListener> listensToBatchUpdates() {
      86          69 :     return Optional.empty();
      87             :   }
      88             : 
      89             :   private void collectSuccessfullUpdates() {
      90          69 :     if (!this.batchUpdates.isEmpty()) {
      91          69 :       for (BatchUpdate bu : batchUpdates) {
      92          69 :         updatedBranches.putAll(bu.getSuccessfullyUpdatedBranches(dryrun));
      93          69 :       }
      94             :     }
      95          69 :   }
      96             : }

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