Add REST API for /restore

Changes can be restored via REST api.  The web UI now uses this API,
and the ssh command line uses the same backend.

Change-Id: I7a7e8706a52289f87ace5a60ef3bcd6dd2a2dbde
This commit is contained in:
Brad Larson
2012-11-14 18:54:12 -06:00
committed by Shawn O. Pearce
parent aab01c4701
commit 117a39b316
13 changed files with 159 additions and 258 deletions

View File

@@ -34,11 +34,6 @@ public interface ChangeManageService extends RemoteJsonService {
void createNewPatchSet(final PatchSet.Id patchSetId, final String newCommitMessage,
final AsyncCallback<ChangeDetail> callback);
@Audit
@SignInRequired
void restoreChange(PatchSet.Id patchSetId, String message,
AsyncCallback<ChangeDetail> callback);
@Audit
@SignInRequired
void publish(PatchSet.Id patchSetId, AsyncCallback<ChangeDetail> callback);

View File

@@ -31,6 +31,13 @@ public class ChangeApi {
api(id, "abandon").data(input).post(cb);
}
/** Restore a previously abandoned change to be open again. */
public static void restore(int id, String msg, AsyncCallback<ChangeInfo> cb) {
Input input = Input.create();
input.message(emptyToNull(msg));
api(id, "restore").data(input).post(cb);
}
/** Create a new change that reverts the delta caused by this change. */
public static void revert(int id, String msg, AsyncCallback<ChangeInfo> cb) {
Input input = Input.create();

View File

@@ -605,8 +605,22 @@ class PatchSetComplexDisclosurePanel extends ComplexDisclosurePanel
@Override
public void onSend() {
Util.MANAGE_SVC.restoreChange(patchSet.getId(), getMessageText(),
createCallback());
ChangeApi.restore(changeDetail.getChange().getChangeId(),
getMessageText(), new AsyncCallback<ChangeInfo>() {
@Override
public void onSuccess(ChangeInfo result) {
sent = true;
Gerrit.display(PageLinks.toChange(new Change.Id(result
._number())));
hide();
}
@Override
public void onFailure(Throwable caught) {
enableButtons(true);
new ErrorDialog(caught.getMessage()).center();
}
});
}
}.center();
}

View File

@@ -25,7 +25,6 @@ import com.google.inject.Inject;
class ChangeManageServiceImpl implements ChangeManageService {
private final SubmitAction.Factory submitAction;
private final RebaseChangeHandler.Factory rebaseChangeFactory;
private final RestoreChangeHandler.Factory restoreChangeHandlerFactory;
private final PublishAction.Factory publishAction;
private final DeleteDraftChange.Factory deleteDraftChangeFactory;
private final EditCommitMessageHandler.Factory editCommitMessageHandlerFactory;
@@ -33,13 +32,11 @@ class ChangeManageServiceImpl implements ChangeManageService {
@Inject
ChangeManageServiceImpl(final SubmitAction.Factory patchSetAction,
final RebaseChangeHandler.Factory rebaseChangeFactory,
final RestoreChangeHandler.Factory restoreChangeHandlerFactory,
final PublishAction.Factory publishAction,
final DeleteDraftChange.Factory deleteDraftChangeFactory,
final EditCommitMessageHandler.Factory editCommitMessageHandler) {
this.submitAction = patchSetAction;
this.rebaseChangeFactory = rebaseChangeFactory;
this.restoreChangeHandlerFactory = restoreChangeHandlerFactory;
this.publishAction = publishAction;
this.deleteDraftChangeFactory = deleteDraftChangeFactory;
this.editCommitMessageHandlerFactory = editCommitMessageHandler;
@@ -55,11 +52,6 @@ class ChangeManageServiceImpl implements ChangeManageService {
rebaseChangeFactory.create(patchSetId).to(callback);
}
public void restoreChange(final PatchSet.Id patchSetId, final String message,
final AsyncCallback<ChangeDetail> callback) {
restoreChangeHandlerFactory.create(patchSetId, message).to(callback);
}
public void publish(final PatchSet.Id patchSetId,
final AsyncCallback<ChangeDetail> callback) {
publishAction.create(patchSetId).to(callback);

View File

@@ -29,7 +29,6 @@ public class ChangeModule extends RpcServletModule {
@Override
protected void configure() {
factory(EditCommitMessageHandler.Factory.class);
factory(RestoreChangeHandler.Factory.class);
factory(RebaseChangeHandler.Factory.class);
factory(ChangeDetailFactory.Factory.class);
factory(IncludedInDetailFactory.Factory.class);

View File

@@ -1,76 +0,0 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.httpd.rpc.changedetail;
import com.google.gerrit.common.data.ChangeDetail;
import com.google.gerrit.common.data.ReviewResult;
import com.google.gerrit.common.errors.NoSuchEntityException;
import com.google.gerrit.httpd.rpc.Handler;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.server.changedetail.RestoreChange;
import com.google.gerrit.server.mail.EmailException;
import com.google.gerrit.server.patch.PatchSetInfoNotAvailableException;
import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import java.io.IOException;
import javax.annotation.Nullable;
class RestoreChangeHandler extends Handler<ChangeDetail> {
interface Factory {
RestoreChangeHandler create(PatchSet.Id patchSetId, String message);
}
private final Provider<RestoreChange> restoreChangeProvider;
private final ChangeDetailFactory.Factory changeDetailFactory;
private final PatchSet.Id patchSetId;
@Nullable
private final String message;
@Inject
RestoreChangeHandler(final Provider<RestoreChange> restoreChangeProvider,
final ChangeDetailFactory.Factory changeDetailFactory,
@Assisted final PatchSet.Id patchSetId,
@Assisted @Nullable final String message) {
this.restoreChangeProvider = restoreChangeProvider;
this.changeDetailFactory = changeDetailFactory;
this.patchSetId = patchSetId;
this.message = message;
}
@Override
public ChangeDetail call() throws NoSuchChangeException, OrmException,
EmailException, NoSuchEntityException, InvalidChangeOperationException,
PatchSetInfoNotAvailableException, RepositoryNotFoundException,
IOException {
final RestoreChange restoreChange = restoreChangeProvider.get();
restoreChange.setChangeId(patchSetId.getParentKey());
restoreChange.setMessage(message);
final ReviewResult result = restoreChange.call();
if (result.getErrors().size() > 0) {
throw new NoSuchChangeException(result.getChangeId());
}
return changeDetailFactory.create(result.getChangeId()).call();
}
}

View File

@@ -18,6 +18,7 @@ import com.google.common.base.Strings;
import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.Change;
@@ -40,6 +41,7 @@ public class Abandon implements RestModifyView<ChangeResource, Input> {
private final ChangeJson json;
public static class Input {
@DefaultInput
public String message;
}

View File

@@ -40,6 +40,7 @@ public class Module extends RestApiModule {
put(CHANGE_KIND, "topic").to(PutTopic.class);
delete(CHANGE_KIND, "topic").to(PutTopic.class);
post(CHANGE_KIND, "abandon").to(Abandon.class);
post(CHANGE_KIND, "restore").to(Restore.class);
child(CHANGE_KIND, "reviewers").to(Reviewers.class);
post(CHANGE_KIND, "revert").to(Revert.class);

View File

@@ -0,0 +1,120 @@
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.change;
import com.google.common.base.Strings;
import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Change.Status;
import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.change.Restore.Input;
import com.google.gerrit.server.mail.RestoredSender;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gwtorm.server.AtomicUpdate;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class Restore implements RestModifyView<ChangeResource, Input> {
private final ChangeHooks hooks;
private final RestoredSender.Factory restoredSenderFactory;
private final Provider<ReviewDb> dbProvider;
private final ChangeJson json;
public static class Input {
@DefaultInput
public String message;
}
@Inject
Restore(ChangeHooks hooks,
RestoredSender.Factory restoredSenderFactory,
Provider<ReviewDb> dbProvider,
ChangeJson json) {
this.hooks = hooks;
this.restoredSenderFactory = restoredSenderFactory;
this.dbProvider = dbProvider;
this.json = json;
}
@Override
public Class<Input> inputType() {
return Input.class;
}
@Override
public Object apply(ChangeResource req, Input input)
throws Exception {
ChangeControl control = req.getControl();
Change change = req.getChange();
if (!control.canRestore()) {
throw new AuthException("restore not permitted");
} else if (change.getStatus() != Status.ABANDONED) {
throw new ResourceConflictException("change is " + status(change));
}
// Create a message to accompany the restore change
ReviewDb db = dbProvider.get();
PatchSet.Id patchSetId = change.currentPatchSetId();
IdentifiedUser currentUser = (IdentifiedUser) control.getCurrentUser();
String message = Strings.emptyToNull(input.message);
final ChangeMessage cmsg = new ChangeMessage(
new ChangeMessage.Key(change.getId(), ChangeUtil.messageUUID(db)),
currentUser.getAccountId(), patchSetId);
StringBuilder msg = new StringBuilder();
msg.append(String.format("Patch Set %d: Restored", patchSetId.get()));
if (message != null) {
msg.append("\n\n");
msg.append(message);
}
cmsg.setMessage(msg.toString());
// Restore the change
final Change updatedChange = db.changes().atomicUpdate(
change.getId(),
new AtomicUpdate<Change>() {
@Override
public Change update(Change change) {
if (change.getStatus() == Change.Status.ABANDONED) {
change.setStatus(Change.Status.NEW);
ChangeUtil.updated(change);
return change;
}
return null;
}
});
if (updatedChange == null) {
throw new ResourceConflictException("change is "
+ status(db.changes().get(change.getId())));
}
ChangeUtil.updatedChange(db, currentUser, updatedChange, cmsg,
restoredSenderFactory);
hooks.doChangeRestoredHook(updatedChange, currentUser.getAccount(),
message, db);
return json.format(change.getId());
}
private static String status(Change change) {
return change != null ? change.getStatus().name().toLowerCase() : "deleted";
}
}

View File

@@ -1,157 +0,0 @@
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.changedetail;
import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.common.data.ReviewResult;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.ProjectUtil;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.mail.EmailException;
import com.google.gerrit.server.mail.RestoredSender;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gwtorm.server.AtomicUpdate;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import java.io.IOException;
import java.util.concurrent.Callable;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
public class RestoreChange implements Callable<ReviewResult> {
private final RestoredSender.Factory restoredSenderFactory;
private final ChangeControl.Factory changeControlFactory;
private final ReviewDb db;
private final GitRepositoryManager repoManager;
private final IdentifiedUser currentUser;
private final ChangeHooks hooks;
@Argument(index = 0, required = true, multiValued = false,
usage = "change to restore", metaVar = "CHANGE")
private Change.Id changeId;
public void setChangeId(final Change.Id changeId) {
this.changeId = changeId;
}
@Option(name = "--message", aliases = {"-m"},
usage = "optional message to append to change")
private String message;
public void setMessage(final String message) {
this.message = message;
}
@Inject
RestoreChange(final RestoredSender.Factory restoredSenderFactory,
final ChangeControl.Factory changeControlFactory, final ReviewDb db,
final GitRepositoryManager repoManager, final IdentifiedUser currentUser,
final ChangeHooks hooks) {
this.restoredSenderFactory = restoredSenderFactory;
this.changeControlFactory = changeControlFactory;
this.db = db;
this.repoManager = repoManager;
this.currentUser = currentUser;
this.hooks = hooks;
changeId = null;
message = null;
}
@Override
public ReviewResult call() throws EmailException, NoSuchChangeException,
InvalidChangeOperationException, OrmException,
RepositoryNotFoundException, IOException {
if (changeId == null) {
throw new InvalidChangeOperationException("changeId is required");
}
final ReviewResult result = new ReviewResult();
result.setChangeId(changeId);
final ChangeControl control = changeControlFactory.validateFor(changeId);
final Change change = db.changes().get(changeId);
final PatchSet.Id patchSetId = change.currentPatchSetId();
if (!control.canRestore()) {
result.addError(new ReviewResult.Error(
ReviewResult.Error.Type.RESTORE_NOT_PERMITTED));
return result;
}
final PatchSet patch = db.patchSets().get(patchSetId);
if (patch == null) {
throw new NoSuchChangeException(changeId);
}
final Branch.NameKey destBranch = control.getChange().getDest();
if (!ProjectUtil.branchExists(repoManager, destBranch)) {
result.addError(new ReviewResult.Error(
ReviewResult.Error.Type.DEST_BRANCH_NOT_FOUND, destBranch.get()));
return result;
}
// Create a message to accompany the restored change
final ChangeMessage cmsg =
new ChangeMessage(new ChangeMessage.Key(changeId, ChangeUtil
.messageUUID(db)), currentUser.getAccountId(), patchSetId);
final StringBuilder msgBuf =
new StringBuilder("Patch Set " + patchSetId.get() + ": Restored");
if (message != null && message.length() > 0) {
msgBuf.append("\n\n");
msgBuf.append(message);
}
cmsg.setMessage(msgBuf.toString());
// Restore the change
final Change updatedChange = db.changes().atomicUpdate(changeId,
new AtomicUpdate<Change>() {
@Override
public Change update(Change change) {
if (change.getStatus() == Change.Status.ABANDONED) {
change.setStatus(Change.Status.NEW);
ChangeUtil.updated(change);
return change;
} else {
return null;
}
}
});
if (updatedChange == null) {
result.addError(new ReviewResult.Error(
ReviewResult.Error.Type.CHANGE_NOT_ABANDONED));
return result;
}
ChangeUtil.updatedChange(db, currentUser, updatedChange, cmsg,
restoredSenderFactory);
hooks.doChangeRestoredHook(updatedChange, currentUser.getAccount(),
message, db);
return result;
}
}

View File

@@ -42,7 +42,6 @@ import com.google.gerrit.server.mail.MergeFailSender;
import com.google.gerrit.server.mail.MergedSender;
import com.google.gerrit.server.mail.RebasedPatchSetSender;
import com.google.gerrit.server.mail.ReplacePatchSetSender;
import com.google.gerrit.server.mail.RestoredSender;
import com.google.gerrit.server.patch.AddReviewer;
import com.google.gerrit.server.patch.RemoveReviewer;
import com.google.gerrit.server.project.ChangeControl;
@@ -85,7 +84,6 @@ public class GerritRequestModule extends FactoryModule {
factory(ReplacePatchSetSender.Factory.class);
factory(RebasedPatchSetSender.Factory.class);
factory(RemoveReviewer.Factory.class);
factory(RestoredSender.Factory.class);
factory(MergedSender.Factory.class);
factory(MergeFailSender.Factory.class);
factory(PerformCreateGroup.Factory.class);

View File

@@ -22,5 +22,6 @@ public class EmailModule extends FactoryModule {
factory(AbandonedSender.Factory.class);
factory(CommentSender.Factory.class);
factory(RevertedSender.Factory.class);
factory(RestoredSender.Factory.class);
}
}

View File

@@ -33,9 +33,9 @@ import com.google.gerrit.server.change.Abandon;
import com.google.gerrit.server.change.ChangeResource;
import com.google.gerrit.server.change.PostReview;
import com.google.gerrit.server.change.RevisionResource;
import com.google.gerrit.server.change.Restore;
import com.google.gerrit.server.changedetail.DeleteDraftPatchSet;
import com.google.gerrit.server.changedetail.PublishDraft;
import com.google.gerrit.server.changedetail.RestoreChange;
import com.google.gerrit.server.changedetail.Submit;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.InvalidChangeOperationException;
@@ -134,7 +134,7 @@ public class ReviewCommand extends SshCommand {
private PublishDraft.Factory publishDraftFactory;
@Inject
private Provider<RestoreChange> restoreChangeProvider;
private Provider<Restore> restoreProvider;
@Inject
private Submit.Factory submitFactory;
@@ -230,11 +230,16 @@ public class ReviewCommand extends SshCommand {
writeError("error: " + parseError(Type.CHANGE_IS_CLOSED) + "\n");
}
} else if (restoreChange) {
final RestoreChange restoreChange = restoreChangeProvider.get();
restoreChange.setChangeId(patchSetId.getParentKey());
restoreChange.setMessage(changeComment);
final ReviewResult result = restoreChange.call();
handleReviewResultErrors(result);
final Restore restore = restoreProvider.get();
final Restore.Input input = new Restore.Input();
input.message = changeComment;
try {
restore.apply(new ChangeResource(ctl), input);
} catch(AuthException e) {
writeError("error: " + parseError(Type.RESTORE_NOT_PERMITTED) + "\n");
} catch(ResourceConflictException e) {
writeError("error: " + parseError(Type.CHANGE_NOT_ABANDONED) + "\n");
}
}
if (submitChange) {
final ReviewResult result = submitFactory.create(patchSetId).call();