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.git.receive; 16 : 17 : import com.google.auto.value.AutoValue; 18 : import com.google.common.collect.ImmutableMap; 19 : import com.google.common.collect.ImmutableSet; 20 : import com.google.common.collect.Maps; 21 : import com.google.gerrit.entities.Change; 22 : import java.util.Arrays; 23 : import java.util.EnumMap; 24 : 25 : /** Keeps track of the change IDs thus far updated by {@link ReceiveCommits}. */ 26 : @AutoValue 27 96 : public abstract class ReceiveCommitsResult { 28 : /** Status of a change. Used to aggregate metrics. */ 29 97 : public enum ChangeStatus { 30 97 : CREATED, 31 97 : REPLACED, 32 97 : AUTOCLOSED, 33 : } 34 : 35 : /** 36 : * Returns change IDs of the given type for which the BatchUpdate succeeded, or empty list if 37 : * there are none. 38 : */ 39 : public abstract ImmutableMap<ChangeStatus, ImmutableSet<Change.Id>> changes(); 40 : 41 : /** Indicate that the ReceiveCommits call involved a magic branch, such as {@code refs/for/}. */ 42 : public abstract boolean magicPush(); 43 : 44 : public static Builder builder() { 45 97 : return new AutoValue_ReceiveCommitsResult.Builder().magicPush(false); 46 : } 47 : 48 : public static ReceiveCommitsResult empty() { 49 3 : return builder().build(); 50 : } 51 : 52 : @AutoValue.Builder 53 : public abstract static class Builder { 54 : private EnumMap<ChangeStatus, ImmutableSet.Builder<Change.Id>> changes; 55 : 56 97 : Builder() { 57 97 : changes = Maps.newEnumMap(ChangeStatus.class); 58 97 : Arrays.stream(ChangeStatus.values()).forEach(k -> changes.put(k, ImmutableSet.builder())); 59 97 : } 60 : 61 : /** Record a change ID update as having completed. */ 62 : public Builder addChange(ChangeStatus key, Change.Id id) { 63 88 : changes.get(key).add(id); 64 88 : return this; 65 : } 66 : 67 : public abstract Builder magicPush(boolean isMagicPush); 68 : 69 : public ReceiveCommitsResult build() { 70 : ImmutableMap.Builder<ChangeStatus, ImmutableSet<Change.Id>> changesBuilder = 71 96 : ImmutableMap.builder(); 72 96 : changes.entrySet().forEach(e -> changesBuilder.put(e.getKey(), e.getValue().build())); 73 96 : changes(changesBuilder.build()); 74 96 : return autoBuild(); 75 : } 76 : 77 : protected abstract Builder changes(ImmutableMap<ChangeStatus, ImmutableSet<Change.Id>> changes); 78 : 79 : protected abstract ReceiveCommitsResult autoBuild(); 80 : } 81 : }