Allow setting notification handling on abandon via API

Change-Id: I099c9b094ac6b75248f44f665347506db15b2749
This commit is contained in:
Stephen Li
2016-08-02 11:05:11 -07:00
parent f5eeb927be
commit e5fcdf7910
4 changed files with 29 additions and 10 deletions

View File

@@ -4113,6 +4113,11 @@ The `AbandonInput` entity contains information for abandoning a change.
|`message` |optional| |`message` |optional|
Message to be added as review comment to the change when abandoning the Message to be added as review comment to the change when abandoning the
change. 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]] [[action-info]]

View File

@@ -15,9 +15,11 @@
package com.google.gerrit.extensions.api.changes; package com.google.gerrit.extensions.api.changes;
import com.google.gerrit.extensions.restapi.DefaultInput; import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.api.changes.NotifyHandling;
public class AbandonInput { public class AbandonInput {
@DefaultInput @DefaultInput
public String message; public String message;
public NotifyHandling notify = NotifyHandling.ALL;
} }

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.server.change;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.gerrit.common.TimeUtil; import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.extensions.api.changes.AbandonInput; 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.common.ChangeInfo;
import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ResourceConflictException; import com.google.gerrit.extensions.restapi.ResourceConflictException;
@@ -86,17 +87,22 @@ public class Abandon implements RestModifyView<ChangeResource, AbandonInput>,
if (!control.canAbandon(dbProvider.get())) { if (!control.canAbandon(dbProvider.get())) {
throw new AuthException("abandon not permitted"); 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); return json.create(ChangeJson.NO_OPTIONS).format(change);
} }
public Change abandon(ChangeControl control, String msgTxt) public Change abandon(ChangeControl control, String msgTxt)
throws RestApiException, UpdateException { 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(); CurrentUser user = control.getUser();
Account account = user.isIdentifiedUser() Account account = user.isIdentifiedUser()
? user.asIdentifiedUser().getAccount() ? user.asIdentifiedUser().getAccount()
: null; : null;
Op op = new Op(msgTxt, account); Op op = new Op(msgTxt, account, notifyHandling);
try (BatchUpdate u = batchUpdateFactory.create(dbProvider.get(), try (BatchUpdate u = batchUpdateFactory.create(dbProvider.get(),
control.getProject().getNameKey(), user, TimeUtil.nowTs())) { control.getProject().getNameKey(), user, TimeUtil.nowTs())) {
u.addOp(control.getId(), op).execute(); u.addOp(control.getId(), op).execute();
@@ -111,10 +117,12 @@ public class Abandon implements RestModifyView<ChangeResource, AbandonInput>,
private Change change; private Change change;
private PatchSet patchSet; private PatchSet patchSet;
private ChangeMessage message; private ChangeMessage message;
private NotifyHandling notifyHandling;
private Op(String msgTxt, Account account) { private Op(String msgTxt, Account account, NotifyHandling notifyHandling) {
this.account = account; this.account = account;
this.msgTxt = msgTxt; this.msgTxt = msgTxt;
this.notifyHandling = notifyHandling;
} }
@Override @Override
@@ -167,11 +175,13 @@ public class Abandon implements RestModifyView<ChangeResource, AbandonInput>,
cm.setFrom(account.getId()); cm.setFrom(account.getId());
} }
cm.setChangeMessage(message.getMessage(), ctx.getWhen()); cm.setChangeMessage(message.getMessage(), ctx.getWhen());
cm.setNotify(notifyHandling);
cm.send(); cm.send();
} catch (Exception e) { } catch (Exception e) {
log.error("Cannot email update for change " + change.getId(), 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);
} }
} }

View File

@@ -49,11 +49,13 @@ public class ChangeAbandoned {
} }
public void fire(ChangeInfo change, RevisionInfo revision, 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()) { if (!listeners.iterator().hasNext()) {
return; return;
} }
Event event = new Event(change, revision, abandoner, reason, when); Event event = new Event(change, revision, abandoner, reason, when,
notifyHandling);
for (ChangeAbandonedListener l : listeners) { for (ChangeAbandonedListener l : listeners) {
try { try {
l.onChangeAbandoned(event); l.onChangeAbandoned(event);
@@ -64,7 +66,7 @@ public class ChangeAbandoned {
} }
public void fire(Change change, PatchSet ps, Account abandoner, String reason, public void fire(Change change, PatchSet ps, Account abandoner, String reason,
Timestamp when) { Timestamp when, NotifyHandling notifyHandling) {
if (!listeners.iterator().hasNext()) { if (!listeners.iterator().hasNext()) {
return; return;
} }
@@ -72,7 +74,7 @@ public class ChangeAbandoned {
fire(util.changeInfo(change), fire(util.changeInfo(change),
util.revisionInfo(change.getProject(), ps), util.revisionInfo(change.getProject(), ps),
util.accountInfo(abandoner), util.accountInfo(abandoner),
reason, when); reason, when, notifyHandling);
} catch (PatchListNotAvailableException | GpgException | IOException } catch (PatchListNotAvailableException | GpgException | IOException
| OrmException e) { | OrmException e) {
log.error("Couldn't fire event", e); log.error("Couldn't fire event", e);
@@ -85,8 +87,8 @@ public class ChangeAbandoned {
private final String reason; private final String reason;
Event(ChangeInfo change, RevisionInfo revision, AccountInfo abandoner, Event(ChangeInfo change, RevisionInfo revision, AccountInfo abandoner,
String reason, Timestamp when) { String reason, Timestamp when, NotifyHandling notifyHandling) {
super(change, revision, abandoner, when, NotifyHandling.ALL); super(change, revision, abandoner, when, notifyHandling);
this.abandoner = abandoner; this.abandoner = abandoner;
this.reason = reason; this.reason = reason;
} }