Add RevisionApi#testSubmitRule

Change-Id: I8fee313ef71462d3acaa4ae84934895932699566
This commit is contained in:
Patrick Hiesel 2018-10-15 11:07:10 +02:00
parent d787ad2ee4
commit 24983a0448
4 changed files with 104 additions and 57 deletions

View File

@ -23,6 +23,7 @@ import com.google.gerrit.extensions.common.EditInfo;
import com.google.gerrit.extensions.common.FileInfo;
import com.google.gerrit.extensions.common.MergeableInfo;
import com.google.gerrit.extensions.common.RobotCommentInfo;
import com.google.gerrit.extensions.common.TestSubmitRuleInfo;
import com.google.gerrit.extensions.common.TestSubmitRuleInput;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.NotImplementedException;
@ -128,6 +129,8 @@ public interface RevisionApi {
SubmitType testSubmitType(TestSubmitRuleInput in) throws RestApiException;
List<TestSubmitRuleInfo> testSubmitRule(TestSubmitRuleInput in) throws RestApiException;
MergeListRequest getMergeList() throws RestApiException;
abstract class MergeListRequest {
@ -357,6 +360,11 @@ public interface RevisionApi {
throw new NotImplementedException();
}
@Override
public List<TestSubmitRuleInfo> testSubmitRule(TestSubmitRuleInput in) throws RestApiException {
throw new NotImplementedException();
}
@Override
public MergeListRequest getMergeList() throws RestApiException {
throw new NotImplementedException();

View File

@ -0,0 +1,35 @@
// Copyright (C) 2018 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.extensions.common;
import java.util.Map;
public class TestSubmitRuleInfo {
/** @see com.google.gerrit.common.data.SubmitRecord.Status */
public String status;
public String errorMessage;
public Map<String, AccountInfo> ok;
public Map<String, AccountInfo> reject;
public Map<String, None> need;
public Map<String, AccountInfo> may;
public Map<String, None> impossible;
public static class None {
private None() {}
public static None INSTANCE = new None();
}
}

View File

@ -43,6 +43,7 @@ import com.google.gerrit.extensions.common.FileInfo;
import com.google.gerrit.extensions.common.Input;
import com.google.gerrit.extensions.common.MergeableInfo;
import com.google.gerrit.extensions.common.RobotCommentInfo;
import com.google.gerrit.extensions.common.TestSubmitRuleInfo;
import com.google.gerrit.extensions.common.TestSubmitRuleInput;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.IdString;
@ -76,6 +77,7 @@ import com.google.gerrit.server.restapi.change.Reviewed;
import com.google.gerrit.server.restapi.change.RevisionReviewers;
import com.google.gerrit.server.restapi.change.RobotComments;
import com.google.gerrit.server.restapi.change.Submit;
import com.google.gerrit.server.restapi.change.TestSubmitRule;
import com.google.gerrit.server.restapi.change.TestSubmitType;
import com.google.inject.Inject;
import com.google.inject.Provider;
@ -125,6 +127,7 @@ class RevisionApiImpl implements RevisionApi {
private final GetRevisionActions revisionActions;
private final TestSubmitType testSubmitType;
private final TestSubmitType.Get getSubmitType;
private final Provider<TestSubmitRule> testSubmitRule;
private final Provider<GetMergeList> getMergeList;
private final PutDescription putDescription;
private final GetDescription getDescription;
@ -164,6 +167,7 @@ class RevisionApiImpl implements RevisionApi {
GetRevisionActions revisionActions,
TestSubmitType testSubmitType,
TestSubmitType.Get getSubmitType,
Provider<TestSubmitRule> testSubmitRule,
Provider<GetMergeList> getMergeList,
PutDescription putDescription,
GetDescription getDescription,
@ -201,6 +205,7 @@ class RevisionApiImpl implements RevisionApi {
this.revisionActions = revisionActions;
this.testSubmitType = testSubmitType;
this.getSubmitType = getSubmitType;
this.testSubmitRule = testSubmitRule;
this.getMergeList = getMergeList;
this.putDescription = putDescription;
this.getDescription = getDescription;
@ -558,6 +563,15 @@ class RevisionApiImpl implements RevisionApi {
}
}
@Override
public List<TestSubmitRuleInfo> testSubmitRule(TestSubmitRuleInput in) throws RestApiException {
try {
return testSubmitRule.get().apply(revision, in);
} catch (Exception e) {
throw asRestApiException("Cannot test submit rule", e);
}
}
@Override
public MergeListRequest getMergeList() throws RestApiException {
return new MergeListRequest() {

View File

@ -18,6 +18,7 @@ import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
import com.google.gerrit.common.data.SubmitRecord;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.common.TestSubmitRuleInfo;
import com.google.gerrit.extensions.common.TestSubmitRuleInput;
import com.google.gerrit.extensions.common.TestSubmitRuleInput.Filters;
import com.google.gerrit.extensions.restapi.AuthException;
@ -35,7 +36,6 @@ import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.kohsuke.args4j.Option;
public class TestSubmitRule implements RestModifyView<RevisionResource, TestSubmitRuleInput> {
@ -63,7 +63,7 @@ public class TestSubmitRule implements RestModifyView<RevisionResource, TestSubm
}
@Override
public List<Record> apply(RevisionResource rsrc, TestSubmitRuleInput input)
public List<TestSubmitRuleInfo> apply(RevisionResource rsrc, TestSubmitRuleInput input)
throws AuthException, OrmException, PermissionBackendException {
if (input == null) {
input = new TestSubmitRuleInput();
@ -83,71 +83,61 @@ public class TestSubmitRule implements RestModifyView<RevisionResource, TestSubm
ChangeData cd = changeDataFactory.create(db.get(), rsrc.getNotes());
List<SubmitRecord> records = submitRuleEvaluatorFactory.create(opts).evaluate(cd);
List<Record> out = Lists.newArrayListWithCapacity(records.size());
List<TestSubmitRuleInfo> out = Lists.newArrayListWithCapacity(records.size());
AccountLoader accounts = accountInfoFactory.create(true);
for (SubmitRecord r : records) {
out.add(new Record(r, accounts));
out.add(newSubmitRuleInfo(r, accounts));
}
accounts.fill();
return out;
}
static class Record {
SubmitRecord.Status status;
String errorMessage;
Map<String, AccountInfo> ok;
Map<String, AccountInfo> reject;
Map<String, None> need;
Map<String, AccountInfo> may;
Map<String, None> impossible;
private static TestSubmitRuleInfo newSubmitRuleInfo(SubmitRecord r, AccountLoader accounts) {
TestSubmitRuleInfo info = new TestSubmitRuleInfo();
info.status = r.status.name();
info.errorMessage = r.errorMessage;
Record(SubmitRecord r, AccountLoader accounts) {
this.status = r.status;
this.errorMessage = r.errorMessage;
if (r.labels != null) {
for (SubmitRecord.Label n : r.labels) {
AccountInfo who = n.appliedBy != null ? accounts.get(n.appliedBy) : new AccountInfo(null);
label(n, who);
}
}
}
private void label(SubmitRecord.Label n, AccountInfo who) {
switch (n.status) {
case OK:
if (ok == null) {
ok = new LinkedHashMap<>();
}
ok.put(n.label, who);
break;
case REJECT:
if (reject == null) {
reject = new LinkedHashMap<>();
}
reject.put(n.label, who);
break;
case NEED:
if (need == null) {
need = new LinkedHashMap<>();
}
need.put(n.label, new None());
break;
case MAY:
if (may == null) {
may = new LinkedHashMap<>();
}
may.put(n.label, who);
break;
case IMPOSSIBLE:
if (impossible == null) {
impossible = new LinkedHashMap<>();
}
impossible.put(n.label, new None());
break;
if (r.labels != null) {
for (SubmitRecord.Label n : r.labels) {
AccountInfo who = n.appliedBy != null ? accounts.get(n.appliedBy) : new AccountInfo(null);
label(info, n, who);
}
}
return info;
}
static class None {}
private static void label(TestSubmitRuleInfo info, SubmitRecord.Label n, AccountInfo who) {
switch (n.status) {
case OK:
if (info.ok == null) {
info.ok = new LinkedHashMap<>();
}
info.ok.put(n.label, who);
break;
case REJECT:
if (info.reject == null) {
info.reject = new LinkedHashMap<>();
}
info.reject.put(n.label, who);
break;
case NEED:
if (info.need == null) {
info.need = new LinkedHashMap<>();
}
info.need.put(n.label, TestSubmitRuleInfo.None.INSTANCE);
break;
case MAY:
if (info.may == null) {
info.may = new LinkedHashMap<>();
}
info.may.put(n.label, who);
break;
case IMPOSSIBLE:
if (info.impossible == null) {
info.impossible = new LinkedHashMap<>();
}
info.impossible.put(n.label, TestSubmitRuleInfo.None.INSTANCE);
break;
}
}
}