InlineEdit: Extend POST changes/<id>/edit endpoint to rename files

Change-Id: I702911b6ab5cec06e8d570d6dc196362e9e7e77e
This commit is contained in:
David Ostrovsky
2015-01-21 00:17:49 +01:00
committed by David Pursehouse
parent 2d142b11c1
commit a00c953a44
3 changed files with 51 additions and 8 deletions

View File

@@ -1317,14 +1317,15 @@ content isn't provided, it is wiped out for that file. As response
---- ----
[[post-edit]] [[post-edit]]
=== Restore file content in Change Edit === Restore file content or rename files in Change Edit
-- --
'POST /changes/link:#change-id[\{change-id\}]/edit 'POST /changes/link:#change-id[\{change-id\}]/edit
-- --
Creates empty change edit or restores file content in change edit. The Creates empty change edit, restores file content or renames files in change
request body needs to include a link:#change-edit-input[ChangeEditInput] edit. The request body needs to include a
entity when a file within change edit should be restored. link:#change-edit-input[ChangeEditInput] entity when a file within change
edit should be restored or old and new file names to rename a file.
.Request .Request
---- ----
@@ -1336,9 +1337,23 @@ entity when a file within change edit should be restored.
} }
---- ----
or for rename:
.Request
----
POST /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/edit HTTP/1.0
Content-Type: application/json; charset=UTF-8
{
"old_path": "foo",
"new_path": "bar"
}
----
When change edit doesn't exist for this change yet it is created. When path When change edit doesn't exist for this change yet it is created. When path
and restore flag are provided in request body, this file is restored. As and restore flag are provided in request body, this file is restored. When
response "`204 No Content`" is returned. old and new file names are provided, the file is renamed. As response
"`204 No Content`" is returned.
.Response .Response
---- ----
@@ -4066,6 +4081,8 @@ path within change edit.
|=========================== |===========================
|Field Name ||Description |Field Name ||Description
|`restore_path`|optional|Path to file to restore. |`restore_path`|optional|Path to file to restore.
|`old_path` |optional|Old path to file to rename.
|`new_path` |optional|New path to file to rename.
|=========================== |===========================
[[change-edit-message-input]] [[change-edit-message-input]]

View File

@@ -434,6 +434,25 @@ public class ChangeEditIT extends AbstractDaemonTest {
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME), CONTENT_OLD); ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME), CONTENT_OLD);
} }
@Test
public void renameFileRest() throws Exception {
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
Post.Input in = new Post.Input();
in.oldPath = FILE_NAME;
in.newPath = FILE_NAME3;
assertThat(adminSession.post(urlEdit(), in).getStatusCode()).isEqualTo(
SC_NO_CONTENT);
Optional<ChangeEdit> edit = editUtil.byChange(change);
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME3), CONTENT_OLD);
try {
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
fail("ResourceNotFoundException expected");
} catch (ResourceNotFoundException rnfe) {
}
}
@Test @Test
public void restoreDeletedFileInPatchSetRest() throws Exception { public void restoreDeletedFileInPatchSetRest() throws Exception {
Post.Input in = new Post.Input(); Post.Input in = new Post.Input();

View File

@@ -311,6 +311,8 @@ public class ChangeEdits implements
RestModifyView<ChangeResource, Post.Input> { RestModifyView<ChangeResource, Post.Input> {
public static class Input { public static class Input {
public String restorePath; public String restorePath;
public String oldPath;
public String newPath;
} }
private final Provider<ReviewDb> db; private final Provider<ReviewDb> db;
@@ -335,8 +337,13 @@ public class ChangeEdits implements
edit = createEdit(resource.getChange()); edit = createEdit(resource.getChange());
} }
if (input != null && !Strings.isNullOrEmpty(input.restorePath)) { if (input != null) {
if (!Strings.isNullOrEmpty(input.restorePath)) {
editModifier.restoreFile(edit.get(), input.restorePath); editModifier.restoreFile(edit.get(), input.restorePath);
} else if (!Strings.isNullOrEmpty(input.oldPath)
&& !Strings.isNullOrEmpty(input.newPath)) {
editModifier.renameFile(edit.get(), input.oldPath, input.newPath);
}
} }
return Response.none(); return Response.none();
} }