Line data Source code
1 : // Copyright (C) 2022 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.notedb; 16 : 17 : import com.google.gerrit.entities.SubmitRequirementResult; 18 : import com.google.gerrit.server.project.OnStoreSubmitRequirementResultModifier; 19 : import com.google.gerrit.server.query.change.ChangeData; 20 : import com.google.gerrit.server.update.BatchUpdateOp; 21 : import com.google.gerrit.server.update.ChangeContext; 22 : import com.google.inject.Inject; 23 : import com.google.inject.assistedinject.Assisted; 24 : import java.util.Collection; 25 : import java.util.List; 26 : import java.util.stream.Collectors; 27 : 28 : /** A {@link BatchUpdateOp} that stores the evaluated submit requirements of a change in NoteDb. */ 29 : public class StoreSubmitRequirementsOp implements BatchUpdateOp { 30 : private final Collection<SubmitRequirementResult> submitRequirementResults; 31 : private final ChangeData changeData; 32 : private final OnStoreSubmitRequirementResultModifier onStoreSubmitRequirementResultModifier; 33 : 34 : public interface Factory { 35 : 36 : /** 37 : * {@code submitRequirements} are explicitly passed to the operation so that they are evaluated 38 : * before the {@link #updateChange} is called. 39 : * 40 : * <p>This is because the return results of {@link ChangeData#submitRequirements()} depend on 41 : * the status of the change, which can be modified by other {@link BatchUpdateOp}, sharing the 42 : * same {@link ChangeContext}. 43 : */ 44 : StoreSubmitRequirementsOp create( 45 : Collection<SubmitRequirementResult> submitRequirements, ChangeData changeData); 46 : } 47 : 48 : @Inject 49 : public StoreSubmitRequirementsOp( 50 : OnStoreSubmitRequirementResultModifier onStoreSubmitRequirementResultModifier, 51 : @Assisted Collection<SubmitRequirementResult> submitRequirementResults, 52 54 : @Assisted ChangeData changeData) { 53 54 : this.onStoreSubmitRequirementResultModifier = onStoreSubmitRequirementResultModifier; 54 54 : this.submitRequirementResults = submitRequirementResults; 55 54 : this.changeData = changeData; 56 54 : } 57 : 58 : @Override 59 : public boolean updateChange(ChangeContext ctx) throws Exception { 60 54 : ChangeUpdate update = ctx.getUpdate(ctx.getChange().currentPatchSetId()); 61 54 : List<SubmitRequirementResult> nonLegacySubmitRequirements = 62 54 : submitRequirementResults.stream() 63 : // We don't store results for legacy submit requirements in NoteDb. While 64 : // surfacing submit requirements for closed changes, we load submit records 65 : // from NoteDb and convert them to submit requirement results. See 66 : // ChangeData#submitRequirements(). 67 54 : .filter(srResult -> !srResult.isLegacy()) 68 54 : .map( 69 : // Pass to OnStoreSubmitRequirementResultModifier for override 70 : srResult -> 71 2 : onStoreSubmitRequirementResultModifier.modifyResultOnStore( 72 2 : srResult.submitRequirement(), srResult, changeData, ctx)) 73 54 : .collect(Collectors.toList()); 74 54 : update.putSubmitRequirementResults(nonLegacySubmitRequirements); 75 54 : return !nonLegacySubmitRequirements.isEmpty(); 76 : } 77 : }