Merge branch 'stable-2.14' into stable-2.15

* stable-2.14:
  BranchApi: Add missing throws declaration on NotImplemented#reflog
  FormatUtil: Fix Math#round() truncation error flagged by error-prone
  ReflogIT: Add test coverage for reflog permissions
  ChangeIT: Move reflog test to ReflogIT
  ChangeIT: Fix and expand reflog test
  BranchApi: Add method to get the branch's reflog
  GetReflog: Move ReflogEntryInfo to a separate class in extension API

Change-Id: I209a4450771ebb640e6997e11d24d699ba20a033
This commit is contained in:
David Pursehouse
2018-08-22 15:48:33 +09:00
2 changed files with 56 additions and 2 deletions

View File

@@ -21,19 +21,25 @@ import static com.google.gerrit.reviewdb.client.RefNames.changeMetaRef;
import com.google.gerrit.acceptance.AbstractDaemonTest; import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit; import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.UseLocalDisk; import com.google.gerrit.acceptance.UseLocalDisk;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.changes.ReviewInput; import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.api.groups.GroupApi;
import com.google.gerrit.extensions.api.projects.BranchApi; import com.google.gerrit.extensions.api.projects.BranchApi;
import com.google.gerrit.extensions.api.projects.ReflogEntryInfo; import com.google.gerrit.extensions.api.projects.ReflogEntryInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.project.Util;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import org.eclipse.jgit.lib.ReflogEntry; import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.junit.Test; import org.junit.Test;
@UseLocalDisk
public class ReflogIT extends AbstractDaemonTest { public class ReflogIT extends AbstractDaemonTest {
@Test @Test
@UseLocalDisk
public void guessRestApiInReflog() throws Exception { public void guessRestApiInReflog() throws Exception {
assume().that(notesMigration.disableChangeReviewDb()).isTrue(); assume().that(notesMigration.disableChangeReviewDb()).isTrue();
PushOneCommit.Result r = createChange(); PushOneCommit.Result r = createChange();
@@ -54,6 +60,7 @@ public class ReflogIT extends AbstractDaemonTest {
} }
@Test @Test
@UseLocalDisk
public void reflogUpdatedBySubmittingChange() throws Exception { public void reflogUpdatedBySubmittingChange() throws Exception {
BranchApi branchApi = gApi.projects().name(project.get()).branch("master"); BranchApi branchApi = gApi.projects().name(project.get()).branch("master");
List<ReflogEntryInfo> reflog = branchApi.reflog(); List<ReflogEntryInfo> reflog = branchApi.reflog();
@@ -74,4 +81,33 @@ public class ReflogIT extends AbstractDaemonTest {
reflog = branchApi.reflog(); reflog = branchApi.reflog();
assertThat(reflog).hasSize(refLogLen + 1); assertThat(reflog).hasSize(refLogLen + 1);
} }
@Test
@UseLocalDisk
public void regularUserIsNotAllowedToGetReflog() throws Exception {
setApiUser(user);
exception.expect(AuthException.class);
gApi.projects().name(project.get()).branch("master").reflog();
}
@Test
@UseLocalDisk
public void ownerUserIsAllowedToGetReflog() throws Exception {
GroupApi groupApi = gApi.groups().create(name("get-reflog"));
groupApi.addMembers("user");
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
Util.allow(cfg, Permission.OWNER, new AccountGroup.UUID(groupApi.get().id), "refs/*");
saveProjectConfig(project, cfg);
setApiUser(user);
gApi.projects().name(project.get()).branch("master").reflog();
}
@Test
@UseLocalDisk
public void adminUserIsAllowedToGetReflog() throws Exception {
setApiUser(admin);
gApi.projects().name(project.get()).branch("master").reflog();
}
} }

View File

@@ -137,7 +137,25 @@ public class FormatUtil {
if (size == 0) { if (size == 0) {
return Resources.C.notAvailable(); return Resources.C.notAvailable();
} }
int p = Math.abs(Math.round(delta * 100 / size)); int p = Math.abs(saturatedCast(delta * 100 / size));
return p + "%"; return p + "%";
} }
/**
* Returns the {@code int} nearest in value to {@code value}.
*
* @param value any {@code long} value
* @return the same value cast to {@code int} if it is in the range of the {@code int} type,
* {@link Integer#MAX_VALUE} if it is too large, or {@link Integer#MIN_VALUE} if it is too
* small
*/
private static int saturatedCast(long value) {
if (value > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (value < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return (int) value;
}
} }