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.edit; 16 : 17 : import com.google.gerrit.entities.PatchSet; 18 : import com.google.gerrit.server.notedb.ChangeNotes; 19 : 20 : /** 21 : * Intended modification target. 22 : * 23 : * <p>See also {@link ModificationTarget}. Some modifications may have a fixed target (e.g. 24 : * suggested fixes of robot comments). For other modifications, the presence of a change edit 25 : * influences their target. The latter comes from the REST endpoints of change edits which work no 26 : * matter whether a change edit is present or not. If it's not present, a new change edit is created 27 : * based on the current patchset. As we don't want to create an "empty" commit for the new change 28 : * edit first, we need this class/interface for the flexible handling. 29 : */ 30 : interface ModificationIntention { 31 : 32 : ModificationTarget getTargetWhenEditExists(ChangeEdit changeEdit); 33 : 34 : ModificationTarget getTargetWhenNoEdit(ChangeNotes notes); 35 : 36 : /** A specific patchset is the modification target. */ 37 : class PatchsetCommit implements ModificationIntention { 38 : 39 : private final PatchSet patchSet; 40 : 41 3 : PatchsetCommit(PatchSet patchSet) { 42 3 : this.patchSet = patchSet; 43 3 : } 44 : 45 : @Override 46 : public ModificationTarget getTargetWhenEditExists(ChangeEdit changeEdit) { 47 2 : return new ModificationTarget.PatchsetCommit(patchSet); 48 : } 49 : 50 : @Override 51 : public ModificationTarget getTargetWhenNoEdit(ChangeNotes notes) { 52 3 : return new ModificationTarget.PatchsetCommit(patchSet); 53 : } 54 : } 55 : 56 : /** 57 : * The latest commit should be the modification target. If a change edit exists, it's considered 58 : * to be the latest commit. Otherwise, defer to the latest patchset commit. 59 : */ 60 22 : class LatestCommit implements ModificationIntention { 61 : 62 : @Override 63 : public ModificationTarget getTargetWhenEditExists(ChangeEdit changeEdit) { 64 12 : return new ModificationTarget.EditCommit(changeEdit); 65 : } 66 : 67 : @Override 68 : public ModificationTarget getTargetWhenNoEdit(ChangeNotes notes) { 69 17 : return new ModificationTarget.PatchsetCommit(notes.getCurrentPatchSet()); 70 : } 71 : } 72 : }