Merge "Merge branch 'stable-2.16'"

This commit is contained in:
David Pursehouse
2019-01-24 08:15:46 +00:00
committed by Gerrit Code Review
8 changed files with 86 additions and 100 deletions

View File

@@ -18,11 +18,13 @@ import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
import com.google.gerrit.extensions.api.changes.FileApi;
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.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.assistedinject.Assisted;
@@ -33,12 +35,21 @@ class FileApiImpl implements FileApi {
private final GetContent getContent;
private final GetDiff getDiff;
private final Reviewed.PutReviewed putReviewed;
private final Reviewed.DeleteReviewed deleteReviewed;
private final FileResource file;
@Inject
FileApiImpl(GetContent getContent, GetDiff getDiff, @Assisted FileResource file) {
FileApiImpl(
GetContent getContent,
GetDiff getDiff,
Reviewed.PutReviewed putReviewed,
Reviewed.DeleteReviewed deleteReviewed,
@Assisted FileResource file) {
this.getContent = getContent;
this.getDiff = getDiff;
this.putReviewed = putReviewed;
this.deleteReviewed = deleteReviewed;
this.file = file;
}
@@ -88,6 +99,19 @@ class FileApiImpl implements FileApi {
};
}
@Override
public void setReviewed(boolean reviewed) throws RestApiException {
try {
if (reviewed) {
putReviewed.apply(file, new Input());
} else {
deleteReviewed.apply(file, new Input());
}
} catch (Exception e) {
throw asRestApiException(String.format("Cannot set %sreviewed", reviewed ? "" : "un"), e);
}
}
private DiffInfo get(DiffRequest r) throws RestApiException {
if (r.getBase() != null) {
getDiff.setBase(r.getBase());

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.project;
import static java.util.stream.Collectors.toSet;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
@@ -329,4 +330,14 @@ public class ProjectCacheImpl implements ProjectCache {
}
}
}
@VisibleForTesting
public void evictAllByName() {
byName.invalidateAll();
}
@VisibleForTesting
public long sizeAllByName() {
return byName.size();
}
}

View File

@@ -16,8 +16,6 @@ package com.google.gerrit.server.restapi.project;
import static com.google.gerrit.extensions.client.ProjectState.HIDDEN;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
@@ -62,18 +60,19 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
@@ -335,9 +334,10 @@ public class ListProjects implements RestReadView<TopLevelResource> {
PermissionBackend.WithUser perm = permissionBackend.user(currentUser);
final TreeMap<Project.NameKey, ProjectNode> treeMap = new TreeMap<>();
try {
for (Project.NameKey projectName : filter(perm)) {
final ProjectState e = projectCache.get(projectName);
if (e == null || (e.getProject().getState() == HIDDEN && !all && state != HIDDEN)) {
Iterable<ProjectState> projectStatesIt = filter(perm)::iterator;
for (ProjectState e : projectStatesIt) {
Project.NameKey projectName = e.getNameKey();
if (e.getProject().getState() == HIDDEN && !all && state != HIDDEN) {
// If we can't get it from the cache, pretend it's not present.
// If all wasn't selected, and it's HIDDEN, pretend it's not present.
// If state HIDDEN wasn't selected, and it's HIDDEN, pretend it's not present.
@@ -487,31 +487,21 @@ public class ListProjects implements RestReadView<TopLevelResource> {
}
}
private Collection<Project.NameKey> filter(PermissionBackend.WithUser perm)
throws BadRequestException, PermissionBackendException {
Stream<Project.NameKey> matches = scan();
private Stream<ProjectState> filter(PermissionBackend.WithUser perm) throws BadRequestException {
return StreamSupport.stream(scan().spliterator(), false)
.map(projectCache::get)
.filter(Objects::nonNull)
.filter(p -> permissionCheck(p, perm));
}
List<Project.NameKey> results = new ArrayList<>();
List<Project.NameKey> projectNameKeys = matches.sorted().collect(toList());
for (Project.NameKey nameKey : projectNameKeys) {
ProjectState state = projectCache.get(nameKey);
requireNonNull(state, () -> String.format("Failed to load project %s", nameKey));
// Hidden projects(permitsRead = false) should only be accessible by the project owners.
// READ_CONFIG is checked here because it's only allowed to project owners(ACCESS may also
// be allowed for other users). Allowing project owners to access here will help them to view
// and update the config of hidden projects easily.
ProjectPermission permissionToCheck =
state.statePermitsRead() ? ProjectPermission.ACCESS : ProjectPermission.READ_CONFIG;
try {
perm.project(nameKey).check(permissionToCheck);
results.add(nameKey);
} catch (AuthException e) {
// Not added to results.
}
}
return results;
private boolean permissionCheck(ProjectState state, PermissionBackend.WithUser perm) {
// Hidden projects(permitsRead = false) should only be accessible by the project owners.
// READ_CONFIG is checked here because it's only allowed to project owners(ACCESS may also
// be allowed for other users). Allowing project owners to access here will help them to view
// and update the config of hidden projects easily.
return perm.project(state.getNameKey())
.testOrFalse(
state.statePermitsRead() ? ProjectPermission.ACCESS : ProjectPermission.READ_CONFIG);
}
private boolean isParentAccessible(