Allow setting notification handling on abandon via API
Change-Id: I099c9b094ac6b75248f44f665347506db15b2749
This commit is contained in:
		@@ -4113,6 +4113,11 @@ The `AbandonInput` entity contains information for abandoning a change.
 | 
			
		||||
|`message`     |optional|
 | 
			
		||||
Message to be added as review comment to the change when abandoning the
 | 
			
		||||
change.
 | 
			
		||||
|`notify`      |optional|
 | 
			
		||||
Notify handling that defines to whom email notifications should be sent after
 | 
			
		||||
the change is abandoned. +
 | 
			
		||||
Allowed values are `NONE`, `OWNER`, `OWNER_REVIEWERS` and `ALL`. +
 | 
			
		||||
If not set, the default is `ALL`.
 | 
			
		||||
|===========================
 | 
			
		||||
 | 
			
		||||
[[action-info]]
 | 
			
		||||
 
 | 
			
		||||
@@ -15,9 +15,11 @@
 | 
			
		||||
package com.google.gerrit.extensions.api.changes;
 | 
			
		||||
 | 
			
		||||
import com.google.gerrit.extensions.restapi.DefaultInput;
 | 
			
		||||
import com.google.gerrit.extensions.api.changes.NotifyHandling;
 | 
			
		||||
 | 
			
		||||
public class AbandonInput {
 | 
			
		||||
  @DefaultInput
 | 
			
		||||
  public String message;
 | 
			
		||||
  public NotifyHandling notify = NotifyHandling.ALL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@ package com.google.gerrit.server.change;
 | 
			
		||||
import com.google.common.base.Strings;
 | 
			
		||||
import com.google.gerrit.common.TimeUtil;
 | 
			
		||||
import com.google.gerrit.extensions.api.changes.AbandonInput;
 | 
			
		||||
import com.google.gerrit.extensions.api.changes.NotifyHandling;
 | 
			
		||||
import com.google.gerrit.extensions.common.ChangeInfo;
 | 
			
		||||
import com.google.gerrit.extensions.restapi.AuthException;
 | 
			
		||||
import com.google.gerrit.extensions.restapi.ResourceConflictException;
 | 
			
		||||
@@ -86,17 +87,22 @@ public class Abandon implements RestModifyView<ChangeResource, AbandonInput>,
 | 
			
		||||
    if (!control.canAbandon(dbProvider.get())) {
 | 
			
		||||
      throw new AuthException("abandon not permitted");
 | 
			
		||||
    }
 | 
			
		||||
    Change change = abandon(control, input.message);
 | 
			
		||||
    Change change = abandon(control, input.message, input.notify);
 | 
			
		||||
    return json.create(ChangeJson.NO_OPTIONS).format(change);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public Change abandon(ChangeControl control, String msgTxt)
 | 
			
		||||
      throws RestApiException, UpdateException {
 | 
			
		||||
    return abandon(control, msgTxt, NotifyHandling.ALL);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public Change abandon(ChangeControl control, String msgTxt,
 | 
			
		||||
      NotifyHandling notifyHandling) throws RestApiException, UpdateException {
 | 
			
		||||
    CurrentUser user = control.getUser();
 | 
			
		||||
    Account account = user.isIdentifiedUser()
 | 
			
		||||
        ? user.asIdentifiedUser().getAccount()
 | 
			
		||||
        : null;
 | 
			
		||||
    Op op = new Op(msgTxt, account);
 | 
			
		||||
    Op op = new Op(msgTxt, account, notifyHandling);
 | 
			
		||||
    try (BatchUpdate u = batchUpdateFactory.create(dbProvider.get(),
 | 
			
		||||
        control.getProject().getNameKey(), user, TimeUtil.nowTs())) {
 | 
			
		||||
      u.addOp(control.getId(), op).execute();
 | 
			
		||||
@@ -111,10 +117,12 @@ public class Abandon implements RestModifyView<ChangeResource, AbandonInput>,
 | 
			
		||||
    private Change change;
 | 
			
		||||
    private PatchSet patchSet;
 | 
			
		||||
    private ChangeMessage message;
 | 
			
		||||
    private NotifyHandling notifyHandling;
 | 
			
		||||
 | 
			
		||||
    private Op(String msgTxt, Account account) {
 | 
			
		||||
    private Op(String msgTxt, Account account, NotifyHandling notifyHandling) {
 | 
			
		||||
      this.account = account;
 | 
			
		||||
      this.msgTxt = msgTxt;
 | 
			
		||||
      this.notifyHandling = notifyHandling;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -167,11 +175,13 @@ public class Abandon implements RestModifyView<ChangeResource, AbandonInput>,
 | 
			
		||||
          cm.setFrom(account.getId());
 | 
			
		||||
        }
 | 
			
		||||
        cm.setChangeMessage(message.getMessage(), ctx.getWhen());
 | 
			
		||||
        cm.setNotify(notifyHandling);
 | 
			
		||||
        cm.send();
 | 
			
		||||
      } catch (Exception e) {
 | 
			
		||||
        log.error("Cannot email update for change " + change.getId(), e);
 | 
			
		||||
      }
 | 
			
		||||
      changeAbandoned.fire(change, patchSet, account, msgTxt, ctx.getWhen());
 | 
			
		||||
      changeAbandoned.fire(change, patchSet, account, msgTxt, ctx.getWhen(),
 | 
			
		||||
          notifyHandling);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -49,11 +49,13 @@ public class ChangeAbandoned {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void fire(ChangeInfo change, RevisionInfo revision,
 | 
			
		||||
      AccountInfo abandoner, String reason, Timestamp when) {
 | 
			
		||||
      AccountInfo abandoner, String reason, Timestamp when,
 | 
			
		||||
      NotifyHandling notifyHandling) {
 | 
			
		||||
    if (!listeners.iterator().hasNext()) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    Event event = new Event(change, revision, abandoner, reason, when);
 | 
			
		||||
    Event event = new Event(change, revision, abandoner, reason, when,
 | 
			
		||||
        notifyHandling);
 | 
			
		||||
    for (ChangeAbandonedListener l : listeners) {
 | 
			
		||||
      try {
 | 
			
		||||
        l.onChangeAbandoned(event);
 | 
			
		||||
@@ -64,7 +66,7 @@ public class ChangeAbandoned {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void fire(Change change, PatchSet ps, Account abandoner, String reason,
 | 
			
		||||
      Timestamp when) {
 | 
			
		||||
      Timestamp when, NotifyHandling notifyHandling) {
 | 
			
		||||
    if (!listeners.iterator().hasNext()) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
@@ -72,7 +74,7 @@ public class ChangeAbandoned {
 | 
			
		||||
      fire(util.changeInfo(change),
 | 
			
		||||
          util.revisionInfo(change.getProject(), ps),
 | 
			
		||||
          util.accountInfo(abandoner),
 | 
			
		||||
          reason, when);
 | 
			
		||||
          reason, when, notifyHandling);
 | 
			
		||||
    } catch (PatchListNotAvailableException | GpgException | IOException
 | 
			
		||||
        | OrmException e) {
 | 
			
		||||
      log.error("Couldn't fire event", e);
 | 
			
		||||
@@ -85,8 +87,8 @@ public class ChangeAbandoned {
 | 
			
		||||
    private final String reason;
 | 
			
		||||
 | 
			
		||||
    Event(ChangeInfo change, RevisionInfo revision, AccountInfo abandoner,
 | 
			
		||||
        String reason, Timestamp when) {
 | 
			
		||||
      super(change, revision, abandoner, when, NotifyHandling.ALL);
 | 
			
		||||
        String reason, Timestamp when, NotifyHandling notifyHandling) {
 | 
			
		||||
      super(change, revision, abandoner, when, notifyHandling);
 | 
			
		||||
      this.abandoner = abandoner;
 | 
			
		||||
      this.reason = reason;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user