Line data Source code
1 : // Copyright (C) 2021 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.query.approval; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.gerrit.entities.Account; 21 : import com.google.gerrit.entities.LabelType; 22 : import com.google.gerrit.entities.PatchSet; 23 : import com.google.gerrit.extensions.client.ChangeKind; 24 : import com.google.gerrit.server.notedb.ChangeNotes; 25 : import org.eclipse.jgit.lib.Config; 26 : import org.eclipse.jgit.revwalk.RevWalk; 27 : 28 : /** Entity representing all required information to match predicates for copying approvals. */ 29 : @AutoValue 30 15 : public abstract class ApprovalContext { 31 : public abstract PatchSet.Id sourcePatchSetId(); 32 : 33 : public abstract Account.Id approverId(); 34 : 35 : public abstract LabelType labelType(); 36 : 37 : /** Value of the approval on the source patch set to be copied. */ 38 : public abstract short approvalValue(); 39 : 40 : /** 41 : * Target change and patch set for the approval. This must be used instead of getting the PatchSet 42 : * from {@link #changeNotes()} because it is possible we are now creating the patch-set, so it 43 : * doesn't exist in changeNotes yet. 44 : */ 45 : public abstract PatchSet targetPatchSet(); 46 : 47 : /** {@link ChangeNotes} of the change in question. */ 48 : public abstract ChangeNotes changeNotes(); 49 : 50 : /** {@link ChangeKind} of the delta between the origin and target patch set. */ 51 : public abstract ChangeKind changeKind(); 52 : 53 : /** Whether the new patch set is a merge commit. */ 54 : public abstract boolean isMerge(); 55 : 56 : /** {@link RevWalk} of the repository for the current commit. */ 57 : public abstract RevWalk revWalk(); 58 : 59 : /** {@link RevWalk} of the repository for the current commit. */ 60 : public abstract Config repoConfig(); 61 : 62 : public static ApprovalContext create( 63 : ChangeNotes changeNotes, 64 : PatchSet.Id sourcePatchSetId, 65 : Account.Id approverId, 66 : LabelType labelType, 67 : short approvalValue, 68 : PatchSet targetPatchSet, 69 : ChangeKind changeKind, 70 : boolean isMerge, 71 : RevWalk revWalk, 72 : Config repoConfig) { 73 15 : checkState( 74 15 : sourcePatchSetId.changeId().equals(targetPatchSet.id().changeId()), 75 : "approval and target must be the same change. got: %s, %s", 76 : sourcePatchSetId, 77 15 : targetPatchSet.id()); 78 : // TODO(ekempin): Use checkState to verify that psa.patchSetId().get() + 1 == id.get() so that 79 : // it's ensured that approvals are only copied to the next consecutive patch set. To add back 80 : // this verification https://gerrit-review.googlesource.com/c/gerrit/+/312633 can be reverted. 81 : // As explained in the commit message of this change doing this check is only possible if there 82 : // are no changes with gaps in patch set numbers. Since it's planned to fix-up old changes with 83 : // gaps in patch set numbers, this todo is a reminder to add back the check once this is done. 84 15 : return new AutoValue_ApprovalContext( 85 : sourcePatchSetId, 86 : approverId, 87 : labelType, 88 : approvalValue, 89 : targetPatchSet, 90 : changeNotes, 91 : changeKind, 92 : isMerge, 93 : revWalk, 94 : repoConfig); 95 : } 96 : }