BranchApi: Add method to get the branch's reflog

GetReflog uses Repository#getReflogReader, but this is not implemented
by DfsRepository which is used in the test framework. As a result, calls
to this API method from tests results in UnsupportedOperationException.

Modify GetReflog to catch the UOE and rethrow as MethodNotAllowed, so
we get a graceful failure rather than "internal server error".

In the tests, expect MethodNotAllowed, with a TODO to rework when/if
the implementation of getReflogReader is done in DfsRepository.

Change-Id: I0d0d718ca4e4ecf2c544ea6e912397c20e2fd7e3
This commit is contained in:
David Pursehouse
2017-06-13 19:42:53 +09:00
parent f0d37e4f80
commit 419ed573b9
4 changed files with 48 additions and 3 deletions

View File

@@ -157,6 +157,16 @@ public class ChangeIT extends AbstractDaemonTest {
System.setProperty("user.timezone", systemTimeZone);
}
@Test
public void reflog() throws Exception {
// Tests are using DfsRepository which does not implement getReflogReader,
// so this will always fail.
// TODO: change this if/when DfsRepository#getReflogReader is implemented.
exception.expect(MethodNotAllowedException.class);
exception.expectMessage("reflog not supported");
gApi.projects().name(project.get()).branch("master").reflog();
}
@Test
public void get() throws Exception {
PushOneCommit.Result r = createChange();

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.extensions.api.projects;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.NotImplementedException;
import com.google.gerrit.extensions.restapi.RestApiException;
import java.util.List;
public interface BranchApi {
BranchApi create(BranchInput in) throws RestApiException;
@@ -28,6 +29,8 @@ public interface BranchApi {
/** Returns the content of a file from the HEAD revision. */
BinaryResult file(String path) throws RestApiException;
List<ReflogEntryInfo> reflog() throws RestApiException;
/**
* A default implementation which allows source compatibility when adding new methods to the
* interface.
@@ -52,5 +55,10 @@ public interface BranchApi {
public BinaryResult file(String path) {
throw new NotImplementedException();
}
@Override
public List<ReflogEntryInfo> reflog() {
throw new NotImplementedException();
}
}
}

View File

@@ -19,6 +19,7 @@ import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
import com.google.gerrit.extensions.api.projects.BranchApi;
import com.google.gerrit.extensions.api.projects.BranchInfo;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.api.projects.ReflogEntryInfo;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.RestApiException;
@@ -29,10 +30,12 @@ import com.google.gerrit.server.project.DeleteBranch;
import com.google.gerrit.server.project.FileResource;
import com.google.gerrit.server.project.FilesCollection;
import com.google.gerrit.server.project.GetContent;
import com.google.gerrit.server.project.GetReflog;
import com.google.gerrit.server.project.ProjectResource;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import java.io.IOException;
import java.util.List;
public class BranchApiImpl implements BranchApi {
interface Factory {
@@ -44,6 +47,7 @@ public class BranchApiImpl implements BranchApi {
private final DeleteBranch deleteBranch;
private final FilesCollection filesCollection;
private final GetContent getContent;
private final GetReflog getReflog;
private final String ref;
private final ProjectResource project;
@@ -54,6 +58,7 @@ public class BranchApiImpl implements BranchApi {
DeleteBranch deleteBranch,
FilesCollection filesCollection,
GetContent getContent,
GetReflog getReflog,
@Assisted ProjectResource project,
@Assisted String ref) {
this.branches = branches;
@@ -61,6 +66,7 @@ public class BranchApiImpl implements BranchApi {
this.deleteBranch = deleteBranch;
this.filesCollection = filesCollection;
this.getContent = getContent;
this.getReflog = getReflog;
this.project = project;
this.ref = ref;
}
@@ -103,6 +109,15 @@ public class BranchApiImpl implements BranchApi {
}
}
@Override
public List<ReflogEntryInfo> reflog() throws RestApiException {
try {
return getReflog.apply(resource());
} catch (IOException e) {
throw new RestApiException("Cannot retrieve reflog", e);
}
}
private BranchResource resource() throws RestApiException, IOException {
return branches.parse(project, IdString.fromDecoded(ref));
}

View File

@@ -17,7 +17,9 @@ package com.google.gerrit.server.project;
import com.google.common.collect.Lists;
import com.google.gerrit.extensions.api.projects.ReflogEntryInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.server.CommonConverters;
import com.google.gerrit.server.args4j.TimestampHandler;
@@ -27,13 +29,16 @@ import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.ReflogReader;
import org.eclipse.jgit.lib.Repository;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GetReflog implements RestReadView<BranchResource> {
private static final Logger log = LoggerFactory.getLogger(GetReflog.class);
private final GitRepositoryManager repoManager;
@Option(
@@ -84,13 +89,20 @@ public class GetReflog implements RestReadView<BranchResource> {
@Override
public List<ReflogEntryInfo> apply(BranchResource rsrc)
throws AuthException, ResourceNotFoundException, RepositoryNotFoundException, IOException {
throws RestApiException, IOException {
if (!rsrc.getControl().isOwner()) {
throw new AuthException("not project owner");
}
try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
ReflogReader r = repo.getReflogReader(rsrc.getRef());
ReflogReader r;
try {
r = repo.getReflogReader(rsrc.getRef());
} catch (UnsupportedOperationException e) {
String msg = "reflog not supported on repo " + rsrc.getNameKey().get();
log.error(msg);
throw new MethodNotAllowedException(msg);
}
if (r == null) {
throw new ResourceNotFoundException(rsrc.getRef());
}