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
This commit is contained in:
Edwin Kempin
2020-03-25 15:25:47 +01:00
parent 4fb2fbb47b
commit 17617f7df3
12 changed files with 168 additions and 32 deletions

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.extensions.api.changes;
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 @@ public interface RevisionApi {
/** 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 interface RevisionApi {
public String etag() throws RestApiException {
throw new NotImplementedException();
}
@Override
public BinaryResult getArchive(ArchiveFormat format) throws RestApiException {
throw new NotImplementedException();
}
}
}

View File

@@ -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;
}

View File

@@ -37,6 +37,7 @@ import com.google.gerrit.extensions.api.changes.RevisionApi;
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.CreateDraftComment;
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.Provider;
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 @@ class RevisionApiImpl implements RevisionApi {
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 @@ class RevisionApiImpl implements RevisionApi {
GetRelated getRelated,
PutDescription putDescription,
GetDescription getDescription,
Provider<GetArchive> getArchiveProvider,
ApprovalsUtil approvalsUtil,
AccountLoader.Factory accountLoaderFactory,
@Assisted RevisionResource r) {
@@ -232,6 +237,7 @@ class RevisionApiImpl implements RevisionApi {
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 @@ class RevisionApiImpl implements RevisionApi {
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);
}
}
}

View File

@@ -28,7 +28,7 @@ import org.eclipse.jgit.archive.ZipFormat;
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 @@ public enum ArchiveFormat {
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);

View File

@@ -17,7 +17,7 @@ package com.google.gerrit.server.config;
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 @@ import org.eclipse.jgit.lib.Config;
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 @@ public class DownloadConfig {
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 @@ public class DownloadConfig {
}
/** Archive formats for downloading. */
public ImmutableSet<ArchiveFormat> getArchiveFormats() {
public ImmutableSet<ArchiveFormatInternal> getArchiveFormats() {
return archiveFormats;
}
}

View File

@@ -18,7 +18,7 @@ import com.google.common.collect.ImmutableMap;
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 @@ import java.util.Set;
@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 @@ public class AllowedFormats {
// 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;
}
}

View File

@@ -17,12 +17,13 @@ package com.google.gerrit.server.restapi.change;
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 @@ import org.kohsuke.args4j.Option;
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 @@ public class GetArchive implements RestReadView<RevisionResource> {
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 @@ public class GetArchive implements RestReadView<RevisionResource> {
}
}
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());

View File

@@ -29,7 +29,7 @@ import com.google.gerrit.extensions.restapi.RestApiException;
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 @@ public class PreviewSubmit implements RestReadView<RevisionResource> {
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 @@ public class PreviewSubmit implements RestReadView<RevisionResource> {
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 @@ public class PreviewSubmit implements RestReadView<RevisionResource> {
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;

View File

@@ -43,7 +43,7 @@ import com.google.gerrit.server.EnableSignedPush;
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 @@ public class GetServerInfo implements RestReadView<ConfigResource> {
}
});
info.archives =
archiveFormats.getAllowed().stream().map(ArchiveFormat::getShortName).collect(toList());
archiveFormats.getAllowed().stream()
.map(ArchiveFormatInternal::getShortName)
.collect(toList());
return info;
}

View File

@@ -20,7 +20,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
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 @@ public class UploadArchive extends AbstractGitCommand {
// 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 @@ public class UploadArchive extends AbstractGitCommand {
}
}
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,

View File

@@ -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");
}
}

View File

@@ -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());
}
}