Search in sources :

Example 1 with ActionVisitor

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;
}
Also used : PrivateInternals_UiActionDescription(com.google.gerrit.extensions.webui.PrivateInternals_UiActionDescription) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) ActionInfo(com.google.gerrit.extensions.common.ActionInfo) UiAction(com.google.gerrit.extensions.webui.UiAction) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ActionVisitor

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);
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) RevisionInfo(com.google.gerrit.extensions.common.RevisionInfo) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor)

Example 3 with ActionVisitor

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");
    }
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) RevisionInfo(com.google.gerrit.extensions.common.RevisionInfo) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) ActionInfo(com.google.gerrit.extensions.common.ActionInfo) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 4 with ActionVisitor

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);
}
Also used : ListChangesOption(com.google.gerrit.extensions.client.ListChangesOption) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) RevisionInfo(com.google.gerrit.extensions.common.RevisionInfo) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) ActionInfo(com.google.gerrit.extensions.common.ActionInfo) Change(com.google.gerrit.reviewdb.client.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 5 with ActionVisitor

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;
}
Also used : PrivateInternals_UiActionDescription(com.google.gerrit.extensions.webui.PrivateInternals_UiActionDescription) CurrentUser(com.google.gerrit.server.CurrentUser) ActionVisitor(com.google.gerrit.extensions.api.changes.ActionVisitor) ActionInfo(com.google.gerrit.extensions.common.ActionInfo) UiAction(com.google.gerrit.extensions.webui.UiAction) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ActionVisitor (com.google.gerrit.extensions.api.changes.ActionVisitor)7 ActionInfo (com.google.gerrit.extensions.common.ActionInfo)6 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)5 RevisionInfo (com.google.gerrit.extensions.common.RevisionInfo)5 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)4 Test (org.junit.Test)4 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)3 ListChangesOption (com.google.gerrit.extensions.client.ListChangesOption)2 PrivateInternals_UiActionDescription (com.google.gerrit.extensions.webui.PrivateInternals_UiActionDescription)2 UiAction (com.google.gerrit.extensions.webui.UiAction)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 LinkedHashMap (java.util.LinkedHashMap)2 Change (com.google.gerrit.entities.Change)1 Change (com.google.gerrit.reviewdb.client.Change)1 CurrentUser (com.google.gerrit.server.CurrentUser)1 TreeSet (java.util.TreeSet)1