Merge branch 'stable-3.0' into stable-3.1
* stable-3.0: Upgrade gitiles-servlet and blame-cache to 0.2-11 Change-Id: Id49aea43bbed52ba95bd4b12fe7d14b229b64479
This commit is contained in:
@@ -15,10 +15,12 @@
|
||||
package com.google.gerrit.extensions.api.changes;
|
||||
|
||||
import com.google.gerrit.extensions.client.DiffPreferencesInfo.Whitespace;
|
||||
import com.google.gerrit.extensions.common.BlameInfo;
|
||||
import com.google.gerrit.extensions.common.DiffInfo;
|
||||
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;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
public interface FileApi {
|
||||
@@ -42,6 +44,12 @@ public interface FileApi {
|
||||
/** Set the file reviewed or not reviewed */
|
||||
void setReviewed(boolean reviewed) throws RestApiException;
|
||||
|
||||
/**
|
||||
* Creates a request to retrieve the blame information. On the returned request formatting options
|
||||
* for the blame request can be set.
|
||||
*/
|
||||
BlameRequest blameRequest() throws RestApiException;
|
||||
|
||||
abstract class DiffRequest {
|
||||
private String base;
|
||||
private Integer context;
|
||||
@@ -97,6 +105,21 @@ public interface FileApi {
|
||||
}
|
||||
}
|
||||
|
||||
abstract class BlameRequest {
|
||||
private boolean forBase;
|
||||
|
||||
public abstract List<BlameInfo> get() throws RestApiException;
|
||||
|
||||
public BlameRequest forBase(boolean forBase) {
|
||||
this.forBase = forBase;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isForBase() {
|
||||
return forBase;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A default implementation which allows source compatibility when adding new methods to the
|
||||
* interface.
|
||||
@@ -131,5 +154,10 @@ public interface FileApi {
|
||||
public void setReviewed(boolean reviewed) throws RestApiException {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlameRequest blameRequest() throws RestApiException {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,16 +17,20 @@ package com.google.gerrit.server.api.changes;
|
||||
import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
|
||||
|
||||
import com.google.gerrit.extensions.api.changes.FileApi;
|
||||
import com.google.gerrit.extensions.common.BlameInfo;
|
||||
import com.google.gerrit.extensions.common.DiffInfo;
|
||||
import com.google.gerrit.extensions.common.Input;
|
||||
import com.google.gerrit.extensions.restapi.BinaryResult;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
import com.google.gerrit.server.change.FileResource;
|
||||
import com.google.gerrit.server.restapi.change.GetBlame;
|
||||
import com.google.gerrit.server.restapi.change.GetContent;
|
||||
import com.google.gerrit.server.restapi.change.GetDiff;
|
||||
import com.google.gerrit.server.restapi.change.Reviewed;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
import java.util.List;
|
||||
|
||||
class FileApiImpl implements FileApi {
|
||||
interface Factory {
|
||||
@@ -34,6 +38,7 @@ class FileApiImpl implements FileApi {
|
||||
}
|
||||
|
||||
private final GetContent getContent;
|
||||
private final Provider<GetBlame> getBlame;
|
||||
private final GetDiff getDiff;
|
||||
private final Reviewed.PutReviewed putReviewed;
|
||||
private final Reviewed.DeleteReviewed deleteReviewed;
|
||||
@@ -42,11 +47,13 @@ class FileApiImpl implements FileApi {
|
||||
@Inject
|
||||
FileApiImpl(
|
||||
GetContent getContent,
|
||||
Provider<GetBlame> getBlame,
|
||||
GetDiff getDiff,
|
||||
Reviewed.PutReviewed putReviewed,
|
||||
Reviewed.DeleteReviewed deleteReviewed,
|
||||
@Assisted FileResource file) {
|
||||
this.getContent = getContent;
|
||||
this.getBlame = getBlame;
|
||||
this.getDiff = getDiff;
|
||||
this.putReviewed = putReviewed;
|
||||
this.deleteReviewed = deleteReviewed;
|
||||
@@ -132,4 +139,18 @@ class FileApiImpl implements FileApi {
|
||||
throw asRestApiException("Cannot retrieve diff", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlameRequest blameRequest() throws RestApiException {
|
||||
return new BlameRequest() {
|
||||
@Override
|
||||
public List<BlameInfo> get() throws RestApiException {
|
||||
try {
|
||||
return getBlame.get().setBase(isForBase()).apply(file).value();
|
||||
} catch (Exception e) {
|
||||
throw asRestApiException("Cannot retrieve blame", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +76,11 @@ public class GetBlame implements RestReadView<FileResource> {
|
||||
this.autoMerger = autoMerger;
|
||||
}
|
||||
|
||||
public GetBlame setBase(boolean base) {
|
||||
this.base = base;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<List<BlameInfo>> apply(FileResource resource)
|
||||
throws RestApiException, IOException, InvalidChangeOperationException {
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
// Copyright (C) 2019 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.api.revision;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
||||
import com.google.gerrit.acceptance.PushOneCommit;
|
||||
import com.google.gerrit.extensions.common.BlameInfo;
|
||||
import com.google.gerrit.extensions.common.RangeInfo;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GetBlameIT extends AbstractDaemonTest {
|
||||
@Test
|
||||
public void forNonExistingFile() throws Exception {
|
||||
PushOneCommit.Result r = createChange("Test Change", "foo.txt", "FOO");
|
||||
List<BlameInfo> blameInfos =
|
||||
gApi.changes().id(r.getChangeId()).current().file("non-existing.txt").blameRequest().get();
|
||||
|
||||
// File doesn't exist in commit.
|
||||
assertThat(blameInfos).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forNonExistingFileFromBase() throws Exception {
|
||||
PushOneCommit.Result r = createChange("Test Change", "foo.txt", "FOO");
|
||||
List<BlameInfo> blameInfos =
|
||||
gApi.changes()
|
||||
.id(r.getChangeId())
|
||||
.current()
|
||||
.file("non-existing.txt")
|
||||
.blameRequest()
|
||||
.forBase(true)
|
||||
.get();
|
||||
|
||||
// File doesn't exist in base commit.
|
||||
assertThat(blameInfos).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forNewlyAddedFile() throws Exception {
|
||||
PushOneCommit.Result r = createChange("Test Change", "foo.txt", "FOO");
|
||||
List<BlameInfo> blameInfos =
|
||||
gApi.changes().id(r.getChangeId()).current().file("foo.txt").blameRequest().get();
|
||||
|
||||
assertThat(blameInfos).hasSize(1);
|
||||
BlameInfo blameInfo = blameInfos.get(0);
|
||||
assertThat(blameInfo.author).isEqualTo(admin.fullName());
|
||||
assertThat(blameInfo.id).isEqualTo(r.getCommit().getId().name());
|
||||
assertThat(blameInfo.commitMsg).isEqualTo(r.getCommit().getFullMessage());
|
||||
assertThat(blameInfo.time).isEqualTo(r.getCommit().getCommitTime());
|
||||
|
||||
assertThat(blameInfo.ranges).hasSize(1);
|
||||
RangeInfo rangeInfo = blameInfo.ranges.get(0);
|
||||
assertThat(rangeInfo.start).isEqualTo(1);
|
||||
assertThat(rangeInfo.end).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forNewlyAddedFileFromBase() throws Exception {
|
||||
String changeId = createChange("Test Change", "foo.txt", "FOO").getChangeId();
|
||||
List<BlameInfo> blameInfos =
|
||||
gApi.changes().id(changeId).current().file("foo.txt").blameRequest().forBase(true).get();
|
||||
|
||||
// File doesn't exist in base commit.
|
||||
assertThat(blameInfos).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forRecreatedFile() throws Exception {
|
||||
// Create change that adds 'foo.txt'.
|
||||
createChange("Change 1", "foo.txt", "FOO");
|
||||
|
||||
// Create change that deletes 'foo.txt'.
|
||||
pushFactory
|
||||
.create(admin.newIdent(), testRepo, "Change 2", "foo.txt", "FOO")
|
||||
.rm("refs/for/master");
|
||||
|
||||
// Create change that recreates 'foo.txt'.
|
||||
PushOneCommit.Result r = createChange("Change 3", "foo.txt", "FOO");
|
||||
List<BlameInfo> blameInfos =
|
||||
gApi.changes().id(r.getChangeId()).current().file("foo.txt").blameRequest().get();
|
||||
|
||||
assertThat(blameInfos).hasSize(1);
|
||||
BlameInfo blameInfo = blameInfos.get(0);
|
||||
assertThat(blameInfo.author).isEqualTo(admin.fullName());
|
||||
assertThat(blameInfo.id).isEqualTo(r.getCommit().getId().name());
|
||||
assertThat(blameInfo.commitMsg).isEqualTo(r.getCommit().getFullMessage());
|
||||
assertThat(blameInfo.time).isEqualTo(r.getCommit().getCommitTime());
|
||||
|
||||
assertThat(blameInfo.ranges).hasSize(1);
|
||||
RangeInfo rangeInfo = blameInfo.ranges.get(0);
|
||||
assertThat(rangeInfo.start).isEqualTo(1);
|
||||
assertThat(rangeInfo.end).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forRecreatedFileFromBase() throws Exception {
|
||||
// Create change that adds 'foo.txt'.
|
||||
createChange("Change 1", "foo.txt", "FOO");
|
||||
|
||||
// Create change that deletes 'foo.txt'.
|
||||
pushFactory
|
||||
.create(admin.newIdent(), testRepo, "Change 2", "foo.txt", "FOO")
|
||||
.rm("refs/for/master");
|
||||
|
||||
// Create change that recreates 'foo.txt'.
|
||||
String changeId3 = createChange("Change 3", "foo.txt", "FOO").getChangeId();
|
||||
List<BlameInfo> blameInfos =
|
||||
gApi.changes().id(changeId3).current().file("foo.txt").blameRequest().forBase(true).get();
|
||||
|
||||
// File doesn't exist in base commit.
|
||||
assertThat(blameInfos).isEmpty();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user