use of com.google.gerrit.extensions.api.changes.ActionVisitor in project gerrit by GerritCodeReview.
the class ActionJson method toActionMap.
private Map<String, ActionInfo> toActionMap(ChangeControl ctl, List<ActionVisitor> visitors, ChangeInfo changeInfo) {
Map<String, ActionInfo> out = new LinkedHashMap<>();
if (!ctl.getUser().isIdentifiedUser()) {
return out;
}
FluentIterable<UiAction.Description> descs = uiActions.from(changeViews, changeResourceFactory.create(ctl));
// resulting action map.
if (ctl.getChange().getStatus().isOpen()) {
UiAction.Description descr = new UiAction.Description();
PrivateInternals_UiActionDescription.setId(descr, "followup");
PrivateInternals_UiActionDescription.setMethod(descr, "POST");
descr.setTitle("Create follow-up change");
descr.setLabel("Follow-Up");
descs = descs.append(descr);
}
ACTION: for (UiAction.Description d : descs) {
ActionInfo actionInfo = new ActionInfo(d);
for (ActionVisitor visitor : visitors) {
if (!visitor.visit(d.getId(), actionInfo, changeInfo)) {
continue ACTION;
}
}
out.put(d.getId(), actionInfo);
}
return out;
}
use of com.google.gerrit.extensions.api.changes.ActionVisitor in project gerrit by GerritCodeReview.
the class ActionJson method format.
public Map<String, ActionInfo> format(RevisionResource rsrc) {
ChangeInfo changeInfo = null;
RevisionInfo revisionInfo = null;
List<ActionVisitor> visitors = visitors();
if (!visitors.isEmpty()) {
changeInfo = changeJson().format(rsrc);
revisionInfo = requireNonNull(Iterables.getOnlyElement(changeInfo.revisions.values()));
changeInfo.revisions = null;
}
return toActionMap(rsrc, visitors, changeInfo, revisionInfo);
}
use of com.google.gerrit.extensions.api.changes.ActionVisitor in project gerrit by GerritCodeReview.
the class ActionsIT method oldRevisionActionVisitor.
@Test
public void oldRevisionActionVisitor() throws Exception {
String id = createChange().getChangeId();
amendChange(id);
ChangeInfo origChange = gApi.changes().id(id).get(CHANGE_ACTIONS);
class Visitor implements ActionVisitor {
@Override
public boolean visit(String name, ActionInfo actionInfo, ChangeInfo changeInfo) {
// Do nothing; implicitly called for CURRENT_ACTIONS.
return true;
}
@Override
public boolean visit(String name, ActionInfo actionInfo, ChangeInfo changeInfo, RevisionInfo revisionInfo) {
assertThat(changeInfo).isNotNull();
assertThat(changeInfo._number).isEqualTo(origChange._number);
assertThat(revisionInfo).isNotNull();
assertThat(revisionInfo._number).isEqualTo(1);
if (name.equals("description")) {
actionInfo.label = "Describify";
}
return true;
}
}
Map<String, ActionInfo> origActions = gApi.changes().id(id).revision(1).actions();
assertThat(origActions.keySet()).containsExactly("description");
assertThat(origActions.get("description").label).isEqualTo("Edit Description");
try (Registration registration = extensionRegistry.newRegistration().add(new Visitor())) {
// Unlike for the current revision, actions for old revisions are only available via the
// revision API.
Map<String, ActionInfo> newActions = gApi.changes().id(id).revision(1).actions();
assertThat(newActions).isNotNull();
assertThat(newActions.keySet()).isEqualTo(origActions.keySet());
ActionInfo description = newActions.get("description");
assertThat(description).isNotNull();
assertThat(description.label).isEqualTo("Describify");
}
}
use of com.google.gerrit.extensions.api.changes.ActionVisitor in project gerrit by GerritCodeReview.
the class ActionsIT method revisionActionVisitor.
@Test
public void revisionActionVisitor() throws Exception {
String id = createChange().getChangeId();
ChangeInfo origChange = gApi.changes().id(id).get(EnumSet.of(ListChangesOption.CHANGE_ACTIONS));
class Visitor implements ActionVisitor {
@Override
public boolean visit(String name, ActionInfo actionInfo, ChangeInfo changeInfo) {
// Do nothing; implicitly called for CURRENT_ACTIONS.
return true;
}
@Override
public boolean visit(String name, ActionInfo actionInfo, ChangeInfo changeInfo, RevisionInfo revisionInfo) {
assertThat(changeInfo).isNotNull();
assertThat(changeInfo._number).isEqualTo(origChange._number);
assertThat(revisionInfo).isNotNull();
assertThat(revisionInfo._number).isEqualTo(1);
if (name.equals("cherrypick")) {
return false;
}
if (name.equals("rebase")) {
actionInfo.label = "All Your Base";
}
return true;
}
}
Map<String, ActionInfo> origActions = gApi.changes().id(id).current().actions();
assertThat(origActions.keySet()).containsAllOf("cherrypick", "rebase");
assertThat(origActions.get("rebase").label).isEqualTo("Rebase");
Visitor v = new Visitor();
visitorHandle = actionVisitors.add(v);
// Test different codepaths within ActionJson...
// ...via revision API.
visitedRevisionActionsAssertions(origActions, gApi.changes().id(id).current().actions());
// ...via change API with option.
EnumSet<ListChangesOption> opts = EnumSet.of(CURRENT_ACTIONS, CURRENT_REVISION);
ChangeInfo changeInfo = gApi.changes().id(id).get(opts);
RevisionInfo revisionInfo = Iterables.getOnlyElement(changeInfo.revisions.values());
visitedRevisionActionsAssertions(origActions, revisionInfo.actions);
// ...via ChangeJson directly.
ChangeData cd = changeDataFactory.create(db, project, new Change.Id(origChange._number));
revisionInfo = changeJsonFactory.create(opts).getRevisionInfo(cd.changeControl(), Iterables.getOnlyElement(cd.patchSets()));
visitedRevisionActionsAssertions(origActions, revisionInfo.actions);
}
use of com.google.gerrit.extensions.api.changes.ActionVisitor in project gerrit by GerritCodeReview.
the class ActionJson method toActionMap.
private Map<String, ActionInfo> toActionMap(ChangeData changeData, List<ActionVisitor> visitors, ChangeInfo changeInfo) {
CurrentUser user = userProvider.get();
Map<String, ActionInfo> out = new LinkedHashMap<>();
if (!user.isIdentifiedUser()) {
return out;
}
Iterable<UiAction.Description> descs = uiActions.from(changeViews, changeResourceFactory.create(changeData, user));
// resulting action map.
if (!changeData.change().isAbandoned()) {
UiAction.Description descr = new UiAction.Description();
PrivateInternals_UiActionDescription.setId(descr, "followup");
PrivateInternals_UiActionDescription.setMethod(descr, "POST");
descr.setTitle("Create follow-up change");
descr.setLabel("Follow-Up");
descs = Iterables.concat(descs, Collections.singleton(descr));
}
ACTION: for (UiAction.Description d : descs) {
ActionInfo actionInfo = new ActionInfo(d);
for (ActionVisitor visitor : visitors) {
if (!visitor.visit(d.getId(), actionInfo, changeInfo)) {
continue ACTION;
}
}
out.put(d.getId(), actionInfo);
}
return out;
}
Aggregations