Revision API: Add method to get patch
Patch is returned as BinaryResult.
Change-Id: Ia92f00d7332d0f6d91744ffbcc6098824bdedd58
diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/PushOneCommit.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/PushOneCommit.java
index 85ba6de..77c38c8 100644
--- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/PushOneCommit.java
+++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/PushOneCommit.java
@@ -47,6 +47,23 @@
public static final String SUBJECT = "test commit";
public static final String FILE_NAME = "a.txt";
public static final String FILE_CONTENT = "some content";
+ public static final String PATCH =
+ "From %s Mon Sep 17 00:00:00 2001\n" +
+ "From: Administrator <admin@example.com>\n" +
+ "Date: %s\n" +
+ "Subject: [PATCH] test commit\n" +
+ "\n" +
+ "Change-Id: %s\n" +
+ "---\n" +
+ "\n" +
+ "diff --git a/a.txt b/a.txt\n" +
+ "new file mode 100644\n" +
+ "index 0000000..f0eec86\n" +
+ "--- /dev/null\n" +
+ "+++ b/a.txt\n" +
+ "@@ -0,0 +1 @@\n" +
+ "+some content\n" +
+ "\\ No newline at end of file\n";
public interface Factory {
PushOneCommit create(
diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/revision/RevisionIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/revision/RevisionIT.java
index 23e3685..5429b95 100644
--- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/revision/RevisionIT.java
+++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/revision/RevisionIT.java
@@ -17,6 +17,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.acceptance.PushOneCommit.FILE_CONTENT;
import static com.google.gerrit.acceptance.PushOneCommit.FILE_NAME;
+import static com.google.gerrit.acceptance.PushOneCommit.PATCH;
import static org.eclipse.jgit.lib.Constants.HEAD;
import com.google.common.base.Predicate;
@@ -40,6 +41,7 @@
import com.google.gerrit.extensions.common.CommentInfo;
import com.google.gerrit.extensions.common.DiffInfo;
import com.google.gerrit.extensions.common.MergeableInfo;
+import com.google.gerrit.extensions.common.RevisionInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
@@ -52,6 +54,8 @@
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
@@ -540,6 +544,25 @@
.isEqualTo(in.message);
}
+ @Test
+ public void patch() throws Exception {
+ PushOneCommit.Result r = createChange();
+ ChangeApi changeApi = gApi.changes()
+ .id(r.getChangeId());
+ BinaryResult bin = changeApi
+ .revision(r.getCommit().name())
+ .patch();
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ bin.writeTo(os);
+ String res = new String(os.toByteArray(), StandardCharsets.UTF_8);
+ DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
+ ChangeInfo change = changeApi.get();
+ RevisionInfo rev = change.revisions.get(change.currentRevision);
+ String date = dateFormat.format(rev.commit.author.date);
+ assertThat(res).isEqualTo(
+ String.format(PATCH, r.getCommitId().name(), date, r.getChangeId()));
+ }
+
private void merge(PushOneCommit.Result r) throws Exception {
revision(r).review(ReviewInput.approve());
revision(r).submit();
diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/changes/RevisionApi.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/changes/RevisionApi.java
index cb0d5f3..44c2ba4 100644
--- a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/changes/RevisionApi.java
+++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/changes/RevisionApi.java
@@ -18,6 +18,7 @@
import com.google.gerrit.extensions.common.CommentInfo;
import com.google.gerrit.extensions.common.FileInfo;
import com.google.gerrit.extensions.common.MergeableInfo;
+import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.NotImplementedException;
import com.google.gerrit.extensions.restapi.RestApiException;
@@ -57,6 +58,11 @@
CommentApi comment(String id) throws RestApiException;
+ /**
+ * Returns patch of revision.
+ */
+ BinaryResult patch() throws RestApiException;
+
Map<String, ActionInfo> actions() throws RestApiException;
/**
@@ -180,6 +186,11 @@
}
@Override
+ public BinaryResult patch() throws RestApiException {
+ throw new NotImplementedException();
+ }
+
+ @Override
public Map<String, ActionInfo> actions() throws RestApiException {
throw new NotImplementedException();
}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java
index 1f8aa0c..0569e5f 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java
@@ -31,6 +31,7 @@
import com.google.gerrit.extensions.common.CommentInfo;
import com.google.gerrit.extensions.common.FileInfo;
import com.google.gerrit.extensions.common.MergeableInfo;
+import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
@@ -41,6 +42,7 @@
import com.google.gerrit.server.change.DraftComments;
import com.google.gerrit.server.change.FileResource;
import com.google.gerrit.server.change.Files;
+import com.google.gerrit.server.change.GetPatch;
import com.google.gerrit.server.change.GetRevisionActions;
import com.google.gerrit.server.change.ListRevisionComments;
import com.google.gerrit.server.change.ListRevisionDrafts;
@@ -79,6 +81,7 @@
private final RevisionResource revision;
private final Provider<Files> files;
private final Provider<Files.ListFiles> listFiles;
+ private final Provider<GetPatch> getPatch;
private final Provider<PostReview> review;
private final Provider<Mergeable> mergeable;
private final FileApiImpl.Factory fileApi;
@@ -103,6 +106,7 @@
Reviewed.DeleteReviewed deleteReviewed,
Provider<Files> files,
Provider<Files.ListFiles> listFiles,
+ Provider<GetPatch> getPatch,
Provider<PostReview> review,
Provider<Mergeable> mergeable,
FileApiImpl.Factory fileApi,
@@ -127,6 +131,7 @@
this.putReviewed = putReviewed;
this.deleteReviewed = deleteReviewed;
this.listFiles = listFiles;
+ this.getPatch = getPatch;
this.mergeable = mergeable;
this.fileApi = fileApi;
this.listComments = listComments;
@@ -357,6 +362,15 @@
}
@Override
+ public BinaryResult patch() throws RestApiException {
+ try {
+ return getPatch.get().apply(revision);
+ } catch (IOException e) {
+ throw new RestApiException("Cannot get patch", e);
+ }
+ }
+
+ @Override
public Map<String, ActionInfo> actions() throws RestApiException {
return revisionActions.apply(revision).value();
}