Search in sources :

Example 11 with QueryChanges

use of com.google.gerrit.server.restapi.change.QueryChanges in project gerrit by GerritCodeReview.

the class QueryChangesIT method skipVisibility_noReadPermission.

@Test
@SuppressWarnings("unchecked")
public void skipVisibility_noReadPermission() throws Exception {
    createChange().getChangeId();
    requestScopeOperations.setApiUser(admin.id());
    QueryChanges queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("is:open repo:" + project.get());
    List<List<ChangeInfo>> result = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
    assertThat(result).hasSize(1);
    try (ProjectConfigUpdate u = updateProject(allProjects)) {
        ProjectConfig cfg = u.getConfig();
        removeAllBranchPermissions(cfg, Permission.READ);
        u.save();
    }
    queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("is:open repo:" + project.get());
    List<List<ChangeInfo>> result2 = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
    assertThat(result2).hasSize(0);
    queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("is:open repo:" + project.get());
    queryChanges.skipVisibility(true);
    List<List<ChangeInfo>> result3 = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
    assertThat(result3).hasSize(1);
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 12 with QueryChanges

use of com.google.gerrit.server.restapi.change.QueryChanges in project gerrit by GerritCodeReview.

the class QueryChangesIT method queryByFullNameEmailFormatWithEmptyFullNameWhenEmailMatchesSeveralAccounts.

@Test
public void queryByFullNameEmailFormatWithEmptyFullNameWhenEmailMatchesSeveralAccounts() throws Exception {
    // Create 2 accounts with the same preferred email (both account must have no external ID for
    // the email because otherwise the account with the external ID takes precedence).
    String email = "foo.bar@example.com";
    Account.Id account1 = accountOperations.newAccount().create();
    accountOperations.account(account1).forInvalidation().preferredEmailWithoutExternalId(email).invalidate();
    Account.Id account2 = accountOperations.newAccount().create();
    accountOperations.account(account2).forInvalidation().preferredEmailWithoutExternalId(email).invalidate();
    // Search with "Full Name <email>" format, but without full name. Both created accounts match
    // the email. In this case Gerrit falls back to match on the full name. Check that this logic
    // doesn't fail if the full name in the input string is not present.
    QueryChanges queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("<" + email + ">");
    assertThat(queryChanges.apply(TopLevelResource.INSTANCE).statusCode()).isEqualTo(SC_OK);
}
Also used : Account(com.google.gerrit.entities.Account) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 13 with QueryChanges

use of com.google.gerrit.server.restapi.change.QueryChanges in project gerrit by GerritCodeReview.

the class QueryChangesIT method withPagedResults.

@Test
@UseClockStep
@SuppressWarnings("unchecked")
public void withPagedResults() throws Exception {
    // Create 4 visible changes.
    createChange(testRepo).getChange().getId().get();
    createChange(testRepo).getChange().getId().get();
    int changeId3 = createChange(testRepo).getChange().getId().get();
    int changeId4 = createChange(testRepo).getChange().getId().get();
    // Create hidden project.
    Project.NameKey hiddenProject = projectOperations.newProject().create();
    TestRepository<InMemoryRepository> hiddenRepo = cloneProject(hiddenProject, admin);
    // Create 2 hidden changes.
    createChange(hiddenRepo);
    createChange(hiddenRepo);
    // Actually hide project
    projectOperations.project(hiddenProject).forUpdate().add(block(Permission.READ).ref("refs/*").group(REGISTERED_USERS)).update();
    // Create a change query that matches all changes (visible and hidden changes).
    // The index returns the changes ordered by last updated timestamp:
    // hiddenChange2, hiddenChange1, change4, change3, change2, change1
    QueryChanges queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("branch:master");
    // Set a limit on the query so that we need to paginate over the results from the index.
    queryChanges.setLimit(2);
    // Execute the query and verify the results.
    // Since the limit is set to 2, at most 2 changes are returned to user, but the index query is
    // executed with limit 3 (+1 so that we can populate the _more_changes field on the last
    // result).
    // This means the index query with limit 3 returns these changes:
    // hiddenChange2, hiddenChange1, change4
    // The 2 hidden changes are filtered out because they are not visible to the caller.
    // This means we have only one matching result (change4) but the limit (3) is not exhausted
    // yet. Hence the next page is loaded from the index (startIndex is 3 to skip the results
    // that we already processed, limit is again 3). The results for the next page are:
    // change3, change2, change1
    // change2 and change1 are dropped because they are over the limit.
    List<ChangeInfo> result = (List<ChangeInfo>) queryChanges.apply(TopLevelResource.INSTANCE).value();
    assertThat(result.stream().map(i -> i._number).collect(toList())).containsExactly(changeId3, changeId4);
}
Also used : TestProjectUpdate.block(com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.block) Patch(com.google.gerrit.entities.Patch) Arrays(java.util.Arrays) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) Inject(com.google.inject.Inject) RestResponse(com.google.gerrit.acceptance.RestResponse) REGISTERED_USERS(com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AccountOperations(com.google.gerrit.acceptance.testsuite.account.AccountOperations) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) ImmutableList(com.google.common.collect.ImmutableList) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) AuthException(com.google.gerrit.extensions.restapi.AuthException) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) TestRepository(org.eclipse.jgit.junit.TestRepository) ImmutableMap(com.google.common.collect.ImmutableMap) AccessSection(com.google.gerrit.entities.AccessSection) Permission(com.google.gerrit.entities.Permission) Account(com.google.gerrit.entities.Account) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) TopLevelResource(com.google.gerrit.extensions.restapi.TopLevelResource) RequestScopeOperations(com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations) UseClockStep(com.google.gerrit.acceptance.UseClockStep) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) List(java.util.List) ProjectOperations(com.google.gerrit.acceptance.testsuite.project.ProjectOperations) SC_OK(javax.servlet.http.HttpServletResponse.SC_OK) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Project(com.google.gerrit.entities.Project) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) GerritJUnit.assertThrows(com.google.gerrit.testing.GerritJUnit.assertThrows) Project(com.google.gerrit.entities.Project) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) UseClockStep(com.google.gerrit.acceptance.UseClockStep)

Example 14 with QueryChanges

use of com.google.gerrit.server.restapi.change.QueryChanges in project gerrit by GerritCodeReview.

the class QueryChangesIT method skipVisibility_rejectedForNonAdmin.

@Test
public void skipVisibility_rejectedForNonAdmin() throws Exception {
    requestScopeOperations.setApiUser(user.id());
    final QueryChanges queryChanges = queryChangesProvider.get();
    String query = "is:open repo:" + project.get();
    queryChanges.addQuery(query);
    AuthException thrown = assertThrows(AuthException.class, () -> queryChanges.skipVisibility(true));
    assertThat(thrown).hasMessageThat().isEqualTo("administrate server not permitted");
}
Also used : AuthException(com.google.gerrit.extensions.restapi.AuthException) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 15 with QueryChanges

use of com.google.gerrit.server.restapi.change.QueryChanges in project gerrit by GerritCodeReview.

the class StarredChanges method list.

@Override
public RestView<AccountResource> list() throws ResourceNotFoundException {
    return (RestReadView<AccountResource>) self -> {
        QueryChanges query = changes.list();
        query.addQuery("has:star");
        return query.apply(TopLevelResource.INSTANCE);
    };
}
Also used : RestReadView(com.google.gerrit.extensions.restapi.RestReadView) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges)

Aggregations

QueryChanges (com.google.gerrit.server.restapi.change.QueryChanges)15 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)13 Test (org.junit.Test)13 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)9 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 Collectors.toList (java.util.stream.Collectors.toList)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)3 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)2 Account (com.google.gerrit.entities.Account)2 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)2 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 RestResponse (com.google.gerrit.acceptance.RestResponse)1 UseClockStep (com.google.gerrit.acceptance.UseClockStep)1 AccountOperations (com.google.gerrit.acceptance.testsuite.account.AccountOperations)1