Line data Source code
1 : // Copyright (C) 2018 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.rules; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : import static com.google.common.collect.ImmutableList.toImmutableList; 19 : 20 : import com.google.gerrit.entities.LabelFunction; 21 : import com.google.gerrit.entities.LabelType; 22 : import com.google.gerrit.entities.PatchSetApproval; 23 : import com.google.gerrit.entities.SubmitRecord; 24 : import com.google.gerrit.extensions.annotations.Exports; 25 : import com.google.gerrit.extensions.config.FactoryModule; 26 : import com.google.gerrit.server.query.change.ChangeData; 27 : import com.google.inject.Singleton; 28 : import java.util.ArrayList; 29 : import java.util.Collection; 30 : import java.util.List; 31 : import java.util.Optional; 32 : 33 : /** 34 : * Java implementation of Gerrit's default pre-submit rules behavior: check if the labels have the 35 : * correct values, according to the {@link LabelFunction} they are attached to. 36 : * 37 : * <p>As this behavior is also implemented by the Prolog rules system, we skip it if at least one 38 : * project in the hierarchy has a {@code rules.pl} file. 39 : */ 40 : @Singleton 41 146 : public final class DefaultSubmitRule implements SubmitRule { 42 : public static final String RULE_NAME = "gerrit~DefaultSubmitRule"; 43 : 44 152 : public static class DefaultSubmitRuleModule extends FactoryModule { 45 : @Override 46 : public void configure() { 47 152 : bind(SubmitRule.class) 48 152 : .annotatedWith(Exports.named("DefaultRules")) 49 152 : .to(DefaultSubmitRule.class); 50 152 : } 51 : } 52 : 53 : @Override 54 : public Optional<SubmitRecord> evaluate(ChangeData cd) { 55 103 : SubmitRecord submitRecord = new SubmitRecord(); 56 103 : submitRecord.status = SubmitRecord.Status.OK; 57 : 58 103 : List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes(); 59 103 : List<PatchSetApproval> approvals = cd.currentApprovals(); 60 103 : submitRecord.labels = new ArrayList<>(labelTypes.size()); 61 : 62 103 : for (LabelType t : labelTypes) { 63 103 : LabelFunction labelFunction = t.getFunction(); 64 103 : checkState( 65 : labelFunction != null, 66 : "Unable to find the LabelFunction for label %s, change %s", 67 103 : t.getName(), 68 103 : cd.getId()); 69 : 70 103 : Collection<PatchSetApproval> approvalsForLabel = getApprovalsForLabel(approvals, t); 71 103 : SubmitRecord.Label label = labelFunction.check(t, approvalsForLabel); 72 103 : submitRecord.labels.add(label); 73 : 74 103 : switch (label.status) { 75 : case OK: 76 : case MAY: 77 55 : break; 78 : 79 : case NEED: 80 : case REJECT: 81 : case IMPOSSIBLE: 82 103 : submitRecord.status = SubmitRecord.Status.NOT_READY; 83 : break; 84 : } 85 103 : } 86 : 87 103 : return Optional.of(submitRecord); 88 : } 89 : 90 : private static List<PatchSetApproval> getApprovalsForLabel( 91 : List<PatchSetApproval> approvals, LabelType t) { 92 103 : return approvals.stream() 93 103 : .filter(input -> input.label().equals(t.getLabelId().get())) 94 103 : .collect(toImmutableList()); 95 : } 96 : }