Line data Source code
1 : // Copyright (C) 2019 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.git; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.common.collect.ImmutableList; 21 : import com.google.gerrit.common.Nullable; 22 : import com.google.gerrit.common.UsedAt; 23 : import java.io.IOException; 24 : import java.util.Optional; 25 : import org.eclipse.jgit.lib.BatchRefUpdate; 26 : import org.eclipse.jgit.lib.RefUpdate; 27 : import org.eclipse.jgit.transport.ReceiveCommand; 28 : 29 : /** Thrown when updating a ref in Git fails. */ 30 : public class GitUpdateFailureException extends IOException { 31 : private static final long serialVersionUID = 1L; 32 : 33 : private final ImmutableList<GitUpdateFailure> failures; 34 : 35 : public GitUpdateFailureException(String message, RefUpdate refUpdate) { 36 1 : super(message); 37 1 : this.failures = ImmutableList.of(GitUpdateFailure.create(refUpdate)); 38 1 : } 39 : 40 : public GitUpdateFailureException(String message, BatchRefUpdate batchRefUpdate) { 41 12 : super(message); 42 12 : this.failures = 43 12 : batchRefUpdate.getCommands().stream() 44 12 : .filter(c -> c.getResult() != ReceiveCommand.Result.OK) 45 12 : .map(GitUpdateFailure::create) 46 12 : .collect(toImmutableList()); 47 12 : } 48 : 49 : /** Returns the names of the refs for which the update failed. */ 50 : public ImmutableList<String> getFailedRefs() { 51 1 : return failures.stream().map(GitUpdateFailure::ref).collect(toImmutableList()); 52 : } 53 : 54 : /** Returns the failures that caused this exception. */ 55 : @UsedAt(UsedAt.Project.GOOGLE) 56 : public ImmutableList<GitUpdateFailure> getFailures() { 57 0 : return failures; 58 : } 59 : 60 : @AutoValue 61 13 : public abstract static class GitUpdateFailure { 62 : private static GitUpdateFailure create(RefUpdate refUpdate) { 63 1 : return builder().ref(refUpdate.getName()).result(refUpdate.getResult().name()).build(); 64 : } 65 : 66 : private static GitUpdateFailure create(ReceiveCommand receiveCommand) { 67 12 : return builder() 68 12 : .ref(receiveCommand.getRefName()) 69 12 : .result(receiveCommand.getResult().name()) 70 12 : .message(receiveCommand.getMessage()) 71 12 : .build(); 72 : } 73 : 74 : public abstract String ref(); 75 : 76 : public abstract String result(); 77 : 78 : public abstract Optional<String> message(); 79 : 80 : public static GitUpdateFailure.Builder builder() { 81 13 : return new AutoValue_GitUpdateFailureException_GitUpdateFailure.Builder(); 82 : } 83 : 84 : @AutoValue.Builder 85 13 : abstract static class Builder { 86 : abstract Builder ref(String ref); 87 : 88 : abstract Builder result(String result); 89 : 90 : abstract Builder message(@Nullable String message); 91 : 92 : abstract GitUpdateFailure build(); 93 : } 94 : } 95 : }