Search in sources :

Example 1 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class ComponentTagsAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String componentUuid = request.mandatoryParam(PARAM_COMPONENT_UUID);
    checkIfAnyComponentsNeedIssueSync(componentUuid);
    SearchRequest searchRequest = new SearchRequest().setComponentUuids(singletonList(componentUuid)).setTypes(ISSUE_TYPE_NAMES).setResolved(false).setCreatedAfter(request.param(PARAM_CREATED_AFTER));
    IssueQuery query = queryService.create(searchRequest);
    int pageSize = request.mandatoryParamAsInt(PAGE_SIZE);
    try (JsonWriter json = response.newJsonWriter()) {
        json.beginObject().name("tags").beginArray();
        for (Map.Entry<String, Long> tag : issueIndex.countTags(query, pageSize).entrySet()) {
            json.beginObject().prop("key", tag.getKey()).prop("value", tag.getValue()).endObject();
        }
        json.endArray().endObject();
    }
}
Also used : SearchRequest(org.sonar.server.issue.SearchRequest) IssueQuery(org.sonar.server.issue.index.IssueQuery) JsonWriter(org.sonar.api.utils.text.JsonWriter) Map(java.util.Map)

Example 2 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class SearchAction method doHandle.

private SearchWsResponse doHandle(SearchRequest request) {
    // prepare the Elasticsearch request
    SearchOptions options = createSearchOptionsFromRequest(request);
    EnumSet<SearchAdditionalField> additionalFields = SearchAdditionalField.getFromRequest(request);
    IssueQuery query = issueQueryFactory.create(request);
    Set<String> facetsRequiringProjectParameter = options.getFacets().stream().filter(FACETS_REQUIRING_PROJECT::contains).collect(toSet());
    checkArgument(facetsRequiringProjectParameter.isEmpty() || (!query.projectUuids().isEmpty()), "Facet(s) '%s' require to also filter by project", String.join(",", facetsRequiringProjectParameter));
    // execute request
    SearchResponse result = issueIndex.search(query, options);
    List<String> issueKeys = Arrays.stream(result.getHits().getHits()).map(SearchHit::getId).collect(MoreCollectors.toList(result.getHits().getHits().length));
    // load the additional information to be returned in response
    SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector(issueKeys);
    collectLoggedInUser(collector);
    collectRequestParams(collector, request);
    Facets facets = new Facets(result, Optional.ofNullable(query.timeZone()).orElse(system2.getDefaultTimeZone().toZoneId()));
    if (!options.getFacets().isEmpty()) {
        // add missing values to facets. For example if assignee "john" and facet on "assignees" are requested, then
        // "john" should always be listed in the facet. If it is not present, then it is added with value zero.
        // This is a constraint from webapp UX.
        completeFacets(facets, request, query);
        collectFacets(collector, facets);
    }
    SearchResponseData preloadedData = new SearchResponseData(emptyList());
    preloadedData.addRules(List.copyOf(query.rules()));
    SearchResponseData data = searchResponseLoader.load(preloadedData, collector, additionalFields, facets);
    // FIXME allow long in Paging
    Paging paging = forPageIndex(options.getPage()).withPageSize(options.getLimit()).andTotal((int) getTotalHits(result).value);
    return searchResponseFormat.formatSearch(additionalFields, data, paging, facets);
}
Also used : Facets(org.sonar.server.es.Facets) Paging(org.sonar.api.utils.Paging) SearchOptions(org.sonar.server.es.SearchOptions) SearchResponse(org.elasticsearch.action.search.SearchResponse) IssueQuery(org.sonar.server.issue.index.IssueQuery)

Example 3 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class IssuesFinderSortTest method should_sort_by_update_date.

@Test
public void should_sort_by_update_date() {
    Date date = new Date();
    Date date1 = DateUtils.addDays(date, -3);
    Date date2 = DateUtils.addDays(date, -2);
    Date date3 = DateUtils.addDays(date, -1);
    IssueDto issue1 = new IssueDto().setKee("A").setIssueUpdateDate(date1);
    IssueDto issue2 = new IssueDto().setKee("B").setIssueUpdateDate(date3);
    IssueDto issue3 = new IssueDto().setKee("C").setIssueUpdateDate(date2);
    List<IssueDto> dtoList = newArrayList(issue1, issue2, issue3);
    IssueQuery query = IssueQuery.builder().sort(IssueQuery.SORT_BY_UPDATE_DATE).asc(false).build();
    IssuesFinderSort issuesFinderSort = new IssuesFinderSort(dtoList, query);
    List<IssueDto> result = newArrayList(issuesFinderSort.sort());
    assertThat(result).hasSize(3);
    assertThat(result.get(0).getIssueUpdateDate()).isEqualTo(date3);
    assertThat(result.get(1).getIssueUpdateDate()).isEqualTo(date2);
    assertThat(result.get(2).getIssueUpdateDate()).isEqualTo(date1);
}
Also used : IssueDto(org.sonar.db.issue.IssueDto) IssueQuery(org.sonar.server.issue.index.IssueQuery) Date(java.util.Date) Test(org.junit.Test)

Example 4 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class IssuesFinderSortTest method should_sort_by_desc_severity.

@Test
public void should_sort_by_desc_severity() {
    IssueDto issue1 = new IssueDto().setKee("A").setSeverity("INFO");
    IssueDto issue2 = new IssueDto().setKee("B").setSeverity("BLOCKER");
    IssueDto issue3 = new IssueDto().setKee("C").setSeverity("MAJOR");
    List<IssueDto> dtoList = newArrayList(issue1, issue2, issue3);
    IssueQuery query = IssueQuery.builder().sort(IssueQuery.SORT_BY_SEVERITY).asc(false).build();
    IssuesFinderSort issuesFinderSort = new IssuesFinderSort(dtoList, query);
    List<IssueDto> result = newArrayList(issuesFinderSort.sort());
    assertThat(result).hasSize(3);
    assertThat(result.get(0).getSeverity()).isEqualTo("BLOCKER");
    assertThat(result.get(1).getSeverity()).isEqualTo("MAJOR");
    assertThat(result.get(2).getSeverity()).isEqualTo("INFO");
}
Also used : IssueDto(org.sonar.db.issue.IssueDto) IssueQuery(org.sonar.server.issue.index.IssueQuery) Test(org.junit.Test)

Example 5 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class IssuesFinderSortTest method should_not_sort_with_null_sort.

@Test
public void should_not_sort_with_null_sort() {
    IssueDto issue1 = new IssueDto().setKee("A").setAssigneeUuid("perceval");
    IssueDto issue2 = new IssueDto().setKee("B").setAssigneeUuid("arthur");
    IssueDto issue3 = new IssueDto().setKee("C").setAssigneeUuid("vincent");
    IssueDto issue4 = new IssueDto().setKee("D").setAssigneeUuid(null);
    List<IssueDto> dtoList = newArrayList(issue1, issue2, issue3, issue4);
    IssueQuery query = IssueQuery.builder().sort(null).build();
    IssuesFinderSort issuesFinderSort = new IssuesFinderSort(dtoList, query);
    List<IssueDto> result = newArrayList(issuesFinderSort.sort());
    assertThat(result).hasSize(4);
    assertThat(result.get(0).getAssigneeUuid()).isEqualTo("perceval");
    assertThat(result.get(1).getAssigneeUuid()).isEqualTo("arthur");
    assertThat(result.get(2).getAssigneeUuid()).isEqualTo("vincent");
    assertThat(result.get(3).getAssigneeUuid()).isNull();
}
Also used : IssueDto(org.sonar.db.issue.IssueDto) IssueQuery(org.sonar.server.issue.index.IssueQuery) Test(org.junit.Test)

Aggregations

IssueQuery (org.sonar.server.issue.index.IssueQuery)11 Test (org.junit.Test)8 IssueDto (org.sonar.db.issue.IssueDto)7 Date (java.util.Date)3 SearchOptions (org.sonar.server.es.SearchOptions)2 Collections (java.util.Collections)1 Map (java.util.Map)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 Paging (org.sonar.api.utils.Paging)1 JsonWriter (org.sonar.api.utils.text.JsonWriter)1 Facets (org.sonar.server.es.Facets)1 SearchRequest (org.sonar.server.issue.SearchRequest)1