Line data Source code
1 : // Copyright (C) 2022 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.query.change; 16 : 17 : import com.google.gerrit.index.query.QueryParseException; 18 : import dk.brics.automaton.RegExp; 19 : import dk.brics.automaton.RunAutomaton; 20 : 21 : /** 22 : * A submit requirement predicate that matches with changes having the author email's address 23 : * matching a specific regular expression pattern. 24 : */ 25 : public class RegexAuthorEmailPredicate extends SubmitRequirementPredicate { 26 : protected final RunAutomaton authorEmailPattern; 27 : 28 : public RegexAuthorEmailPredicate(String pattern) throws QueryParseException { 29 1 : super("authoremail", pattern); 30 : 31 1 : if (pattern.startsWith("^")) { 32 1 : pattern = pattern.substring(1); 33 : } 34 : 35 1 : if (pattern.endsWith("$") && !pattern.endsWith("\\$")) { 36 1 : pattern = pattern.substring(0, pattern.length() - 1); 37 : } 38 : 39 : try { 40 1 : this.authorEmailPattern = new RunAutomaton(new RegExp(pattern).toAutomaton()); 41 0 : } catch (IllegalArgumentException e) { 42 0 : throw new QueryParseException(String.format("invalid regular expression: %s", pattern), e); 43 1 : } 44 1 : } 45 : 46 : @Override 47 : public boolean match(ChangeData cd) { 48 1 : return authorEmailPattern.run(cd.getAuthor().getEmailAddress()); 49 : } 50 : 51 : @Override 52 : public int getCost() { 53 0 : return 1; 54 : } 55 : }