Switch to using DRAFT status

Change-Id: I45f1309b136e791660d197a700778bfcb557f703
diff --git a/pom.xml b/pom.xml
index d5a58af..b051526 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,7 +25,7 @@
   <version>1.0</version>
   <properties>
     <Gerrit-ApiType>plugin</Gerrit-ApiType>
-    <Gerrit-ApiVersion>2.8-SNAPSHOT</Gerrit-ApiVersion>
+    <Gerrit-ApiVersion>2.9-SNAPSHOT</Gerrit-ApiVersion>
   </properties>
 
   <build>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/wip/BaseAction.java b/src/main/java/com/googlesource/gerrit/plugins/wip/BaseAction.java
index 47c5f38..556954e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/wip/BaseAction.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/wip/BaseAction.java
@@ -14,19 +14,20 @@
 
 package com.googlesource.gerrit.plugins.wip;
 
+import java.io.IOException;
 import java.util.Collections;
 
 import com.google.common.base.Strings;
+import com.google.common.util.concurrent.CheckedFuture;
 import com.google.gerrit.extensions.restapi.ResourceConflictException;
 import com.google.gerrit.reviewdb.client.Change;
-import com.google.gerrit.reviewdb.client.ChangeMessage;
-import com.google.gerrit.reviewdb.client.Change.Id;
 import com.google.gerrit.reviewdb.client.Change.Status;
+import com.google.gerrit.reviewdb.client.ChangeMessage;
 import com.google.gerrit.reviewdb.server.ReviewDb;
-import com.google.gerrit.server.ApprovalsUtil;
 import com.google.gerrit.server.ChangeUtil;
 import com.google.gerrit.server.CurrentUser;
 import com.google.gerrit.server.IdentifiedUser;
+import com.google.gerrit.server.index.ChangeIndexer;
 import com.google.gwtorm.server.AtomicUpdate;
 import com.google.gwtorm.server.OrmException;
 import com.google.inject.Inject;
@@ -39,19 +40,22 @@
 
   private final Provider<ReviewDb> dbProvider;
   private final Provider<CurrentUser> userProvider;
+  private final ChangeIndexer indexer;
 
   @Inject
   BaseAction(Provider<ReviewDb> dbProvider,
-      Provider<CurrentUser> userProvider) {
+      Provider<CurrentUser> userProvider,
+      ChangeIndexer indexer) {
     this.dbProvider = dbProvider;
     this.userProvider = userProvider;
+    this.indexer = indexer;
   }
 
   protected void changeStatus(Change change, Input input, final Status from,
-      final Status to) throws OrmException,
-      ResourceConflictException {
+      final Status to) throws OrmException, ResourceConflictException,
+      IOException {
     ReviewDb db = dbProvider.get();
-    Id changeId = change.getId();
+    Change.Id changeId = change.getId();
     db.changes().beginTransaction(changeId);
     try {
       change = db.changes().atomicUpdate(
@@ -75,7 +79,11 @@
 
       db.changeMessages().insert(Collections.singleton(
           newMessage(input, change)));
-      new ApprovalsUtil(db).syncChangeStatus(change);
+
+      CheckedFuture<?, IOException> indexFuture =
+          indexer.indexAsync(change.getId());
+      indexFuture.checkedGet();
+
       db.commit();
     } finally {
       db.rollback();
@@ -84,16 +92,14 @@
 
   private ChangeMessage newMessage(Input input,
       Change change) throws OrmException {
-    StringBuilder msg = new StringBuilder(
-        "Change "
-        + change.getId().get()
-        + ": "
-        + ((change.getStatus() == Status.WORKINPROGRESS)
-            ? "Work In Progress"
-            : "Ready For Review"));
-    if (!Strings.nullToEmpty(input.message).trim().isEmpty()) {
-      msg.append("\n\n");
-      msg.append(input.message.trim());
+    StringBuilder buf = new StringBuilder(change.getStatus() == Status.DRAFT
+        ? "Work In Progress"
+        : "Ready For Review");
+
+    String msg = Strings.nullToEmpty(input.message).trim();
+    if (!msg.isEmpty()) {
+      buf.append("\n\n");
+      buf.append(msg);
     }
 
     ChangeMessage message = new ChangeMessage(
diff --git a/src/main/java/com/googlesource/gerrit/plugins/wip/HttpModule.java b/src/main/java/com/googlesource/gerrit/plugins/wip/HttpModule.java
index a3fea87..387c747 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/wip/HttpModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/wip/HttpModule.java
@@ -19,7 +19,7 @@
 import com.google.gerrit.extensions.webui.WebUiPlugin;
 import com.google.gerrit.httpd.plugins.HttpPluginModule;
 
-public class HttpModule extends HttpPluginModule {
+class HttpModule extends HttpPluginModule {
   @Override
   protected void configureServlets() {
     DynamicSet.bind(binder(), WebUiPlugin.class)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/wip/Module.java b/src/main/java/com/googlesource/gerrit/plugins/wip/Module.java
index 6a02eb4..2a3d06d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/wip/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/wip/Module.java
@@ -15,10 +15,7 @@
 package com.googlesource.gerrit.plugins.wip;
 
 import static com.google.gerrit.server.change.RevisionResource.REVISION_KIND;
-import static com.googlesource.gerrit.plugins.wip.WorkInProgressCapability.WORK_IN_PROGRESS;
 
-import com.google.gerrit.extensions.annotations.Exports;
-import com.google.gerrit.extensions.config.CapabilityDefinition;
 import com.google.gerrit.extensions.restapi.RestApiModule;
 import com.google.inject.AbstractModule;
 
@@ -26,16 +23,13 @@
 
   @Override
   protected void configure() {
-    bind(CapabilityDefinition.class)
-        .annotatedWith(Exports.named(WORK_IN_PROGRESS))
-        .to(WorkInProgressCapability.class);
     install(new RestApiModule() {
       @Override
       protected void configure() {
         post(REVISION_KIND, "set-wip")
-            .to(WorkInProgressAction.class);
+            .to(WorkInProgress.class);
         post(REVISION_KIND, "set-ready")
-            .to(ReadyForReviewAction.class);
+            .to(ReadyForReview.class);
       }
     });
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/wip/ReadyForReviewAction.java b/src/main/java/com/googlesource/gerrit/plugins/wip/ReadyForReview.java
similarity index 78%
rename from src/main/java/com/googlesource/gerrit/plugins/wip/ReadyForReviewAction.java
rename to src/main/java/com/googlesource/gerrit/plugins/wip/ReadyForReview.java
index be68997..be1c4da 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/wip/ReadyForReviewAction.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/wip/ReadyForReview.java
@@ -14,7 +14,8 @@
 
 package com.googlesource.gerrit.plugins.wip;
 
-import com.google.gerrit.extensions.annotations.RequiresCapability;
+import java.io.IOException;
+
 import com.google.gerrit.extensions.restapi.ResourceConflictException;
 import com.google.gerrit.extensions.restapi.Response;
 import com.google.gerrit.extensions.restapi.RestModifyView;
@@ -25,26 +26,27 @@
 import com.google.gerrit.reviewdb.server.ReviewDb;
 import com.google.gerrit.server.CurrentUser;
 import com.google.gerrit.server.change.RevisionResource;
+import com.google.gerrit.server.index.ChangeIndexer;
 import com.google.gwtorm.server.OrmException;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 
-@RequiresCapability(WorkInProgressCapability.WORK_IN_PROGRESS)
-class ReadyForReviewAction extends BaseAction implements
+class ReadyForReview extends BaseAction implements
     UiAction<RevisionResource>,
     RestModifyView<RevisionResource, BaseAction.Input> {
 
   @Inject
-  ReadyForReviewAction(Provider<ReviewDb> dbProvider,
-      Provider<CurrentUser> userProvider) {
-    super(dbProvider, userProvider);
+  ReadyForReview(Provider<ReviewDb> dbProvider,
+      Provider<CurrentUser> userProvider,
+      ChangeIndexer indexer) {
+    super(dbProvider, userProvider, indexer);
   }
 
   @Override
   public Object apply(RevisionResource rsrc, Input input)
-      throws ResourceConflictException, OrmException {
+      throws ResourceConflictException, OrmException, IOException {
     Change change = rsrc.getChange();
-    if (change.getStatus() != Status.WORKINPROGRESS) {
+    if (change.getStatus() != Status.DRAFT) {
       throw new ResourceConflictException("change is " + status(change));
     }
 
@@ -52,7 +54,7 @@
       throw new ResourceConflictException("not current patch set");
     }
 
-    changeStatus(change, input, Status.WORKINPROGRESS, Status.NEW);
+    changeStatus(change, input, Status.DRAFT, Status.NEW);
     return Response.none();
   }
 
@@ -62,7 +64,8 @@
     return new Description()
         .setLabel("Ready")
         .setTitle("Set Ready For Review")
-        .setVisible(rsrc.getChange().getStatus() == Status.WORKINPROGRESS
+        .setVisible(rsrc.getControl().isOwner()
+           && rsrc.getChange().getStatus() == Status.DRAFT
            && rsrc.getPatchSet().getId().equals(current));
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/wip/SetCommand.java b/src/main/java/com/googlesource/gerrit/plugins/wip/SetCommand.java
index 535c85a..276dbf8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/wip/SetCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/wip/SetCommand.java
@@ -23,15 +23,15 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.gerrit.extensions.annotations.RequiresCapability;
 import com.google.gerrit.extensions.restapi.ResourceConflictException;
+import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
 import com.google.gerrit.reviewdb.client.Change;
 import com.google.gerrit.reviewdb.client.PatchSet;
 import com.google.gerrit.reviewdb.client.RevId;
 import com.google.gerrit.reviewdb.server.ReviewDb;
-import com.google.gerrit.server.change.ChangeResource;
+import com.google.gerrit.server.change.ChangesCollection;
 import com.google.gerrit.server.change.RevisionResource;
-import com.google.gerrit.server.project.ChangeControl;
+import com.google.gerrit.server.index.ChangeIndexer;
 import com.google.gerrit.server.project.NoSuchChangeException;
 import com.google.gerrit.server.project.ProjectControl;
 import com.google.gerrit.sshd.CommandMetaData;
@@ -41,9 +41,8 @@
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 
-@RequiresCapability(WorkInProgressCapability.WORK_IN_PROGRESS)
 @CommandMetaData(name = "set", description = "Mark the change as WIP or Ready")
-public class SetCommand extends SshCommand {
+class SetCommand extends SshCommand {
   private static final Logger log = LoggerFactory.getLogger(SetCommand.class);
 
   private final Set<PatchSet> patchSets = new HashSet<PatchSet>();
@@ -82,13 +81,16 @@
   private ReviewDb db;
 
   @Inject
-  private ChangeControl.Factory changeControlFactory;
+  private Provider<WorkInProgress> wipProvider;
 
   @Inject
-  private Provider<WorkInProgressAction> wipProvider;
+  private Provider<ReadyForReview> readyProvider;
 
   @Inject
-  private Provider<ReadyForReviewAction> readyProvider;
+  ChangeIndexer indexer;
+
+  @Inject
+  ChangesCollection changes;
 
   @Override
   protected void run() throws UnloggedFailure {
@@ -103,13 +105,8 @@
     for (PatchSet patchSet : patchSets) {
       try {
         mark(patchSet);
-      } catch (NoSuchChangeException e) {
-        ok = false;
-        writeError("no such change " + patchSet.getId().getParentKey().get());
-      } catch (ResourceConflictException e) {
-        writeError("error: " + e.getMessage() + "\n");
-        ok = false;
-      } catch (OrmException e) {
+      } catch (NoSuchChangeException | ResourceConflictException | IOException
+          | OrmException | ResourceNotFoundException e) {
         ok = false;
         writeError("fatal: internal server error while approving "
             + patchSet.getId() + "\n");
@@ -123,10 +120,11 @@
   }
 
   private void mark(PatchSet patchSet) throws NoSuchChangeException,
-      ResourceConflictException, OrmException {
-    RevisionResource rsrc = new RevisionResource(
-        new ChangeResource(changeControlFactory
-            .controlFor(patchSet.getId().getParentKey())), patchSet);
+      ResourceConflictException, OrmException, IOException,
+      ResourceNotFoundException {
+    RevisionResource rsrc =
+        new RevisionResource(changes.parse(patchSet.getId().getParentKey()),
+            patchSet);
     BaseAction.Input input = new BaseAction.Input();
     input.message = changeComment;
     if (wipChange) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/wip/SshModule.java b/src/main/java/com/googlesource/gerrit/plugins/wip/SshModule.java
index 7b21460..cbd39c1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/wip/SshModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/wip/SshModule.java
@@ -16,7 +16,7 @@
 
 import com.google.gerrit.sshd.PluginCommandModule;
 
-public class SshModule extends PluginCommandModule {
+class SshModule extends PluginCommandModule {
   @Override
   protected void configureCommands() {
     command(SetCommand.class);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/wip/WorkInProgressAction.java b/src/main/java/com/googlesource/gerrit/plugins/wip/WorkInProgress.java
similarity index 79%
rename from src/main/java/com/googlesource/gerrit/plugins/wip/WorkInProgressAction.java
rename to src/main/java/com/googlesource/gerrit/plugins/wip/WorkInProgress.java
index e5601f0..41eda46 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/wip/WorkInProgressAction.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/wip/WorkInProgress.java
@@ -14,7 +14,8 @@
 
 package com.googlesource.gerrit.plugins.wip;
 
-import com.google.gerrit.extensions.annotations.RequiresCapability;
+import java.io.IOException;
+
 import com.google.gerrit.extensions.restapi.ResourceConflictException;
 import com.google.gerrit.extensions.restapi.Response;
 import com.google.gerrit.extensions.restapi.RestModifyView;
@@ -25,24 +26,25 @@
 import com.google.gerrit.reviewdb.server.ReviewDb;
 import com.google.gerrit.server.CurrentUser;
 import com.google.gerrit.server.change.RevisionResource;
+import com.google.gerrit.server.index.ChangeIndexer;
 import com.google.gwtorm.server.OrmException;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 
-@RequiresCapability(WorkInProgressCapability.WORK_IN_PROGRESS)
-class WorkInProgressAction extends BaseAction implements
+class WorkInProgress extends BaseAction implements
     UiAction<RevisionResource>,
     RestModifyView<RevisionResource, BaseAction.Input> {
 
   @Inject
-  WorkInProgressAction(Provider<ReviewDb> dbProvider,
-      Provider<CurrentUser> userProvider) {
-    super(dbProvider, userProvider);
+  WorkInProgress(Provider<ReviewDb> dbProvider,
+      Provider<CurrentUser> userProvider,
+      ChangeIndexer indexer) {
+    super(dbProvider, userProvider, indexer);
   }
 
   @Override
   public Object apply(RevisionResource rsrc, Input input)
-      throws ResourceConflictException, OrmException {
+      throws ResourceConflictException, OrmException, IOException {
     Change change = rsrc.getChange();
     if (change.getStatus() != Status.NEW) {
       throw new ResourceConflictException("change is " + status(change));
@@ -52,7 +54,7 @@
       throw new ResourceConflictException("not current patch set");
     }
 
-    changeStatus(change, input, Status.NEW, Status.WORKINPROGRESS);
+    changeStatus(change, input, Status.NEW, Status.DRAFT);
     return Response.none();
   }
 
@@ -62,7 +64,8 @@
     return new Description()
         .setLabel("WIP")
         .setTitle("Set Work In Progress")
-        .setVisible(rsrc.getChange().getStatus() == Status.NEW
+        .setVisible(rsrc.getControl().isOwner()
+           && rsrc.getChange().getStatus() == Status.NEW
            && rsrc.getPatchSet().getId().equals(current));
   }
 }
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index a18fb93..e1de899 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -1,7 +1,7 @@
 Gerrit Work in Progress plugin
 ==============================
 
-This plugin adds a new button that allows an authorized user to set a
+This plugin adds a new button that allows a change owner to set a
 change to Work In Progress, and a button to change from WIP back
 to a "Ready For Review" state.
 
@@ -12,10 +12,6 @@
 In addition this plugin exposes this functionality as REST endpoints
 and SSH command.
 
-It is intended to be used in combination with the new "Change Owners" group. The
-plugin owned capability "Work In Progress" can be granted to that group,
-so that only change owners can toggle the WIP state.
-
 Work In Progress Workflow:
 --------------------------