Line data Source code
1 : // Copyright (C) 2017 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.git.validators; 16 : 17 : import static java.util.Objects.requireNonNull; 18 : 19 : import com.google.common.collect.ImmutableMap; 20 : import com.google.gerrit.entities.Project; 21 : import com.google.gerrit.extensions.annotations.ExtensionPoint; 22 : import com.google.gerrit.server.git.RefCache; 23 : import com.google.gerrit.server.update.ChainedReceiveCommands; 24 : import com.google.gerrit.server.validators.ValidationException; 25 : import java.io.IOException; 26 : import java.util.Optional; 27 : import org.eclipse.jgit.lib.ObjectId; 28 : import org.eclipse.jgit.revwalk.RevWalk; 29 : import org.eclipse.jgit.transport.ReceiveCommand; 30 : 31 : /** 32 : * Listener to validate ref updates performed during submit operation. 33 : * 34 : * <p>As submit strategies may generate new commits (e.g. Cherry Pick), this listener allows 35 : * validation of resulting new commit before destination branch is updated and new patchset ref is 36 : * created. 37 : * 38 : * <p>If you only care about validating the change being submitted and not the resulting new commit, 39 : * consider using {@link MergeValidationListener} instead. 40 : */ 41 : @ExtensionPoint 42 : public interface OnSubmitValidationListener { 43 : class Arguments { 44 : private Project.NameKey project; 45 : private RevWalk rw; 46 : private ImmutableMap<String, ReceiveCommand> commands; 47 : private RefCache refs; 48 : 49 : /** 50 : * @param project project. 51 : * @param rw revwalk that can read unflushed objects from {@code refs}. 52 : * @param commands commands to be executed. 53 : */ 54 53 : Arguments(Project.NameKey project, RevWalk rw, ChainedReceiveCommands commands) { 55 53 : this.project = requireNonNull(project); 56 53 : this.rw = requireNonNull(rw); 57 53 : this.refs = requireNonNull(commands); 58 53 : this.commands = ImmutableMap.copyOf(commands.getCommands()); 59 53 : } 60 : 61 : /** Get the project name for this operation. */ 62 : public Project.NameKey getProject() { 63 6 : return project; 64 : } 65 : 66 : /** 67 : * Get a revwalk for this operation. 68 : * 69 : * <p>This instance is able to read all objects mentioned in {@link #getCommands()} and {@link 70 : * #getRef(String)}. 71 : * 72 : * @return open revwalk. 73 : */ 74 : public RevWalk getRevWalk() { 75 6 : return rw; 76 : } 77 : 78 : /** 79 : * Returns a map from ref to commands covering all ref operations to be performed on this 80 : * repository as part of the ongoing submit operation. 81 : */ 82 : public ImmutableMap<String, ReceiveCommand> getCommands() { 83 6 : return commands; 84 : } 85 : 86 : /** 87 : * Get a ref from the repository. 88 : * 89 : * @param name ref name; can be any ref, not just the ones mentioned in {@link #getCommands()}. 90 : * @return latest value of a ref in the repository, as if all commands from {@link 91 : * #getCommands()} had already been applied. 92 : * @throws IOException if an error occurred reading the ref. 93 : */ 94 : public Optional<ObjectId> getRef(String name) throws IOException { 95 6 : return refs.get(name); 96 : } 97 : } 98 : 99 : /** 100 : * Called right before branch is updated with new commit or commits as a result of submit. 101 : * 102 : * <p>If ValidationException is thrown, submitting is aborted. 103 : */ 104 : void preBranchUpdate(Arguments args) throws ValidationException; 105 : }