package org.sonar.server.issue.index;
import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.Collection;
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;
-import static org.sonar.api.issue.Issue.STATUS_OPEN;
-import static org.sonar.api.issue.Issue.STATUS_REOPENED;
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
import static org.sonar.server.es.BaseDoc.epochMillisToEpochSeconds;
import static org.sonar.server.es.EsUtils.escapeSpecialRegexChars;
.collect(MoreCollectors.toList(branchUuids.size()));
}
- public BranchStatistics searchPrStatistics(String projectUuid, String pullRequestUuid) {
- SearchRequestBuilder request = client.prepareSearch(INDEX_TYPE_ISSUE)
- .setRouting(projectUuid)
- .setQuery(
- boolQuery()
- .must(termQuery(FIELD_ISSUE_BRANCH_UUID, pullRequestUuid))
- .mustNot(existsQuery(FIELD_ISSUE_RESOLUTION))
- .must(termsQuery(FIELD_ISSUE_STATUS, STATUS_OPEN, STATUS_REOPENED))
- .must(termQuery(FIELD_ISSUE_IS_MAIN_BRANCH, Boolean.toString(false))))
- .setSize(0)
- .addAggregation(AggregationBuilders.terms("types").field(FIELD_ISSUE_TYPE));
-
- ImmutableMap<String, Long> issueCountByType = ((StringTerms) request.get()
- .getAggregations()
- .get("types")).getBuckets()
- .stream()
- .collect(uniqueIndex(StringTerms.Bucket::getKeyAsString, InternalTerms.Bucket::getDocCount));
-
- return new BranchStatistics(pullRequestUuid, issueCountByType);
- }
-
public List<SecurityStandardCategoryStatistics> getSansTop25Report(String projectUuid, boolean isViewOrApp, boolean includeCwe) {
SearchRequestBuilder request = prepareNonClosedVulnerabilitiesAndHotspotSearch(projectUuid, isViewOrApp);
Stream.of(SANS_TOP_25_INSECURE_INTERACTION, SANS_TOP_25_RISKY_RESOURCE, SANS_TOP_25_POROUS_DEFENSES).forEach(sansCategory -> {
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
-import com.tngtech.java.junit.dataprovider.DataProvider;
-import com.tngtech.java.junit.dataprovider.DataProviderRunner;
-import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.junit.runner.RunWith;
import org.sonar.api.issue.Issue;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.internal.TestSystem2;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.rules.ExpectedException.none;
-import static org.sonar.api.issue.Issue.RESOLUTION_FALSE_POSITIVE;
import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
-import static org.sonar.api.issue.Issue.RESOLUTION_REMOVED;
-import static org.sonar.api.issue.Issue.RESOLUTION_WONT_FIX;
-import static org.sonar.api.issue.Issue.STATUS_CLOSED;
-import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
-import static org.sonar.api.issue.Issue.STATUS_OPEN;
-import static org.sonar.api.issue.Issue.STATUS_REOPENED;
-import static org.sonar.api.issue.Issue.STATUS_RESOLVED;
import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.api.rules.RuleType.BUG;
import static org.sonar.api.rules.RuleType.CODE_SMELL;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.server.issue.IssueDocTesting.newDoc;
-@RunWith(DataProviderRunner.class)
public class IssueIndexTest {
@Rule
assertThat(underTest.searchBranchStatistics(project.uuid(), singletonList("unknown"))).isEmpty();
}
- @Test
- @UseDataProvider("resolutionAndStatusAndExpectedCount")
- public void searchPrStatistics_finds_unresolved_and_unconfirmed_issues(String status, String resolution, long expectedCount) {
- ComponentDto project = db.components().insertMainBranch();
- ComponentDto pr = db.components().insertProjectBranch(project);
- ComponentDto fileOnPr = db.components().insertComponent(newFileDto(pr));
- indexIssues(newDoc(project),
- newDoc(pr).setType(BUG).setStatus(status).setResolution(resolution),
- newDoc(pr).setType(VULNERABILITY).setStatus(status).setResolution(resolution),
- newDoc(pr).setType(CODE_SMELL).setStatus(status).setResolution(resolution),
- newDoc(fileOnPr).setType(BUG).setStatus(status).setResolution(resolution),
- newDoc(fileOnPr).setType(VULNERABILITY).setStatus(status).setResolution(resolution),
- newDoc(fileOnPr).setType(CODE_SMELL).setStatus(status).setResolution(resolution)
- );
-
- BranchStatistics branchStatistics = underTest.searchPrStatistics(project.uuid(), pr.uuid());
-
- assertThat(branchStatistics)
- .extracting(
- BranchStatistics::getBranchUuid,
- BranchStatistics::getBugs,
- BranchStatistics::getVulnerabilities,
- BranchStatistics::getCodeSmells
- ).containsExactly(pr.uuid(), expectedCount, expectedCount, expectedCount);
- }
-
- @DataProvider
- public static Object[][] resolutionAndStatusAndExpectedCount() {
- return new Object[][] {
- {STATUS_OPEN, null, 2},
- {STATUS_REOPENED, null, 2},
- {STATUS_CONFIRMED, null, 0},
- {STATUS_RESOLVED, null, 0},
- {STATUS_CLOSED, null, 0},
- {STATUS_RESOLVED, RESOLUTION_FIXED, 0},
- {STATUS_RESOLVED, RESOLUTION_FALSE_POSITIVE, 0},
- {STATUS_RESOLVED, RESOLUTION_WONT_FIX, 0},
- {STATUS_RESOLVED, RESOLUTION_REMOVED, 0},
- };
- }
-
private void addIssues(ComponentDto component, int bugs, int vulnerabilities, int codeSmelles) {
List<IssueDoc> issues = new ArrayList<>();
IntStream.range(0, bugs).forEach(b -> issues.add(newDoc(component).setType(BUG).setResolution(null)));