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.gerrit.server.project.ProjectCache.illegalState; 18 : 19 : import com.google.gerrit.entities.SubmitRecord; 20 : import com.google.gerrit.entities.SubmitTypeRecord; 21 : import com.google.gerrit.server.project.ProjectCache; 22 : import com.google.gerrit.server.project.ProjectState; 23 : import com.google.gerrit.server.query.change.ChangeData; 24 : import com.google.inject.Inject; 25 : import com.google.inject.Singleton; 26 : import java.util.Optional; 27 : 28 : @Singleton 29 : public class PrologRule implements SubmitRule { 30 : private final PrologRuleEvaluator.Factory factory; 31 : private final ProjectCache projectCache; 32 : 33 : @Inject 34 146 : private PrologRule(PrologRuleEvaluator.Factory factory, ProjectCache projectCache) { 35 146 : this.factory = factory; 36 146 : this.projectCache = projectCache; 37 146 : } 38 : 39 : @Override 40 : public Optional<SubmitRecord> evaluate(ChangeData cd) { 41 103 : ProjectState projectState = 42 103 : projectCache.get(cd.project()).orElseThrow(illegalState(cd.project())); 43 : // We only want to run the Prolog engine if we have at least one rules.pl file to use. 44 103 : if (!projectState.hasPrologRules()) { 45 103 : return Optional.empty(); 46 : } 47 : 48 5 : return Optional.of(evaluate(cd, PrologOptions.defaultOptions())); 49 : } 50 : 51 : public SubmitRecord evaluate(ChangeData cd, PrologOptions opts) { 52 5 : return getEvaluator(cd, opts).evaluate(); 53 : } 54 : 55 : public SubmitTypeRecord getSubmitType(ChangeData cd) { 56 103 : return getSubmitType(cd, PrologOptions.defaultOptions()); 57 : } 58 : 59 : public SubmitTypeRecord getSubmitType(ChangeData cd, PrologOptions opts) { 60 103 : return getEvaluator(cd, opts).getSubmitType(); 61 : } 62 : 63 : private PrologRuleEvaluator getEvaluator(ChangeData cd, PrologOptions opts) { 64 103 : return factory.create(cd, opts); 65 : } 66 : }