Predicates to expose change/commit info to Prolog

Adds new predicates that expose the branch, owner, project, and
topic of a change, the author and committer of the most recent
patchset in the change, and who is the current user.

For user-related predicates, if the user is not a gerrit user, will
return user(anonymous) or similar. Author and committer predicates for
commits return user(id), name, and email.

Change-Id: Ic79564f3f3a070faa4082a47954597fa3717a065
This commit is contained in:
Jason Tsay
2011-06-21 13:25:52 -07:00
parent 1244ed0574
commit 6c6700f9b7
10 changed files with 496 additions and 1 deletions

View File

@@ -18,6 +18,7 @@ import static com.google.gerrit.rules.StoredValue.create;
import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.PatchSet;
import com.google.gerrit.reviewdb.PatchSetInfo;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.server.project.ChangeControl;
@@ -26,6 +27,7 @@ public final class StoredValues {
public static final StoredValue<Change> CHANGE = create(Change.class);
public static final StoredValue<PatchSet.Id> PATCH_SET_ID = create(PatchSet.Id.class);
public static final StoredValue<ChangeControl> CHANGE_CONTROL = create(ChangeControl.class);
public static final StoredValue<PatchSetInfo> PATCH_SET_INFO = create(PatchSetInfo.class);
private StoredValues() {
}

View File

@@ -0,0 +1,100 @@
// 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.PatchSet;
import com.google.gerrit.reviewdb.PatchSetInfo;
import com.google.gerrit.reviewdb.UserIdentity;
import com.google.gerrit.rules.PrologEnvironment;
import com.google.gerrit.rules.StoredValues;
import com.google.gerrit.server.patch.PatchSetInfoFactory;
import com.google.gerrit.server.patch.PatchSetInfoNotAvailableException;
import com.googlecode.prolog_cafe.lang.IntegerTerm;
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;
abstract class AbstractCommitUserIdentityPredicate extends Predicate.P3 {
private static final long serialVersionUID = 1L;
private static final SymbolTerm user = SymbolTerm.intern("user", 1);
private static final SymbolTerm anonymous = SymbolTerm.intern("anonymous");
AbstractCommitUserIdentityPredicate(Term a1, Term a2, Term a3, Operation n) {
arg1 = a1;
arg2 = a2;
arg3 = a3;
cont = n;
}
protected Operation exec(Prolog engine, UserIdentity userId) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
Term a2 = arg2.dereference();
Term a3 = arg3.dereference();
Term idTerm;
Term nameTerm = Prolog.Nil;
Term emailTerm = Prolog.Nil;
Account.Id id = userId.getAccount();
if (id == null) {
idTerm = anonymous;
} else {
idTerm = new IntegerTerm(id.get());
}
String name = userId.getName();
if (name != null && !name.equals("")) {
nameTerm = SymbolTerm.create(name);
}
String email = userId.getEmail();
if (email != null && !email.equals("")) {
emailTerm = SymbolTerm.create(email);
}
if (!a1.unify(new StructureTerm(user, idTerm), engine.trail)) {
return engine.fail();
}
if (!a2.unify(nameTerm, engine.trail)) {
return engine.fail();
}
if (!a3.unify(emailTerm, engine.trail)) {
return engine.fail();
}
return cont;
}
protected PatchSetInfo getPatchSetInfo(Prolog engine)
throws PatchSetInfoNotAvailableException {
PrologEnvironment env = (PrologEnvironment) engine.control;
PatchSetInfo psInfo = env.get(StoredValues.PATCH_SET_INFO);
if (psInfo == null) {
PatchSet.Id patchSetId = env.get(StoredValues.PATCH_SET_ID);
PatchSetInfoFactory patchInfoFactory =
env.getInjector().getInstance(PatchSetInfoFactory.class);
psInfo = patchInfoFactory.get(patchSetId);
env.set(StoredValues.PATCH_SET_INFO, psInfo);
}
return psInfo;
}
}

View File

@@ -0,0 +1,51 @@
// 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.Branch;
import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.rules.PrologEnvironment;
import com.google.gerrit.rules.StoredValues;
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.SymbolTerm;
import com.googlecode.prolog_cafe.lang.Term;
public class PRED_change_branch_1 extends Predicate.P1 {
private static final long serialVersionUID = 1L;
public PRED_change_branch_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;
Change change = StoredValues.CHANGE.get(engine);
Branch.NameKey name = change.getDest();
if (!a1.unify(SymbolTerm.create(name.get()), engine.trail)) {
return engine.fail();
}
return cont;
}
}

View File

@@ -0,0 +1,54 @@
// 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.Change;
import com.google.gerrit.rules.PrologEnvironment;
import com.google.gerrit.rules.StoredValues;
import com.googlecode.prolog_cafe.lang.IntegerTerm;
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;
public class PRED_change_owner_1 extends Predicate.P1 {
private static final long serialVersionUID = 1L;
private static final SymbolTerm user = SymbolTerm.intern("user", 1);
public PRED_change_owner_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;
Change change = StoredValues.CHANGE.get(engine);
Account.Id ownerId = change.getOwner();
if (!a1.unify(new StructureTerm(user, new IntegerTerm(ownerId.get())), engine.trail)) {
return engine.fail();
}
return cont;
}
}

View File

@@ -0,0 +1,51 @@
// 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.Change;
import com.google.gerrit.reviewdb.Project;
import com.google.gerrit.rules.PrologEnvironment;
import com.google.gerrit.rules.StoredValues;
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.SymbolTerm;
import com.googlecode.prolog_cafe.lang.Term;
public class PRED_change_project_1 extends Predicate.P1 {
private static final long serialVersionUID = 1L;
public PRED_change_project_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;
Change change = StoredValues.CHANGE.get(engine);
Project.NameKey name = change.getProject();
if (!a1.unify(SymbolTerm.create(name.get()), engine.trail)) {
return engine.fail();
}
return cont;
}
}

View File

@@ -0,0 +1,54 @@
// 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.Change;
import com.google.gerrit.rules.PrologEnvironment;
import com.google.gerrit.rules.StoredValues;
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.SymbolTerm;
import com.googlecode.prolog_cafe.lang.Term;
public class PRED_change_topic_1 extends Predicate.P1 {
private static final long serialVersionUID = 1L;
public PRED_change_topic_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;
Term topicTerm = Prolog.Nil;
Change change = StoredValues.CHANGE.get(engine);
String topic = change.getTopic();
if (topic != null) {
topicTerm = SymbolTerm.create(topic);
}
if (!a1.unify(topicTerm, engine.trail)) {
return engine.fail();
}
return cont;
}
}

View File

@@ -0,0 +1,45 @@
// 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.PatchSetInfo;
import com.google.gerrit.reviewdb.UserIdentity;
import com.google.gerrit.server.patch.PatchSetInfoNotAvailableException;
import com.googlecode.prolog_cafe.lang.JavaException;
import com.googlecode.prolog_cafe.lang.Operation;
import com.googlecode.prolog_cafe.lang.Prolog;
import com.googlecode.prolog_cafe.lang.PrologException;
import com.googlecode.prolog_cafe.lang.Term;
public class PRED_commit_author_3 extends AbstractCommitUserIdentityPredicate {
private static final long serialVersionUID = 1L;
public PRED_commit_author_3(Term a1, Term a2, Term a3, Operation n) {
super(a1, a2, a3, n);
}
@Override
public Operation exec(Prolog engine) throws PrologException {
PatchSetInfo psInfo;
try {
psInfo = getPatchSetInfo(engine);
} catch (PatchSetInfoNotAvailableException err) {
throw new JavaException(this, 1, err);
}
UserIdentity author = psInfo.getAuthor();
return exec(engine, author);
}
}

View File

@@ -0,0 +1,45 @@
// 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.PatchSetInfo;
import com.google.gerrit.reviewdb.UserIdentity;
import com.google.gerrit.server.patch.PatchSetInfoNotAvailableException;
import com.googlecode.prolog_cafe.lang.JavaException;
import com.googlecode.prolog_cafe.lang.Operation;
import com.googlecode.prolog_cafe.lang.Prolog;
import com.googlecode.prolog_cafe.lang.PrologException;
import com.googlecode.prolog_cafe.lang.Term;
public class PRED_commit_committer_3 extends AbstractCommitUserIdentityPredicate {
private static final long serialVersionUID = 1L;
public PRED_commit_committer_3(Term a1, Term a2, Term a3, Operation n) {
super(a1, a2, a3, n);
}
@Override
public Operation exec(Prolog engine) throws PrologException {
PatchSetInfo psInfo;
try {
psInfo = getPatchSetInfo(engine);
} catch (PatchSetInfoNotAvailableException err) {
throw new JavaException(this, 1, err);
}
UserIdentity committer = psInfo.getCommitter();
return exec(engine, committer);
}
}

View File

@@ -0,0 +1,77 @@
// 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.rules.PrologEnvironment;
import com.google.gerrit.rules.StoredValues;
import com.google.gerrit.server.AnonymousUser;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.PeerDaemonUser;
import com.google.gerrit.server.ReplicationUser;
import com.google.gerrit.server.project.ChangeControl;
import com.googlecode.prolog_cafe.lang.EvaluationException;
import com.googlecode.prolog_cafe.lang.IntegerTerm;
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;
public class PRED_current_user_1 extends Predicate.P1 {
private static final long serialVersionUID = 1L;
private static final SymbolTerm user = SymbolTerm.intern("user", 1);
private static final SymbolTerm anonymous = SymbolTerm.intern("anonymous");
private static final SymbolTerm peerDaemon = SymbolTerm.intern("peer_daemon");
private static final SymbolTerm replication = SymbolTerm.intern("replication");
public PRED_current_user_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;
ChangeControl cControl = StoredValues.CHANGE_CONTROL.get(engine);
CurrentUser curUser = cControl.getCurrentUser();
Term resultTerm;
if (curUser instanceof IdentifiedUser) {
Account.Id id = ((IdentifiedUser)curUser).getAccountId();
resultTerm = new IntegerTerm(id.get());
} else if (curUser instanceof AnonymousUser) {
resultTerm = anonymous;
} else if (curUser instanceof PeerDaemonUser) {
resultTerm = peerDaemon;
} else if (curUser instanceof ReplicationUser) {
resultTerm = replication;
} else {
throw new EvaluationException("Unknown user type");
}
if (!a1.unify(new StructureTerm(user, resultTerm), engine.trail)) {
return engine.fail();
}
return cont;
}
}

View File

@@ -282,3 +282,19 @@ check_label_range_permission(Label, ExpValue, ok(Who)) :-
% grant_range(Label, Group, Min, Max),
% Min @=< ExpValue, ExpValue @=< Max
% .
%% commit_author/1:
%%
:- public commit_author/1.
%%
commit_author(Author) :-
commit_author(Author, _, _).
%% commit_committer/1:
%%
:- public commit_committer/1.
%%
commit_committer(Committer) :-
commit_committer(Committer, _, _).