Support controlling the submit type for changes from Prolog

Similarly like the "submit_rule" there is now a "submit_type" predicate
which returns the allowed submit type for a change. When the submit_type
predicate is not provided in the rules.pl then the project default
submit type is used for all changes for that project.

Filtering the results of the submit_type is also supported in the same
way like filtering the results of the submit_rule. Using a
submit_type_filter predicate one can enforce a particular submit type
from a parent project. For example, a release engineer may want to
enforce the FAST_FORWARD_ONLY submit type for all changes pushed to
stable release branches while, at the same time, use the project wide
default submit type for changes pushed to the development branch.

Change-Id: Iec08f412723856b40324a6e0ec00ac523be9a3b6
Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com>
This commit is contained in:
Sasa Zivkov
2012-08-13 10:46:08 +02:00
committed by Gerrit Code Review
parent 162c801f4b
commit 680a5f80d9
13 changed files with 289 additions and 34 deletions

View File

@@ -42,6 +42,7 @@ public class ChangeDetail {
protected List<ApprovalDetail> approvals;
protected List<SubmitRecord> submitRecords;
protected Project.SubmitType submitType;
protected SubmitTypeRecord submitTypeRecord;
protected boolean canSubmit;
protected List<ChangeMessage> messages;
protected PatchSet.Id currentPatchSetId;
@@ -189,12 +190,12 @@ public class ChangeDetail {
return submitRecords;
}
public void setSubmitType(Project.SubmitType submitType) {
this.submitType = submitType;
public void setSubmitTypeRecord(SubmitTypeRecord submitTypeRecord) {
this.submitTypeRecord = submitTypeRecord;
}
public Project.SubmitType getSubmitType() {
return submitType;
public SubmitTypeRecord getSubmitTypeRecord() {
return submitTypeRecord;
}
public boolean isCurrentPatchSet(final PatchSetDetail detail) {

View File

@@ -34,7 +34,7 @@ public class PatchSetPublishDetail {
protected List<PermissionRange> labels;
protected List<ApprovalDetail> approvals;
protected List<SubmitRecord> submitRecords;
protected Project.SubmitType submitType;
protected SubmitTypeRecord submitTypeRecord;
protected List<PatchSetApproval> given;
protected boolean canSubmit;
@@ -63,12 +63,12 @@ public class PatchSetPublishDetail {
return submitRecords;
}
public void setSubmitType(Project.SubmitType submitType) {
this.submitType = submitType;
public void setSubmitTypeRecord(SubmitTypeRecord submitTypeRecord) {
this.submitTypeRecord = submitTypeRecord;
}
public Project.SubmitType getSubmitType() {
return submitType;
public SubmitTypeRecord getSubmitTypeRecord() {
return submitTypeRecord;
}
public List<PatchSetApproval> getGiven() {

View File

@@ -0,0 +1,58 @@
// Copyright (C) 2012 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 com.google.gerrit.common.data;
import com.google.gerrit.reviewdb.client.Project;
/**
* Describes the submit type for a change.
*/
public class SubmitTypeRecord {
public static enum Status {
/** The type was computed successfully */
OK,
/** An internal server error occurred preventing computation.
* <p>
* Additional detail may be available in {@link SubmitTypeRecord#errorMessage}
*/
RULE_ERROR
}
public static SubmitTypeRecord OK(Project.SubmitType type) {
SubmitTypeRecord r = new SubmitTypeRecord();
r.status = Status.OK;
r.type = type;
return r;
}
public Status status;
public Project.SubmitType type;
public String errorMessage;
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(status);
if (status == Status.RULE_ERROR && errorMessage != null) {
sb.append('(').append(errorMessage).append(")");
}
if (type != null) {
sb.append('[');
sb.append(type.name());
sb.append(']');
}
return sb.toString();
}
}

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.client.changes;
import com.google.gerrit.common.data.AccountInfoCache;
import com.google.gerrit.common.data.SubmitTypeRecord;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSetInfo;
import com.google.gerrit.reviewdb.client.Project;
@@ -37,8 +38,8 @@ public class ChangeDescriptionBlock extends Composite {
}
public void display(Change chg, Boolean starred, PatchSetInfo info,
final AccountInfoCache acc, Project.SubmitType submitType) {
infoBlock.display(chg, acc, submitType);
final AccountInfoCache acc, SubmitTypeRecord submitTypeRecord) {
infoBlock.display(chg, acc, submitTypeRecord);
messageBlock.display(chg.getId(), starred, info.getMessage());
}
}

View File

@@ -21,9 +21,9 @@ import com.google.gerrit.client.ui.AccountLink;
import com.google.gerrit.client.ui.BranchLink;
import com.google.gerrit.client.ui.ProjectLink;
import com.google.gerrit.common.data.AccountInfoCache;
import com.google.gerrit.common.data.SubmitTypeRecord;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
@@ -81,7 +81,7 @@ public class ChangeInfoBlock extends Composite {
}
public void display(final Change chg, final AccountInfoCache acc,
Project.SubmitType submitType) {
SubmitTypeRecord submitTypeRecord) {
final Branch.NameKey dst = chg.getDest();
CopyableLabel changeIdLabel =
@@ -98,8 +98,14 @@ public class ChangeInfoBlock extends Composite {
table.setText(R_UPLOADED, 1, mediumFormat(chg.getCreatedOn()));
table.setText(R_UPDATED, 1, mediumFormat(chg.getLastUpdatedOn()));
table.setText(R_STATUS, 1, Util.toLongString(chg.getStatus()));
table.setText(R_SUBMIT_TYPE, 1,
com.google.gerrit.client.admin.Util.toLongString(submitType));
String submitType;
if (submitTypeRecord.status == SubmitTypeRecord.Status.OK) {
submitType = com.google.gerrit.client.admin.Util
.toLongString(submitTypeRecord.type);
} else {
submitType = submitTypeRecord.status.name();
}
table.setText(R_SUBMIT_TYPE, 1, submitType);
final Change.Status status = chg.getStatus();
if (Gerrit.getConfig().testChangeMerge()) {
if (status.equals(Change.Status.NEW) || status.equals(Change.Status.DRAFT)) {

View File

@@ -282,7 +282,7 @@ public class ChangeScreen extends Screen
descriptionBlock.display(detail.getChange(),
detail.isStarred(),
detail.getCurrentPatchSetDetail().getInfo(),
detail.getAccounts(), detail.getSubmitType());
detail.getAccounts(), detail.getSubmitTypeRecord());
dependsOn.display(detail.getDependsOn());
neededBy.display(detail.getNeededBy());
approvals.display(detail);

View File

@@ -275,7 +275,7 @@ public class PublishCommentScreen extends AccountScreen implements
setPageTitle(Util.M.publishComments(r.getChange().getKey().abbreviate(),
patchSetId.get()));
descBlock.display(r.getChange(), null, r.getPatchSetInfo(), r.getAccounts(),
r.getSubmitType());
r.getSubmitTypeRecord());
if (r.getChange().getStatus().isOpen()) {
initApprovals(r, approvalPanel);

View File

@@ -168,7 +168,7 @@ public class ChangeDetailFactory extends Handler<ChangeDetail> {
}
detail.setSubmitRecords(submitRecords);
detail.setSubmitType(control.getSubmitType());
detail.setSubmitTypeRecord(control.getSubmitTypeRecord(db, patch));
patchsetsById = new HashMap<PatchSet.Id, PatchSet>();
loadPatchSets();

View File

@@ -34,7 +34,6 @@ import com.google.gerrit.server.patch.PatchSetInfoFactory;
import com.google.gerrit.server.patch.PatchSetInfoNotAvailableException;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.workflow.CategoryFunction;
import com.google.gerrit.server.workflow.FunctionState;
import com.google.gwtorm.server.OrmException;
@@ -61,7 +60,6 @@ final class PatchSetPublishDetailFactory extends Handler<PatchSetPublishDetail>
private final ApprovalTypes approvalTypes;
private final AccountInfoCacheFactory aic;
private final IdentifiedUser user;
private final ProjectCache projectCache;
private final PatchSet.Id patchSetId;
@@ -78,9 +76,7 @@ final class PatchSetPublishDetailFactory extends Handler<PatchSetPublishDetail>
final ChangeControl.GenericFactory changeControlGenericFactory,
final IdentifiedUser.GenericFactory identifiedUserFactory,
final ApprovalTypes approvalTypes,
final IdentifiedUser user,
final ProjectCache projectCache,
@Assisted final PatchSet.Id patchSetId) {
final IdentifiedUser user, @Assisted final PatchSet.Id patchSetId) {
this.infoFactory = infoFactory;
this.db = db;
this.functionState = functionState;
@@ -90,7 +86,6 @@ final class PatchSetPublishDetailFactory extends Handler<PatchSetPublishDetail>
this.approvalTypes = approvalTypes;
this.aic = accountInfoCacheFactory.create();
this.user = user;
this.projectCache = projectCache;
this.patchSetId = patchSetId;
}
@@ -191,8 +186,7 @@ final class PatchSetPublishDetailFactory extends Handler<PatchSetPublishDetail>
detail.setSubmitRecords(submitRecords);
}
detail.setSubmitType(projectCache.get(change.getProject())
.getProject().getSubmitType());
detail.setSubmitTypeRecord(control.getSubmitTypeRecord(db, patchSet));
detail.setLabels(allowed);
detail.setGiven(given);

View File

@@ -23,6 +23,7 @@ import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.common.data.Capable;
import com.google.gerrit.common.data.SubmitTypeRecord;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.ApprovalCategory;
import com.google.gerrit.reviewdb.client.Branch;
@@ -572,7 +573,20 @@ public class MergeOp {
}
private SubmitType getSubmitType(final Change change, final PatchSet ps) {
return destProject.getSubmitType();
try {
final SubmitTypeRecord r =
changeControlFactory.controlFor(change,
identifiedUserFactory.create(change.getOwner()))
.getSubmitTypeRecord(db, ps);
if (r.status != SubmitTypeRecord.Status.OK) {
log.error("Failed to get submit type for " + change.getKey());
return null;
}
return r.type;
} catch (NoSuchChangeException e) {
log.error("Failed to get submit type for " + change.getKey(), e);
return null;
}
}
private static <K, T> List<T> getList(final K key, final Map<K, List<T>> map) {

View File

@@ -16,12 +16,12 @@ package com.google.gerrit.server.project;
import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.common.data.SubmitRecord;
import com.google.gerrit.common.data.SubmitTypeRecord;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.Project.SubmitType;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
@@ -439,9 +439,63 @@ public class ChangeControl {
return out;
}
public SubmitType getSubmitType() {
ProjectState projectState = getProjectControl().getProjectState();
return projectState.getProject().getSubmitType();
public SubmitTypeRecord getSubmitTypeRecord(ReviewDb db, PatchSet patchSet) {
return getSubmitTypeRecord(db, patchSet, null);
}
@SuppressWarnings("unchecked")
public SubmitTypeRecord getSubmitTypeRecord(ReviewDb db, PatchSet patchSet,
@Nullable ChangeData cd) {
if (!patchSet.getId().equals(change.currentPatchSetId())) {
return typeRuleError("Patch set " + patchSet.getPatchSetId()
+ " is not current");
}
try {
if (change.getStatus() == Change.Status.DRAFT && !isDraftVisible(db, cd)) {
return typeRuleError("Patch set " + patchSet.getPatchSetId()
+ " not found");
}
if (patchSet.isDraft() && !isDraftVisible(db, cd)) {
return typeRuleError("Patch set " + patchSet.getPatchSetId()
+ " not found");
}
} catch (OrmException err) {
return logTypeRuleError("Cannot read patch set " + patchSet.getId(),
err);
}
List<String> results;
SubmitRuleEvaluator evaluator;
try {
evaluator = new SubmitRuleEvaluator(db, patchSet,
getProjectControl(), this, change, cd,
false,
"locate_submit_type", "get_submit_type",
"locate_submit_type_filter", "filter_submit_type_results");
results = evaluator.evaluate().toJava();
} catch (RuleEvalException e) {
return logTypeRuleError(e.getMessage(), e);
}
if (results.isEmpty()) {
// Should never occur for a well written rule
log.error("Submit rule '" + evaluator.getSubmitRule() + "' for change "
+ change.getId() + " of " + getProject().getName()
+ " has no solution.");
return typeRuleError("Project submit rule has no solution");
}
// Take only the first result and convert it to SubmitTypeRecord
// This logic will need to change once we support multiple submit types
// in the UI
String typeName = results.get(0);
try {
return SubmitTypeRecord.OK(
Project.SubmitType.valueOf(typeName.toUpperCase()));
} catch (IllegalArgumentException e) {
return logInvalidType(evaluator.getSubmitRule(), typeName);
}
}
private List<SubmitRecord> logInvalidResult(Term rule, Term record) {
@@ -466,6 +520,29 @@ public class ChangeControl {
return Collections.singletonList(rec);
}
private SubmitTypeRecord logInvalidType(Term rule, String record) {
return logTypeRuleError("Submit type rule " + rule + " for change "
+ change.getId() + " of " + getProject().getName()
+ " output invalid result: " + record);
}
private SubmitTypeRecord logTypeRuleError(String err, Exception e) {
log.error(err, e);
return typeRuleError("Error evaluating project type rules, check server log");
}
private SubmitTypeRecord logTypeRuleError(String err) {
log.error(err);
return typeRuleError("Error evaluating project type rules, check server log");
}
private SubmitTypeRecord typeRuleError(String err) {
SubmitTypeRecord rec = new SubmitTypeRecord();
rec.status = SubmitTypeRecord.Status.RULE_ERROR;
rec.errorMessage = err;
return rec;
}
private void appliedBy(SubmitRecord.Label label, Term status) {
if (status.isStructure() && status.arity() == 1) {
Term who = status.arg(0);

View File

@@ -0,0 +1,58 @@
// Copyright (C) 2012 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.client.Project.SubmitType;
import com.google.gerrit.rules.StoredValues;
import com.google.gerrit.server.project.ChangeControl;
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_project_default_submit_type_1 extends Predicate.P1 {
private static final SymbolTerm[] term;
static {
SubmitType[] val = SubmitType.values();
term = new SymbolTerm[val.length];
for (int i = 0; i < val.length; i++) {
term[i] = SymbolTerm.create(val[i].name());
}
}
public PRED_project_default_submit_type_1(Term a1, Operation n) {
arg1 = a1;
cont = n;
}
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
ChangeControl control = StoredValues.CHANGE_CONTROL.get(engine);
SubmitType submitType = control.getProject().getSubmitType();
if (!a1.unify(term[submitType.ordinal()], engine.trail)) {
return engine.fail();
}
return cont;
}
}

View File

@@ -140,12 +140,12 @@ not_same(_, _).
:- public can_submit/2.
%%
can_submit(SubmitRule, S) :-
call_submit_rule(SubmitRule, Tmp),
call_rule(SubmitRule, Tmp),
Tmp =.. [submit | Ls],
( is_all_ok(Ls) -> S = ok(Tmp), ! ; S = not_ready(Tmp) ).
call_submit_rule(P:X, Arg) :- !, F =.. [X, Arg], P:F.
call_submit_rule(X, Arg) :- !, F =.. [X, Arg], F.
call_rule(P:X, Arg) :- !, F =.. [X, Arg], P:F.
call_rule(X, Arg) :- !, F =.. [X, Arg], F.
is_all_ok([]).
is_all_ok([label(_, ok(__)) | Ls]) :- is_all_ok(Ls).
@@ -182,6 +182,31 @@ locate_submit_rule(RuleName) :-
locate_helper(submit_rule, default_submit, 1, RuleName).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% get_submit_type/2:
%%
%% Executes the SubmitTypeRule and return the first solution
%%
:- public get_submit_type/2.
%%
get_submit_type(SubmitTypeRule, A) :-
call_rule(SubmitTypeRule, A), !.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% locate_submit_type/1:
%%
%% Finds a submit_type_rule depending on what rules are available.
%% If none are available, use project_default_submit_type/1.
%%
:- public locate_submit_type/1.
%%
locate_submit_type(RuleName) :-
locate_helper(submit_type, project_default_submit_type, 1, RuleName).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% default_submit/1:
@@ -311,6 +336,17 @@ filter_submit_results(Filter, [], Out, Out).
call_submit_filter(P:X, R, S) :- !, F =.. [X, R, S], P:F.
call_submit_filter(X, R, S) :- F =.. [X, R, S], F.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% filter_submit_type_results/3:
%%
%% Executes the submit_type_filter against the result,
%% returns the filtered result.
%%
:- public filter_submit_type_results/3.
%%
filter_submit_type_results(Filter, In, Out) :- call_submit_filter(Filter, In, Out).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
@@ -331,6 +367,16 @@ locate_submit_filter(FilterName) :-
%%
noop_filter(In, In).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% locate_submit_type_filter/1:
%%
%% Finds a submit_type_filter if available.
%%
:- public locate_submit_type_filter/1.
%%
locate_submit_type_filter(FilterName) :-
locate_helper(submit_type_filter, noop_filter, 2, FilterName).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%