Search in sources :

Example 1 with QueryChanges

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

the class ChangesImpl method get.

private List<ChangeInfo> get(QueryRequest q) throws RestApiException {
    try (DynamicOptions dynamicOptions = new DynamicOptions(injector, dynamicBeans)) {
        QueryChanges qc = queryProvider.get();
        if (q.getQuery() != null) {
            qc.addQuery(q.getQuery());
        }
        qc.setLimit(q.getLimit());
        qc.setStart(q.getStart());
        qc.setNoLimit(q.getNoLimit());
        for (ListChangesOption option : q.getOptions()) {
            qc.addOption(option);
        }
        dynamicOptionParser.parseDynamicOptions(qc, q.getPluginOptions(), dynamicOptions);
        try {
            List<?> result = qc.apply(TopLevelResource.INSTANCE).value();
            if (result.isEmpty()) {
                return ImmutableList.of();
            }
            // Check type safety of result; the extension API should be safer than the
            // REST API in this case, since it's intended to be used in Java.
            Object first = requireNonNull(result.iterator().next());
            checkState(first instanceof ChangeInfo);
            @SuppressWarnings("unchecked") List<ChangeInfo> infos = (List<ChangeInfo>) result;
            return ImmutableList.copyOf(infos);
        } catch (Exception e) {
            throw asRestApiException("Cannot query changes", e);
        }
    }
}
Also used : DynamicOptions(com.google.gerrit.server.DynamicOptions) ListChangesOption(com.google.gerrit.extensions.client.ListChangesOption) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 2 with QueryChanges

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

the class QueryChangesIT method aliasQuery.

@Test
@SuppressWarnings("unchecked")
@GerritConfig(name = "operator-alias.change.numberaliastest", value = "change")
public void aliasQuery() throws Exception {
    String cId1 = createChange().getChangeId();
    String cId2 = createChange().getChangeId();
    int numericId1 = gApi.changes().id(cId1).get()._number;
    int numericId2 = gApi.changes().id(cId2).get()._number;
    QueryChanges queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("numberaliastest:12345");
    queryChanges.addQuery("numberaliastest:" + numericId1);
    queryChanges.addQuery("numberaliastest:" + numericId2);
    List<List<ChangeInfo>> result = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
    assertThat(result).hasSize(3);
    assertThat(result.get(0)).hasSize(0);
    assertThat(result.get(1)).hasSize(1);
    assertThat(result.get(2)).hasSize(1);
    assertThat(result.get(1).get(0)._number).isEqualTo(numericId1);
    assertThat(result.get(2).get(0)._number).isEqualTo(numericId2);
}
Also used : 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) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 3 with QueryChanges

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

the class QueryChangesIT method skipVisibility_privateChange.

@Test
@SuppressWarnings("unchecked")
public void skipVisibility_privateChange() throws Exception {
    TestRepository<InMemoryRepository> userRepo = cloneProject(project, user);
    PushOneCommit.Result result = pushFactory.create(user.newIdent(), userRepo).to("refs/for/master");
    requestScopeOperations.setApiUser(user.id());
    gApi.changes().id(result.getChangeId()).setPrivate(true);
    requestScopeOperations.setApiUser(admin.id());
    QueryChanges 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 : 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) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 4 with QueryChanges

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

the class QueryChangesIT method usingOutOfRangeLabelValuesDoesNotCauseError.

@Test
public void usingOutOfRangeLabelValuesDoesNotCauseError() throws Exception {
    for (String operator : ImmutableList.of("=", ">", ">=", "<", "<=")) {
        QueryChanges queryChanges = queryChangesProvider.get();
        queryChanges.addQuery("label:Code-Review" + operator + "10");
        queryChanges.addQuery("label:Code-Review" + operator + "-10");
        queryChanges.addQuery("Code-Review" + operator + "10");
        queryChanges.addQuery("Code-Review" + operator + "-10");
        assertThat(queryChanges.apply(TopLevelResource.INSTANCE).statusCode()).isEqualTo(SC_OK);
    }
}
Also used : QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 5 with QueryChanges

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

the class QueryChangesIT method defaultQueryCannotBeParsedDueToInvalidRegEx.

@Test
public void defaultQueryCannotBeParsedDueToInvalidRegEx() throws Exception {
    QueryChanges queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("^[A");
    BadRequestException e = assertThrows(BadRequestException.class, () -> queryChanges.apply(TopLevelResource.INSTANCE));
    assertThat(e).hasMessageThat().contains("no viable alternative at character '['");
}
Also used : BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

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