Add Java API to get archive for a revision
This enables us to write tests for the GetArchive REST endpoint.
This change contains only a simple test to show how the API is used.
More tests will be implemented in follow-up changes.
Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I6c12e75d47da2d3fafed548ef7feed0bfe1c321c
diff --git a/java/com/google/gerrit/extensions/api/changes/RevisionApi.java b/java/com/google/gerrit/extensions/api/changes/RevisionApi.java
index 6c6389e5..7ae570f 100644
--- a/java/com/google/gerrit/extensions/api/changes/RevisionApi.java
+++ b/java/com/google/gerrit/extensions/api/changes/RevisionApi.java
@@ -16,6 +16,7 @@
import com.google.common.collect.ListMultimap;
import com.google.gerrit.common.Nullable;
+import com.google.gerrit.extensions.client.ArchiveFormat;
import com.google.gerrit.extensions.client.SubmitType;
import com.google.gerrit.extensions.common.ActionInfo;
import com.google.gerrit.extensions.common.ApprovalInfo;
@@ -158,6 +159,15 @@
/** Returns votes on the revision. */
ListMultimap<String, ApprovalInfo> votes() throws RestApiException;
+ /**
+ * Retrieves the revision as an archive.
+ *
+ * @param format the format of the archive
+ * @return the archive as {@link BinaryResult}
+ * @throws RestApiException
+ */
+ BinaryResult getArchive(ArchiveFormat format) throws RestApiException;
+
abstract class MergeListRequest {
private boolean addLinks;
private int uninterestingParent = 1;
@@ -392,5 +402,10 @@
public String etag() throws RestApiException {
throw new NotImplementedException();
}
+
+ @Override
+ public BinaryResult getArchive(ArchiveFormat format) throws RestApiException {
+ throw new NotImplementedException();
+ }
}
}
diff --git a/java/com/google/gerrit/extensions/client/ArchiveFormat.java b/java/com/google/gerrit/extensions/client/ArchiveFormat.java
new file mode 100644
index 0000000..4ec59cb
--- /dev/null
+++ b/java/com/google/gerrit/extensions/client/ArchiveFormat.java
@@ -0,0 +1,27 @@
+// Copyright (C) 2020 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.extensions.client;
+
+/**
+ * The {@link com.google.gerrit.server.restapi.change.GetArchive} REST endpoint allows to download
+ * revisions as archive. This enum defines the supported archive formats.
+ */
+public enum ArchiveFormat {
+ TGZ,
+ TAR,
+ TBZ2,
+ TXZ,
+ ZIP;
+}
diff --git a/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java b/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java
index 48a8689..b515dfe 100644
--- a/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java
+++ b/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java
@@ -37,6 +37,7 @@
import com.google.gerrit.extensions.api.changes.RevisionReviewerApi;
import com.google.gerrit.extensions.api.changes.RobotCommentApi;
import com.google.gerrit.extensions.api.changes.SubmitInput;
+import com.google.gerrit.extensions.client.ArchiveFormat;
import com.google.gerrit.extensions.client.SubmitType;
import com.google.gerrit.extensions.common.ActionInfo;
import com.google.gerrit.extensions.common.ApprovalInfo;
@@ -70,6 +71,7 @@
import com.google.gerrit.server.restapi.change.DraftComments;
import com.google.gerrit.server.restapi.change.Files;
import com.google.gerrit.server.restapi.change.Fixes;
+import com.google.gerrit.server.restapi.change.GetArchive;
import com.google.gerrit.server.restapi.change.GetCommit;
import com.google.gerrit.server.restapi.change.GetDescription;
import com.google.gerrit.server.restapi.change.GetFixPreview;
@@ -96,6 +98,7 @@
import com.google.inject.assistedinject.Assisted;
import java.util.EnumSet;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.eclipse.jgit.lib.Repository;
@@ -146,6 +149,7 @@
private final GetRelated getRelated;
private final PutDescription putDescription;
private final GetDescription getDescription;
+ private final Provider<GetArchive> getArchiveProvider;
private final ApprovalsUtil approvalsUtil;
private final AccountLoader.Factory accountLoaderFactory;
@@ -190,6 +194,7 @@
GetRelated getRelated,
PutDescription putDescription,
GetDescription getDescription,
+ Provider<GetArchive> getArchiveProvider,
ApprovalsUtil approvalsUtil,
AccountLoader.Factory accountLoaderFactory,
@Assisted RevisionResource r) {
@@ -232,6 +237,7 @@
this.getRelated = getRelated;
this.putDescription = putDescription;
this.getDescription = getDescription;
+ this.getArchiveProvider = getArchiveProvider;
this.approvalsUtil = approvalsUtil;
this.accountLoaderFactory = accountLoaderFactory;
this.revision = r;
@@ -649,4 +655,15 @@
public String etag() throws RestApiException {
return revisionActions.getETag(revision);
}
+
+ @Override
+ public BinaryResult getArchive(ArchiveFormat format) throws RestApiException {
+ GetArchive getArchive = getArchiveProvider.get();
+ getArchive.setFormat(format != null ? format.name().toLowerCase(Locale.US) : null);
+ try {
+ return getArchive.apply(revision).value();
+ } catch (Exception e) {
+ throw asRestApiException("Cannot get archive", e);
+ }
+ }
}
diff --git a/java/com/google/gerrit/server/change/ArchiveFormat.java b/java/com/google/gerrit/server/change/ArchiveFormatInternal.java
similarity index 95%
rename from java/com/google/gerrit/server/change/ArchiveFormat.java
rename to java/com/google/gerrit/server/change/ArchiveFormatInternal.java
index d895a66..f6e9ff9 100644
--- a/java/com/google/gerrit/server/change/ArchiveFormat.java
+++ b/java/com/google/gerrit/server/change/ArchiveFormatInternal.java
@@ -28,7 +28,7 @@
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectLoader;
-public enum ArchiveFormat {
+public enum ArchiveFormatInternal {
TGZ("application/x-gzip", new TgzFormat()),
TAR("application/x-tar", new TarFormat()),
TBZ2("application/x-bzip2", new Tbz2Format()),
@@ -40,7 +40,7 @@
private final String mimeType;
- ArchiveFormat(String mimeType, ArchiveCommand.Format<?> format) {
+ ArchiveFormatInternal(String mimeType, ArchiveCommand.Format<?> format) {
this.format = format;
this.mimeType = mimeType;
ArchiveCommand.registerFormat(name(), format);
diff --git a/java/com/google/gerrit/server/config/DownloadConfig.java b/java/com/google/gerrit/server/config/DownloadConfig.java
index 6dea07d..58ce098 100644
--- a/java/com/google/gerrit/server/config/DownloadConfig.java
+++ b/java/com/google/gerrit/server/config/DownloadConfig.java
@@ -17,7 +17,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.entities.CoreDownloadSchemes;
import com.google.gerrit.extensions.client.GeneralPreferencesInfo.DownloadCommand;
-import com.google.gerrit.server.change.ArchiveFormat;
+import com.google.gerrit.server.change.ArchiveFormatInternal;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.lang.reflect.Field;
@@ -37,7 +37,7 @@
public class DownloadConfig {
private final ImmutableSet<String> downloadSchemes;
private final ImmutableSet<DownloadCommand> downloadCommands;
- private final ImmutableSet<ArchiveFormat> archiveFormats;
+ private final ImmutableSet<ArchiveFormatInternal> archiveFormats;
@Inject
DownloadConfig(@GerritServerConfig Config cfg) {
@@ -69,13 +69,13 @@
String v = cfg.getString("download", null, "archive");
if (v == null) {
- archiveFormats = ImmutableSet.copyOf(EnumSet.allOf(ArchiveFormat.class));
+ archiveFormats = ImmutableSet.copyOf(EnumSet.allOf(ArchiveFormatInternal.class));
} else if (v.isEmpty() || "off".equalsIgnoreCase(v)) {
archiveFormats = ImmutableSet.of();
} else {
archiveFormats =
ImmutableSet.copyOf(
- ConfigUtil.getEnumList(cfg, "download", null, "archive", ArchiveFormat.TGZ));
+ ConfigUtil.getEnumList(cfg, "download", null, "archive", ArchiveFormatInternal.TGZ));
}
}
@@ -110,7 +110,7 @@
}
/** Archive formats for downloading. */
- public ImmutableSet<ArchiveFormat> getArchiveFormats() {
+ public ImmutableSet<ArchiveFormatInternal> getArchiveFormats() {
return archiveFormats;
}
}
diff --git a/java/com/google/gerrit/server/restapi/change/AllowedFormats.java b/java/com/google/gerrit/server/restapi/change/AllowedFormats.java
index 2e313a1..ebec3295 100644
--- a/java/com/google/gerrit/server/restapi/change/AllowedFormats.java
+++ b/java/com/google/gerrit/server/restapi/change/AllowedFormats.java
@@ -18,7 +18,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
-import com.google.gerrit.server.change.ArchiveFormat;
+import com.google.gerrit.server.change.ArchiveFormatInternal;
import com.google.gerrit.server.config.DownloadConfig;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@@ -28,13 +28,13 @@
@Singleton
public class AllowedFormats {
- final ImmutableMap<String, ArchiveFormat> extensions;
- final ImmutableSet<ArchiveFormat> allowed;
+ final ImmutableMap<String, ArchiveFormatInternal> extensions;
+ final ImmutableSet<ArchiveFormatInternal> allowed;
@Inject
AllowedFormats(DownloadConfig cfg) {
- Map<String, ArchiveFormat> exts = new HashMap<>();
- for (ArchiveFormat format : cfg.getArchiveFormats()) {
+ Map<String, ArchiveFormatInternal> exts = new HashMap<>();
+ for (ArchiveFormatInternal format : cfg.getArchiveFormats()) {
for (String ext : format.getSuffixes()) {
exts.put(ext, format);
}
@@ -46,14 +46,14 @@
// valid JAR file, whose code would have access to cookies on the domain.
allowed =
Sets.immutableEnumSet(
- Iterables.filter(cfg.getArchiveFormats(), f -> f != ArchiveFormat.ZIP));
+ Iterables.filter(cfg.getArchiveFormats(), f -> f != ArchiveFormatInternal.ZIP));
}
- public Set<ArchiveFormat> getAllowed() {
+ public Set<ArchiveFormatInternal> getAllowed() {
return allowed;
}
- public ImmutableMap<String, ArchiveFormat> getExtensions() {
+ public ImmutableMap<String, ArchiveFormatInternal> getExtensions() {
return extensions;
}
}
diff --git a/java/com/google/gerrit/server/restapi/change/GetArchive.java b/java/com/google/gerrit/server/restapi/change/GetArchive.java
index 4ebcbdd..33440b0 100644
--- a/java/com/google/gerrit/server/restapi/change/GetArchive.java
+++ b/java/com/google/gerrit/server/restapi/change/GetArchive.java
@@ -17,12 +17,13 @@
import static com.google.gerrit.git.ObjectIds.abbreviateName;
import com.google.common.base.Strings;
+import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestReadView;
-import com.google.gerrit.server.change.ArchiveFormat;
+import com.google.gerrit.server.change.ArchiveFormatInternal;
import com.google.gerrit.server.change.RevisionResource;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.inject.Inject;
@@ -38,9 +39,12 @@
public class GetArchive implements RestReadView<RevisionResource> {
private final GitRepositoryManager repoManager;
private final AllowedFormats allowedFormats;
+ @Nullable private String format;
@Option(name = "--format")
- private String format;
+ public void setFormat(String format) {
+ this.format = format;
+ }
@Inject
GetArchive(GitRepositoryManager repoManager, AllowedFormats allowedFormats) {
@@ -54,11 +58,11 @@
if (Strings.isNullOrEmpty(format)) {
throw new BadRequestException("format is not specified");
}
- final ArchiveFormat f = allowedFormats.extensions.get("." + format);
+ final ArchiveFormatInternal f = allowedFormats.extensions.get("." + format);
if (f == null) {
throw new BadRequestException("unknown archive format");
}
- if (f == ArchiveFormat.ZIP) {
+ if (f == ArchiveFormatInternal.ZIP) {
throw new MethodNotAllowedException("zip format is disabled");
}
boolean close = true;
@@ -103,7 +107,7 @@
}
}
- private static String name(ArchiveFormat format, RevWalk rw, RevCommit commit)
+ private static String name(ArchiveFormatInternal format, RevWalk rw, RevCommit commit)
throws IOException {
return String.format(
"%s%s", abbreviateName(commit, rw.getObjectReader()), format.getDefaultSuffix());
diff --git a/java/com/google/gerrit/server/restapi/change/PreviewSubmit.java b/java/com/google/gerrit/server/restapi/change/PreviewSubmit.java
index e6a60d5..ed6c0a5 100644
--- a/java/com/google/gerrit/server/restapi/change/PreviewSubmit.java
+++ b/java/com/google/gerrit/server/restapi/change/PreviewSubmit.java
@@ -29,7 +29,7 @@
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.IdentifiedUser;
-import com.google.gerrit.server.change.ArchiveFormat;
+import com.google.gerrit.server.change.ArchiveFormatInternal;
import com.google.gerrit.server.change.RevisionResource;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.ioutil.LimitedByteArrayOutputStream;
@@ -87,12 +87,12 @@
if (Strings.isNullOrEmpty(format)) {
throw new BadRequestException("format is not specified");
}
- ArchiveFormat f = allowedFormats.extensions.get("." + format);
+ ArchiveFormatInternal f = allowedFormats.extensions.get("." + format);
if (f == null && format.equals("tgz")) {
// Always allow tgz, even when the allowedFormats doesn't contain it.
// Then we allow at least one format even if the list of allowed
// formats is empty.
- f = ArchiveFormat.TGZ;
+ f = ArchiveFormatInternal.TGZ;
}
if (f == null) {
throw new BadRequestException("unknown archive format");
@@ -109,7 +109,7 @@
return Response.ok(getBundles(rsrc, f));
}
- private BinaryResult getBundles(RevisionResource rsrc, ArchiveFormat f)
+ private BinaryResult getBundles(RevisionResource rsrc, ArchiveFormatInternal f)
throws RestApiException, UpdateException, IOException, ConfigInvalidException,
PermissionBackendException {
IdentifiedUser caller = rsrc.getUser().asIdentifiedUser();
@@ -138,10 +138,11 @@
private static class SubmitPreviewResult extends BinaryResult {
private final MergeOp mergeOp;
- private final ArchiveFormat archiveFormat;
+ private final ArchiveFormatInternal archiveFormat;
private final int maxBundleSize;
- private SubmitPreviewResult(MergeOp mergeOp, ArchiveFormat archiveFormat, int maxBundleSize) {
+ private SubmitPreviewResult(
+ MergeOp mergeOp, ArchiveFormatInternal archiveFormat, int maxBundleSize) {
this.mergeOp = mergeOp;
this.archiveFormat = archiveFormat;
this.maxBundleSize = maxBundleSize;
diff --git a/java/com/google/gerrit/server/restapi/config/GetServerInfo.java b/java/com/google/gerrit/server/restapi/config/GetServerInfo.java
index 4ddc3e8..c83bf42 100644
--- a/java/com/google/gerrit/server/restapi/config/GetServerInfo.java
+++ b/java/com/google/gerrit/server/restapi/config/GetServerInfo.java
@@ -43,7 +43,7 @@
import com.google.gerrit.server.account.AccountVisibilityProvider;
import com.google.gerrit.server.account.Realm;
import com.google.gerrit.server.avatar.AvatarProvider;
-import com.google.gerrit.server.change.ArchiveFormat;
+import com.google.gerrit.server.change.ArchiveFormatInternal;
import com.google.gerrit.server.change.MergeabilityComputationBehavior;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.config.AllUsersName;
@@ -254,7 +254,9 @@
}
});
info.archives =
- archiveFormats.getAllowed().stream().map(ArchiveFormat::getShortName).collect(toList());
+ archiveFormats.getAllowed().stream()
+ .map(ArchiveFormatInternal::getShortName)
+ .collect(toList());
return info;
}
diff --git a/java/com/google/gerrit/sshd/commands/UploadArchive.java b/java/com/google/gerrit/sshd/commands/UploadArchive.java
index 8543a1c..67dc5a5 100644
--- a/java/com/google/gerrit/sshd/commands/UploadArchive.java
+++ b/java/com/google/gerrit/sshd/commands/UploadArchive.java
@@ -20,7 +20,7 @@
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.gerrit.extensions.restapi.AuthException;
-import com.google.gerrit.server.change.ArchiveFormat;
+import com.google.gerrit.server.change.ArchiveFormatInternal;
import com.google.gerrit.server.permissions.PermissionBackend;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.permissions.ProjectPermission;
@@ -174,7 +174,7 @@
// Parse Git arguments
readArguments();
- ArchiveFormat f = allowedFormats.getExtensions().get("." + options.format);
+ ArchiveFormatInternal f = allowedFormats.getExtensions().get("." + options.format);
if (f == null) {
throw new Failure(3, "fatal: upload-archive not permitted for format " + options.format);
}
@@ -222,8 +222,8 @@
}
}
- private Map<String, Object> getFormatOptions(ArchiveFormat f) {
- if (f == ArchiveFormat.ZIP) {
+ private Map<String, Object> getFormatOptions(ArchiveFormatInternal f) {
+ if (f == ArchiveFormatInternal.ZIP) {
int value =
Arrays.asList(
options.level0,
diff --git a/javatests/com/google/gerrit/acceptance/rest/change/GetArchiveIT.java b/javatests/com/google/gerrit/acceptance/rest/change/GetArchiveIT.java
new file mode 100644
index 0000000..63ed977
--- /dev/null
+++ b/javatests/com/google/gerrit/acceptance/rest/change/GetArchiveIT.java
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 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.acceptance.rest.change;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
+
+import com.google.gerrit.acceptance.AbstractDaemonTest;
+import com.google.gerrit.extensions.client.ArchiveFormat;
+import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
+import org.junit.Test;
+
+public class GetArchiveIT extends AbstractDaemonTest {
+ @Test
+ public void zipFormatIsDisabled() throws Exception {
+ String changeId = createChange().getChangeId();
+ MethodNotAllowedException ex =
+ assertThrows(
+ MethodNotAllowedException.class,
+ () -> gApi.changes().id(changeId).current().getArchive(ArchiveFormat.ZIP));
+ assertThat(ex).hasMessageThat().isEqualTo("zip format is disabled");
+ }
+}
diff --git a/javatests/com/google/gerrit/server/change/ArchiveFormatInternalTest.java b/javatests/com/google/gerrit/server/change/ArchiveFormatInternalTest.java
new file mode 100644
index 0000000..003225c
--- /dev/null
+++ b/javatests/com/google/gerrit/server/change/ArchiveFormatInternalTest.java
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 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 static com.google.common.truth.Truth.assertThat;
+import static java.util.stream.Collectors.toList;
+
+import com.google.gerrit.extensions.client.ArchiveFormat;
+import java.util.Arrays;
+import java.util.List;
+import org.junit.Test;
+
+public class ArchiveFormatInternalTest {
+ @Test
+ public void internalAndExternalArchiveFormatEnumsMatch() throws Exception {
+ assertThat(getEnumNames(ArchiveFormatInternal.class))
+ .containsExactlyElementsIn(getEnumNames(ArchiveFormat.class));
+ }
+
+ private static List<String> getEnumNames(Class<? extends Enum<?>> e) {
+ return Arrays.stream(e.getEnumConstants()).map(Enum::name).collect(toList());
+ }
+}