Make Accounts available to Prolog predicates

Plugins like find-owners need to get account information
of a given email address when evaluating submit_rule.
The email address could be in OWNERS files.

Change-Id: I2fdf8fd26834963d8e65a79292ac2188b34e14cc
This commit is contained in:
Chih-Hung Hsieh
2017-07-18 16:22:38 -07:00
parent 73ab856329
commit 8257426085
9 changed files with 54 additions and 10 deletions

View File

@@ -27,6 +27,7 @@ import com.google.gerrit.server.AnonymousUser;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.patch.PatchList;
import com.google.gerrit.server.patch.PatchListCache;
@@ -47,6 +48,7 @@ import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
public final class StoredValues {
public static final StoredValue<Accounts> ACCOUNTS = create(Accounts.class);
public static final StoredValue<AccountCache> ACCOUNT_CACHE = create(AccountCache.class);
public static final StoredValue<ReviewDb> REVIEW_DB = create(ReviewDb.class);
public static final StoredValue<ChangeData> CHANGE_DATA = create(ChangeData.class);

View File

@@ -27,6 +27,7 @@ import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.reviewdb.server.ReviewDbUtil;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.git.BranchOrderSection;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.MergeUtil;
@@ -63,6 +64,7 @@ public class Mergeable implements RestReadView<RevisionResource> {
private final GitRepositoryManager gitManager;
private final AccountCache accountCache;
private final Accounts accounts;
private final ProjectCache projectCache;
private final MergeUtil.Factory mergeUtilFactory;
private final ChangeData.Factory changeDataFactory;
@@ -74,6 +76,7 @@ public class Mergeable implements RestReadView<RevisionResource> {
Mergeable(
GitRepositoryManager gitManager,
AccountCache accountCache,
Accounts accounts,
ProjectCache projectCache,
MergeUtil.Factory mergeUtilFactory,
ChangeData.Factory changeDataFactory,
@@ -82,6 +85,7 @@ public class Mergeable implements RestReadView<RevisionResource> {
MergeabilityCache cache) {
this.gitManager = gitManager;
this.accountCache = accountCache;
this.accounts = accounts;
this.projectCache = projectCache;
this.mergeUtilFactory = mergeUtilFactory;
this.changeDataFactory = changeDataFactory;
@@ -144,7 +148,7 @@ public class Mergeable implements RestReadView<RevisionResource> {
private SubmitType getSubmitType(ChangeData cd, PatchSet patchSet) throws OrmException {
SubmitTypeRecord rec =
new SubmitRuleEvaluator(accountCache, cd).setPatchSet(patchSet).getSubmitType();
new SubmitRuleEvaluator(accountCache, accounts, cd).setPatchSet(patchSet).getSubmitType();
if (rec.status != SubmitTypeRecord.Status.OK) {
throw new OrmException("Submit type rule failed: " + rec);
}

View File

@@ -30,6 +30,7 @@ import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ApprovalsUtil;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountLoader;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.permissions.LabelPermission;
import com.google.gerrit.server.permissions.PermissionBackend;
import com.google.gerrit.server.permissions.PermissionBackendException;
@@ -50,6 +51,7 @@ public class ReviewerJson {
private final PermissionBackend permissionBackend;
private final ChangeData.Factory changeDataFactory;
private final AccountCache accountCache;
private final Accounts accounts;
private final ApprovalsUtil approvalsUtil;
private final AccountLoader.Factory accountLoaderFactory;
@@ -59,12 +61,14 @@ public class ReviewerJson {
PermissionBackend permissionBackend,
ChangeData.Factory changeDataFactory,
AccountCache accountCache,
Accounts accounts,
ApprovalsUtil approvalsUtil,
AccountLoader.Factory accountLoaderFactory) {
this.db = db;
this.permissionBackend = permissionBackend;
this.changeDataFactory = changeDataFactory;
this.accountCache = accountCache;
this.accounts = accounts;
this.approvalsUtil = approvalsUtil;
this.accountLoaderFactory = accountLoaderFactory;
}
@@ -133,7 +137,7 @@ public class ReviewerJson {
PatchSet ps = cd.currentPatchSet();
if (ps != null) {
for (SubmitRecord rec :
new SubmitRuleEvaluator(accountCache, cd)
new SubmitRuleEvaluator(accountCache, accounts, cd)
.setFastEvalLabels(true)
.setAllowDraft(true)
.evaluate()) {

View File

@@ -26,6 +26,7 @@ import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.rules.RulesCache;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountLoader;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.project.SubmitRuleEvaluator;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gwtorm.server.OrmException;
@@ -42,6 +43,7 @@ public class TestSubmitRule implements RestModifyView<RevisionResource, TestSubm
private final RulesCache rules;
private final AccountCache accountCache;
private final AccountLoader.Factory accountInfoFactory;
private final Accounts accounts;
@Option(name = "--filters", usage = "impact of filters in parent projects")
private Filters filters = Filters.RUN;
@@ -52,12 +54,14 @@ public class TestSubmitRule implements RestModifyView<RevisionResource, TestSubm
ChangeData.Factory changeDataFactory,
RulesCache rules,
AccountCache accountCache,
Accounts accounts,
AccountLoader.Factory infoFactory) {
this.db = db;
this.changeDataFactory = changeDataFactory;
this.rules = rules;
this.accountCache = accountCache;
this.accountInfoFactory = infoFactory;
this.accounts = accounts;
}
@Override
@@ -72,7 +76,7 @@ public class TestSubmitRule implements RestModifyView<RevisionResource, TestSubm
input.filters = MoreObjects.firstNonNull(input.filters, filters);
SubmitRuleEvaluator evaluator =
new SubmitRuleEvaluator(
accountCache, changeDataFactory.create(db.get(), rsrc.getControl()));
accountCache, accounts, changeDataFactory.create(db.get(), rsrc.getControl()));
List<SubmitRecord> records =
evaluator

View File

@@ -26,6 +26,7 @@ import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.rules.RulesCache;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.project.SubmitRuleEvaluator;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gwtorm.server.OrmException;
@@ -36,6 +37,7 @@ import org.kohsuke.args4j.Option;
public class TestSubmitType implements RestModifyView<RevisionResource, TestSubmitRuleInput> {
private final Provider<ReviewDb> db;
private final AccountCache accountCache;
private final Accounts accounts;
private final ChangeData.Factory changeDataFactory;
private final RulesCache rules;
@@ -46,10 +48,12 @@ public class TestSubmitType implements RestModifyView<RevisionResource, TestSubm
TestSubmitType(
Provider<ReviewDb> db,
AccountCache accountCache,
Accounts accounts,
ChangeData.Factory changeDataFactory,
RulesCache rules) {
this.db = db;
this.accountCache = accountCache;
this.accounts = accounts;
this.changeDataFactory = changeDataFactory;
this.rules = rules;
}
@@ -66,7 +70,7 @@ public class TestSubmitType implements RestModifyView<RevisionResource, TestSubm
input.filters = MoreObjects.firstNonNull(input.filters, filters);
SubmitRuleEvaluator evaluator =
new SubmitRuleEvaluator(
accountCache, changeDataFactory.create(db.get(), rsrc.getControl()));
accountCache, accounts, changeDataFactory.create(db.get(), rsrc.getControl()));
SubmitTypeRecord rec =
evaluator

View File

@@ -32,6 +32,7 @@ import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.change.Submit;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo;
@@ -96,6 +97,7 @@ public class MergeSuperSet {
}
private final AccountCache accountCache;
private final Accounts accounts;
private final ChangeData.Factory changeDataFactory;
private final Provider<InternalChangeQuery> queryProvider;
private final Provider<MergeOpRepoManager> repoManagerProvider;
@@ -110,11 +112,13 @@ public class MergeSuperSet {
MergeSuperSet(
@GerritServerConfig Config cfg,
AccountCache accountCache,
Accounts accounts,
ChangeData.Factory changeDataFactory,
Provider<InternalChangeQuery> queryProvider,
Provider<MergeOpRepoManager> repoManagerProvider) {
this.cfg = cfg;
this.accountCache = accountCache;
this.accounts = accounts;
this.changeDataFactory = changeDataFactory;
this.queryProvider = queryProvider;
this.repoManagerProvider = repoManagerProvider;
@@ -165,7 +169,7 @@ public class MergeSuperSet {
SubmitTypeRecord str =
ps == cd.currentPatchSet()
? cd.submitTypeRecord()
: new SubmitRuleEvaluator(accountCache, cd).setPatchSet(ps).getSubmitType();
: new SubmitRuleEvaluator(accountCache, accounts, cd).setPatchSet(ps).getSubmitType();
if (!str.isOk()) {
logErrorAndThrow("Failed to get submit type for " + cd.getId() + ": " + str.errorMessage);
}

View File

@@ -28,6 +28,7 @@ import com.google.gerrit.rules.PrologEnvironment;
import com.google.gerrit.rules.StoredValues;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gwtorm.server.OrmException;
import com.googlecode.prolog_cafe.exceptions.CompileException;
@@ -83,6 +84,7 @@ public class SubmitRuleEvaluator {
}
private final AccountCache accountCache;
private final Accounts accounts;
private final ChangeData cd;
private final ChangeControl control;
@@ -94,8 +96,10 @@ public class SubmitRuleEvaluator {
private Term submitRule;
public SubmitRuleEvaluator(AccountCache accountCache, ChangeData cd) throws OrmException {
public SubmitRuleEvaluator(AccountCache accountCache, Accounts accounts, ChangeData cd)
throws OrmException {
this.accountCache = accountCache;
this.accounts = accounts;
this.cd = cd;
this.control = cd.changeControl();
}
@@ -567,6 +571,7 @@ public class SubmitRuleEvaluator {
}
throw new RuleEvalException(msg, err);
}
env.set(StoredValues.ACCOUNTS, accounts);
env.set(StoredValues.ACCOUNT_CACHE, accountCache);
env.set(StoredValues.REVIEW_DB, cd.db());
env.set(StoredValues.CHANGE_DATA, cd);

View File

@@ -59,6 +59,7 @@ import com.google.gerrit.server.ReviewerStatusUpdate;
import com.google.gerrit.server.StarredChangesUtil;
import com.google.gerrit.server.StarredChangesUtil.StarRef;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.change.MergeabilityCache;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.MergeUtil;
@@ -303,7 +304,7 @@ public class ChangeData {
ChangeData cd =
new ChangeData(
null, null, null, null, null, null, null, null, null, null, null, null, null, null,
null, null, project, id);
null, null, null, project, id);
cd.currentPatchSet = new PatchSet(new PatchSet.Id(id, currentPatchSetId));
return cd;
}
@@ -314,6 +315,7 @@ public class ChangeData {
private final ChangeControl.GenericFactory changeControlFactory;
private final IdentifiedUser.GenericFactory userFactory;
private final AccountCache accountCache;
private final Accounts accounts;
private final ProjectCache projectCache;
private final MergeUtil.Factory mergeUtilFactory;
private final ChangeNotes.Factory notesFactory;
@@ -373,6 +375,7 @@ public class ChangeData {
ChangeControl.GenericFactory changeControlFactory,
IdentifiedUser.GenericFactory userFactory,
AccountCache accountCache,
Accounts accounts,
ProjectCache projectCache,
MergeUtil.Factory mergeUtilFactory,
ChangeNotes.Factory notesFactory,
@@ -392,6 +395,7 @@ public class ChangeData {
this.changeControlFactory = changeControlFactory;
this.userFactory = userFactory;
this.accountCache = accountCache;
this.accounts = accounts;
this.projectCache = projectCache;
this.mergeUtilFactory = mergeUtilFactory;
this.notesFactory = notesFactory;
@@ -413,6 +417,7 @@ public class ChangeData {
ChangeControl.GenericFactory changeControlFactory,
IdentifiedUser.GenericFactory userFactory,
AccountCache accountCache,
Accounts accounts,
ProjectCache projectCache,
MergeUtil.Factory mergeUtilFactory,
ChangeNotes.Factory notesFactory,
@@ -431,6 +436,7 @@ public class ChangeData {
this.changeControlFactory = changeControlFactory;
this.userFactory = userFactory;
this.accountCache = accountCache;
this.accounts = accounts;
this.projectCache = projectCache;
this.mergeUtilFactory = mergeUtilFactory;
this.notesFactory = notesFactory;
@@ -453,6 +459,7 @@ public class ChangeData {
ChangeControl.GenericFactory changeControlFactory,
IdentifiedUser.GenericFactory userFactory,
AccountCache accountCache,
Accounts accounts,
ProjectCache projectCache,
MergeUtil.Factory mergeUtilFactory,
ChangeNotes.Factory notesFactory,
@@ -471,6 +478,7 @@ public class ChangeData {
this.changeControlFactory = changeControlFactory;
this.userFactory = userFactory;
this.accountCache = accountCache;
this.accounts = accounts;
this.projectCache = projectCache;
this.mergeUtilFactory = mergeUtilFactory;
this.notesFactory = notesFactory;
@@ -494,6 +502,7 @@ public class ChangeData {
ChangeControl.GenericFactory changeControlFactory,
IdentifiedUser.GenericFactory userFactory,
AccountCache accountCache,
Accounts accounts,
ProjectCache projectCache,
MergeUtil.Factory mergeUtilFactory,
ChangeNotes.Factory notesFactory,
@@ -512,6 +521,7 @@ public class ChangeData {
this.changeControlFactory = changeControlFactory;
this.userFactory = userFactory;
this.accountCache = accountCache;
this.accounts = accounts;
this.projectCache = projectCache;
this.mergeUtilFactory = mergeUtilFactory;
this.notesFactory = notesFactory;
@@ -536,6 +546,7 @@ public class ChangeData {
ChangeControl.GenericFactory changeControlFactory,
IdentifiedUser.GenericFactory userFactory,
AccountCache accountCache,
Accounts accounts,
ProjectCache projectCache,
MergeUtil.Factory mergeUtilFactory,
ChangeNotes.Factory notesFactory,
@@ -557,6 +568,7 @@ public class ChangeData {
this.changeControlFactory = changeControlFactory;
this.userFactory = userFactory;
this.accountCache = accountCache;
this.accounts = accounts;
this.projectCache = projectCache;
this.mergeUtilFactory = mergeUtilFactory;
this.notesFactory = notesFactory;
@@ -1104,7 +1116,8 @@ public class ChangeData {
if (!lazyLoad) {
return Collections.emptyList();
}
records = new SubmitRuleEvaluator(accountCache, this).setOptions(options).evaluate();
records =
new SubmitRuleEvaluator(accountCache, accounts, this).setOptions(options).evaluate();
submitRecords.put(options, records);
}
return records;
@@ -1121,7 +1134,7 @@ public class ChangeData {
public SubmitTypeRecord submitTypeRecord() throws OrmException {
if (submitTypeRecord == null) {
submitTypeRecord = new SubmitRuleEvaluator(accountCache, this).getSubmitType();
submitTypeRecord = new SubmitRuleEvaluator(accountCache, accounts, this).getSubmitType();
}
return submitTypeRecord;
}

View File

@@ -24,6 +24,7 @@ import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.config.TrackingFooters;
import com.google.gerrit.server.data.ChangeAttribute;
import com.google.gerrit.server.data.PatchSetAttribute;
@@ -70,6 +71,7 @@ public class OutputStreamQuery {
private final ReviewDb db;
private final AccountCache accountCache;
private final Accounts accounts;
private final GitRepositoryManager repoManager;
private final ChangeQueryBuilder queryBuilder;
private final ChangeQueryProcessor queryProcessor;
@@ -95,6 +97,7 @@ public class OutputStreamQuery {
OutputStreamQuery(
ReviewDb db,
AccountCache accountCache,
Accounts accounts,
GitRepositoryManager repoManager,
ChangeQueryBuilder queryBuilder,
ChangeQueryProcessor queryProcessor,
@@ -103,6 +106,7 @@ public class OutputStreamQuery {
CurrentUser user) {
this.db = db;
this.accountCache = accountCache;
this.accounts = accounts;
this.repoManager = repoManager;
this.queryBuilder = queryBuilder;
this.queryProcessor = queryProcessor;
@@ -249,7 +253,7 @@ public class OutputStreamQuery {
if (includeSubmitRecords) {
eventFactory.addSubmitRecords(
c,
new SubmitRuleEvaluator(accountCache, d)
new SubmitRuleEvaluator(accountCache, accounts, d)
.setAllowClosed(true)
.setAllowDraft(true)
.evaluate());