Move common Prolog predicates to 'gerrit' package
Because user code will need to import our common rules, move them to a shorter name "gerrit". This is not likely to collide with any classes in a running server as we will eventually narrow down the set of classes available to the PrologEnvironment to a smaller set than the entire running server, and we aren't likely to ever use a 3rd party library from the gerrit.com domain. Change-Id: I8f111ea2cb58229159d3a72c081a3808cc6c3438
This commit is contained in:
87
gerrit-server/src/main/java/gerrit/PRED_$current_user_2.java
Normal file
87
gerrit-server/src/main/java/gerrit/PRED_$current_user_2.java
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright (C) 2011 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package gerrit;
|
||||
|
||||
import com.google.gerrit.reviewdb.Account;
|
||||
import com.google.gerrit.reviewdb.ReviewDb;
|
||||
import com.google.gerrit.rules.PrologEnvironment;
|
||||
import com.google.gerrit.rules.StoredValues;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
import com.googlecode.prolog_cafe.lang.IllegalTypeException;
|
||||
import com.googlecode.prolog_cafe.lang.IntegerTerm;
|
||||
import com.googlecode.prolog_cafe.lang.JavaObjectTerm;
|
||||
import com.googlecode.prolog_cafe.lang.Operation;
|
||||
import com.googlecode.prolog_cafe.lang.PInstantiationException;
|
||||
import com.googlecode.prolog_cafe.lang.Predicate;
|
||||
import com.googlecode.prolog_cafe.lang.Prolog;
|
||||
import com.googlecode.prolog_cafe.lang.PrologException;
|
||||
import com.googlecode.prolog_cafe.lang.Term;
|
||||
|
||||
/**
|
||||
* Loads a CurrentUser object for a user identity.
|
||||
*
|
||||
* <pre>
|
||||
* '$current_user'(+AccountId, -CurrentUser).
|
||||
* </pre>
|
||||
*/
|
||||
class PRED_$current_user_2 extends Predicate.P2 {
|
||||
PRED_$current_user_2(Term a1, Term a2, Operation n) {
|
||||
arg1 = a1;
|
||||
arg2 = a2;
|
||||
cont = n;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Operation exec(Prolog engine) throws PrologException {
|
||||
engine.setB0();
|
||||
Term a1 = arg1.dereference();
|
||||
Term a2 = arg2.dereference();
|
||||
|
||||
if (a1.isVariable()) {
|
||||
throw new PInstantiationException(this, 1);
|
||||
}
|
||||
if (!a1.isInteger()) {
|
||||
throw new IllegalTypeException(this, 1, "int", a1);
|
||||
}
|
||||
Account.Id accountId = new Account.Id(((IntegerTerm) a1).intValue());
|
||||
|
||||
final ReviewDb db = StoredValues.REVIEW_DB.getOrNull(engine);
|
||||
IdentifiedUser.GenericFactory userFactory = userFactory(engine);
|
||||
CurrentUser user;
|
||||
if (db != null) {
|
||||
user = userFactory.create(new Provider<ReviewDb>() {
|
||||
public ReviewDb get() {
|
||||
return db;
|
||||
}
|
||||
}, accountId);
|
||||
} else {
|
||||
user = userFactory.create(accountId);
|
||||
}
|
||||
|
||||
if (!a2.unify(new JavaObjectTerm(user), engine.trail)) {
|
||||
return engine.fail();
|
||||
}
|
||||
|
||||
return cont;
|
||||
}
|
||||
|
||||
private IdentifiedUser.GenericFactory userFactory(Prolog engine) {
|
||||
PrologEnvironment env = (PrologEnvironment) engine.control;
|
||||
return env.getInjector().getInstance(IdentifiedUser.GenericFactory.class);
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
// Copyright 2011 Google Inc. All Rights Reserved.
|
||||
|
||||
package gerrit;
|
||||
|
||||
import com.google.gerrit.common.data.ApprovalType;
|
||||
import com.google.gerrit.common.data.ApprovalTypes;
|
||||
import com.google.gerrit.reviewdb.PatchSet;
|
||||
import com.google.gerrit.reviewdb.PatchSetApproval;
|
||||
import com.google.gerrit.reviewdb.ReviewDb;
|
||||
import com.google.gerrit.rules.PrologEnvironment;
|
||||
import com.google.gerrit.rules.StoredValues;
|
||||
import com.google.gwtorm.client.OrmException;
|
||||
|
||||
import com.googlecode.prolog_cafe.lang.IntegerTerm;
|
||||
import com.googlecode.prolog_cafe.lang.JavaException;
|
||||
import com.googlecode.prolog_cafe.lang.ListTerm;
|
||||
import com.googlecode.prolog_cafe.lang.Operation;
|
||||
import com.googlecode.prolog_cafe.lang.Predicate;
|
||||
import com.googlecode.prolog_cafe.lang.Prolog;
|
||||
import com.googlecode.prolog_cafe.lang.PrologException;
|
||||
import com.googlecode.prolog_cafe.lang.StructureTerm;
|
||||
import com.googlecode.prolog_cafe.lang.SymbolTerm;
|
||||
import com.googlecode.prolog_cafe.lang.Term;
|
||||
|
||||
/** Exports list of {@code commit_label( label('Code-Review', 2), user(12345789) )}. */
|
||||
class PRED_$load_commit_labels_1 extends Predicate.P1 {
|
||||
private static final SymbolTerm sym_commit_label = SymbolTerm.makeSymbol("commit_label", 2);
|
||||
private static final SymbolTerm sym_label = SymbolTerm.makeSymbol("label", 2);
|
||||
private static final SymbolTerm sym_user = SymbolTerm.makeSymbol("user", 1);
|
||||
|
||||
PRED_$load_commit_labels_1(Term a1, Operation n) {
|
||||
arg1 = a1;
|
||||
cont = n;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Operation exec(Prolog engine) throws PrologException {
|
||||
engine.setB0();
|
||||
Term a1 = arg1.dereference();
|
||||
|
||||
Term listHead = Prolog.Nil;
|
||||
try {
|
||||
PrologEnvironment env = (PrologEnvironment) engine.control;
|
||||
ReviewDb db = StoredValues.REVIEW_DB.get(engine);
|
||||
PatchSet.Id patchSetId = StoredValues.PATCH_SET_ID.get(engine);
|
||||
ApprovalTypes types = env.getInjector().getInstance(ApprovalTypes.class);
|
||||
|
||||
for (PatchSetApproval a : db.patchSetApprovals().byPatchSet(patchSetId)) {
|
||||
if (a.getValue() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ApprovalType t = types.byId(a.getCategoryId());
|
||||
if (t == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
StructureTerm labelTerm = new StructureTerm(
|
||||
sym_label,
|
||||
SymbolTerm.makeSymbol(t.getCategory().getLabelName()),
|
||||
new IntegerTerm(a.getValue()));
|
||||
|
||||
StructureTerm userTerm = new StructureTerm(
|
||||
sym_user,
|
||||
new IntegerTerm(a.getAccountId().get()));
|
||||
|
||||
listHead = new ListTerm(
|
||||
new StructureTerm(sym_commit_label, labelTerm, userTerm),
|
||||
listHead);
|
||||
}
|
||||
} catch (OrmException err) {
|
||||
throw new JavaException(this, 1, err);
|
||||
}
|
||||
|
||||
if (!a1.unify(listHead, engine.trail)) {
|
||||
return engine.fail();
|
||||
}
|
||||
return cont;
|
||||
}
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
// Copyright (C) 2011 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package gerrit;
|
||||
|
||||
import com.google.gerrit.common.data.Permission;
|
||||
import com.google.gerrit.common.data.PermissionRange;
|
||||
import com.google.gerrit.rules.StoredValues;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.project.ChangeControl;
|
||||
|
||||
import com.googlecode.prolog_cafe.lang.IllegalTypeException;
|
||||
import com.googlecode.prolog_cafe.lang.IntegerTerm;
|
||||
import com.googlecode.prolog_cafe.lang.JavaObjectTerm;
|
||||
import com.googlecode.prolog_cafe.lang.Operation;
|
||||
import com.googlecode.prolog_cafe.lang.PInstantiationException;
|
||||
import com.googlecode.prolog_cafe.lang.Predicate;
|
||||
import com.googlecode.prolog_cafe.lang.Prolog;
|
||||
import com.googlecode.prolog_cafe.lang.PrologException;
|
||||
import com.googlecode.prolog_cafe.lang.Term;
|
||||
|
||||
/**
|
||||
* Resolves the valid range for a label on a CurrentUser.
|
||||
*
|
||||
* <pre>
|
||||
* '$user_label_range'(+Label, +CurrentUser, -Min, -Max)
|
||||
* </pre>
|
||||
*/
|
||||
class PRED_$user_label_range_4 extends Predicate.P4 {
|
||||
PRED_$user_label_range_4(Term a1, Term a2, Term a3, Term a4, Operation n) {
|
||||
arg1 = a1;
|
||||
arg2 = a2;
|
||||
arg3 = a3;
|
||||
arg4 = a4;
|
||||
cont = n;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Operation exec(Prolog engine) throws PrologException {
|
||||
engine.setB0();
|
||||
Term a1 = arg1.dereference();
|
||||
Term a2 = arg2.dereference();
|
||||
Term a3 = arg3.dereference();
|
||||
Term a4 = arg4.dereference();
|
||||
|
||||
if (a1.isVariable()) {
|
||||
throw new PInstantiationException(this, 1);
|
||||
}
|
||||
if (!a1.isSymbol()) {
|
||||
throw new IllegalTypeException(this, 1, "atom", a1);
|
||||
}
|
||||
String label = a1.name();
|
||||
|
||||
if (a2.isVariable()) {
|
||||
throw new PInstantiationException(this, 2);
|
||||
}
|
||||
if (!a2.isJavaObject() || !a2.convertible(CurrentUser.class)) {
|
||||
throw new IllegalTypeException(this, 2, "CurrentUser)", a2);
|
||||
}
|
||||
CurrentUser user = (CurrentUser) ((JavaObjectTerm) a2).object();
|
||||
|
||||
ChangeControl ctl = StoredValues.CHANGE_CONTROL.get(engine).forUser(user);
|
||||
PermissionRange range = ctl.getRange(Permission.LABEL + label);
|
||||
if (range == null) {
|
||||
return engine.fail();
|
||||
}
|
||||
|
||||
IntegerTerm min = new IntegerTerm(range.getMin());
|
||||
IntegerTerm max = new IntegerTerm(range.getMax());
|
||||
|
||||
if (!a3.unify(min, engine.trail)) {
|
||||
return engine.fail();
|
||||
}
|
||||
|
||||
if (!a4.unify(max, engine.trail)) {
|
||||
return engine.fail();
|
||||
}
|
||||
|
||||
return cont;
|
||||
}
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
// Copyright (C) 2011 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package gerrit;
|
||||
|
||||
import com.google.gerrit.common.data.ApprovalType;
|
||||
import com.google.gerrit.common.data.ApprovalTypes;
|
||||
import com.google.gerrit.rules.PrologEnvironment;
|
||||
|
||||
import com.googlecode.prolog_cafe.lang.IntegerTerm;
|
||||
import com.googlecode.prolog_cafe.lang.ListTerm;
|
||||
import com.googlecode.prolog_cafe.lang.Operation;
|
||||
import com.googlecode.prolog_cafe.lang.Predicate;
|
||||
import com.googlecode.prolog_cafe.lang.Prolog;
|
||||
import com.googlecode.prolog_cafe.lang.PrologException;
|
||||
import com.googlecode.prolog_cafe.lang.StructureTerm;
|
||||
import com.googlecode.prolog_cafe.lang.SymbolTerm;
|
||||
import com.googlecode.prolog_cafe.lang.Term;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Obtain a list of approval types from the server configuration.
|
||||
* <p>
|
||||
* Unifies to a Prolog list of: {@code approval_type(Label, Id, Fun, Min, Max)}
|
||||
* where:
|
||||
* <ul>
|
||||
* <li>{@code Label} - the newer style label name</li>
|
||||
* <li>{@code Id} - the legacy ApprovalCategory.Id from the database</li>
|
||||
* <li>{@code Fun} - legacy function name</li>
|
||||
* <li>{@code Min, Max} - the smallest and largest configured values.</li>
|
||||
* </ul>
|
||||
*/
|
||||
class PRED_get_legacy_approval_types_1 extends Predicate.P1 {
|
||||
PRED_get_legacy_approval_types_1(Term a1, Operation n) {
|
||||
arg1 = a1;
|
||||
cont = n;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Operation exec(Prolog engine) throws PrologException {
|
||||
engine.setB0();
|
||||
Term a1 = arg1.dereference();
|
||||
|
||||
PrologEnvironment env = (PrologEnvironment) engine.control;
|
||||
ApprovalTypes types = env.getInjector().getInstance(ApprovalTypes.class);
|
||||
|
||||
List<ApprovalType> list = types.getApprovalTypes();
|
||||
Term head = Prolog.Nil;
|
||||
for (int idx = list.size() - 1; 0 <= idx; idx--) {
|
||||
head = new ListTerm(export(list.get(idx)), head);
|
||||
}
|
||||
|
||||
if (!a1.unify(head, engine.trail)) {
|
||||
return engine.fail();
|
||||
}
|
||||
return cont;
|
||||
}
|
||||
|
||||
static final SymbolTerm symApprovalType = SymbolTerm.makeSymbol(
|
||||
"approval_type", 5);
|
||||
|
||||
static Term export(ApprovalType type) {
|
||||
return new StructureTerm(symApprovalType,
|
||||
SymbolTerm.makeSymbol(type.getCategory().getLabelName()),
|
||||
SymbolTerm.makeSymbol(type.getCategory().getId().get()),
|
||||
SymbolTerm.makeSymbol(type.getCategory().getFunctionName()),
|
||||
new IntegerTerm(type.getMin().getValue()),
|
||||
new IntegerTerm(type.getMax().getValue()));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user