@Test public void do_not_return_module_key_on_single_module_projects() { ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1")); ComponentDto module = db.components().insertComponent(newModuleDto("M1", project).setDbKey("MK1")); ComponentDto file = db.components().insertComponent(newFileDto(module, null, "F1").setDbKey("FK1")); RuleDefinitionDto rule = db.rules().insert(r -> r.setRuleKey(RuleKey.of("xoo", "x1"))); db.issues().insert(rule, project, file, i -> i.setKee("ISSUE_IN_MODULE")); db.issues().insert(rule, project, project, i -> i.setKee("ISSUE_IN_ROOT_MODULE")); allowAnyoneOnProjects(project); indexIssues(); SearchWsResponse searchResponse = ws.newRequest().executeProtobuf(SearchWsResponse.class); assertThat(searchResponse.getIssuesCount()).isEqualTo(2); for (Issue issue : searchResponse.getIssuesList()) { assertThat(issue.getProject()).isEqualTo("PK1"); if (issue.getKey().equals("ISSUE_IN_MODULE")) { assertThat(issue.getSubProject()).isEqualTo("MK1"); } else if (issue.getKey().equals("ISSUE_IN_ROOT_MODULE")) { assertThat(issue.hasSubProject()).isFalse(); } } }
@Test public void check_projects_facet_max_size() { RuleDefinitionDto rule = db.rules().insert(); IntStream.rangeClosed(1, 110) .forEach(i -> { ComponentDto project = db.components().insertPublicProject(); db.issues().insert(rule, project, project); }); indexPermissions(); indexIssues(); SearchWsResponse response = ws.newRequest() .setParam(FACETS, "projects") .executeProtobuf(SearchWsResponse.class); assertThat(response.getPaging().getTotal()).isEqualTo(110); assertThat(response.getFacets().getFacets(0).getValuesCount()).isEqualTo(100); }
SearchWsResponse formatSearch(Set<SearchAdditionalField> fields, SearchResponseData data, Paging paging, Facets facets) { SearchWsResponse.Builder response = SearchWsResponse.newBuilder(); formatPaging(paging, response); formatEffortTotal(data, response); response.addAllIssues(formatIssues(fields, data)); response.addAllComponents(formatComponents(data)); formatFacets(data, facets, response); if (fields.contains(SearchAdditionalField.RULES)) { response.setRules(formatRules(data)); } if (fields.contains(SearchAdditionalField.USERS)) { response.setUsers(formatUsers(data)); } if (fields.contains(SearchAdditionalField.LANGUAGES)) { response.setLanguages(formatLanguages()); } return response.build(); }
.setParam("sansTop25", request.getSansTop25() == null ? null : request.getSansTop25().stream().collect(Collectors.joining(","))) .setParam("cwe", request.getCwe() == null ? null : request.getCwe().stream().collect(Collectors.joining(","))), SearchWsResponse.parser());
private List<Issue> toIssues(Issues.SearchWsResponse issuesSearchWsResponse, String branch) { List<Issues.Issue> issues = issuesSearchWsResponse.getIssuesList(); if (issues == null) { return Collections.emptyList(); } List<Issues.Component> components = issuesSearchWsResponse.getComponentsList(); Predicate<String> supported = ((Predicate<String>) Qualifiers.FILE::equals).or(Qualifiers.UNIT_TEST_FILE::equals); List<Issue> res = new ArrayList<>(); for (Issues.Issue issue : issues) { Optional<Issues.Component> componentOptional = components.stream() .filter(c -> supported.test(c.getQualifier())) .filter(c -> c.getKey().equals(issue.getComponent())) .findFirst(); File file = null; if (componentOptional.isPresent()) { Issues.Component component = componentOptional.get(); try { file = componentCache.get(component.getKey(), () -> toFile(componentOptional.get(), branch)); } catch (Exception e) { throw new IllegalStateException("Failed to get component file for " + component.getKey(), e); } } res.add(toIssue(issue, file)); } return res; }
public ImmutableList<Issue> getAllIssuesFor(String resourceKey) { final ImmutableList.Builder<Issue> builder = ImmutableList.builder(); SearchWsRequest query = new SearchWsRequest(); query.setComponentRoots(singletonList(resourceKey)) .setResolved(false) .setPageSize(-1); IssuesService issuesService = sonarClient.issues(); SearchWsResponse response = issuesService.search(query); builder.addAll(response.getIssuesList()); Common.Paging paging = response.getPaging(); Integer total = paging.getTotal(); Integer pageSize = paging.getPageSize(); Integer pages = total / pageSize + (total % pageSize > 0 ? 1 : 0); for (int pageIndex = 2; pageIndex <= pages; pageIndex++) { final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator(); if (progressIndicator.isCanceled()) break; final String pagesProgressMessage = String.format("%d / %d pages downloaded", pageIndex, pages); ProgressIndicatorUtil.setText(progressIndicator, pagesProgressMessage); ProgressIndicatorUtil.setFraction(progressIndicator, pageIndex * 1.0 / pages); query.setPage(pageIndex); builder.addAll(issuesService.search(query).getIssuesList()); } return builder.build(); } }
public SearchWsResponse formatSearch(Set<SearchAdditionalField> fields, SearchResponseData data, Paging paging, @Nullable Facets facets) { SearchWsResponse.Builder response = SearchWsResponse.newBuilder(); formatPaging(paging, response); formatEffortTotal(data, response); response.addAllIssues(formatIssues(fields, data)); response.addAllComponents(formatComponents(data)); if (facets != null) { formatFacets(facets, response); } if (fields.contains(SearchAdditionalField.RULE_IDS_AND_KEYS)) { response.setRules(formatRules(data)); } if (fields.contains(SearchAdditionalField.USERS)) { response.setUsers(formatUsers(data)); } if (fields.contains(SearchAdditionalField.LANGUAGES)) { response.setLanguages(formatLanguages()); } return response.build(); }
public Integer issueCount(String severity, String rule) { return wsClient.issues().search(new SearchWsRequest() .setSeverities(Arrays.asList(severity)) .setRules(Arrays.asList(rule))) .getIssuesCount(); }
.setParam("sansTop25", request.getSansTop25() == null ? null : request.getSansTop25().stream().collect(Collectors.joining(","))) .setParam("cwe", request.getCwe() == null ? null : request.getCwe().stream().collect(Collectors.joining(","))), SearchWsResponse.parser());
assertThat(result.getIssuesCount()).isEqualTo(1); assertThat(result.getIssues(0).getFlows(0).getLocationsList()).extracting(Issues.Location::getComponent, Issues.Location::getMsg) .containsExactlyInAnyOrder( tuple(file.getKey(), "FLOW MESSAGE"),
.executeProtobuf(SearchWsResponse.class); assertThat(response.getFacets().getFacetsList()) .extracting(Common.Facet::getProperty, facet -> facet.getValuesList().stream().collect(toMap(FacetValue::getVal, FacetValue::getCount))) .containsExactlyInAnyOrder(
.executeProtobuf(SearchWsResponse.class); assertThat(response.getIssuesList()).isEmpty();
.setParam(PARAM_COMPONENT_KEYS, applicationBranch1.getKey()) .setParam(PARAM_BRANCH, applicationBranch1.getBranch()) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getProject, Issue::getBranch, Issue::hasBranch) .containsExactlyInAnyOrder( .setParam(PARAM_PROJECTS, project1.getKey()) .setParam(PARAM_BRANCH, applicationBranch1.getBranch()) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch) .containsExactlyInAnyOrder(
.executeProtobuf(SearchWsResponse.class); assertThat(response.getIssuesList()) .extracting( Issue::getOrganization, Issue::getKey, Issue::getRule, Issue::getSeverity, Issue::getComponent, Issue::getResolution, Issue::getStatus, Issue::getMessage, Issue::getEffort,
.executeProtobuf(SearchWsResponse.class); assertThat(response.getFacets().getFacetsList()) .extracting(Common.Facet::getProperty, Common.Facet::getValuesCount) .containsExactlyInAnyOrder(
.setParam(PARAM_COMPONENT_KEYS, project.getKey()) .setParam(PARAM_BRANCH, branch.getBranch()) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch) .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchFile.getBranch())); .setParam(PARAM_PROJECT_KEYS, project.getKey()) .setParam(PARAM_BRANCH, branch.getBranch()) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch) .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchFile.getBranch())); .setParam(PARAM_COMPONENT_KEYS, branchFile.getKey()) .setParam(PARAM_BRANCH, branch.getBranch()) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch) .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchFile.getBranch()));
.executeProtobuf(SearchWsResponse.class); assertThat(response.getFacets().getFacetsList()) .extracting(Common.Facet::getProperty, facet -> facet.getValuesList().stream().collect(toMap(FacetValue::getVal, FacetValue::getCount))) .containsExactlyInAnyOrder(
.executeProtobuf(SearchWsResponse.class); assertThat(response.getIssuesList()) .extracting(Issue::getKey, i -> i.getComments().getCommentsList()) .containsExactlyInAnyOrder(tuple(issue.getKey(), Collections.emptyList()));
.executeProtobuf(SearchWsResponse.class); assertThat(response.getFacets().getFacetsList()) .extracting(Common.Facet::getProperty, facet -> facet.getValuesList().stream().collect(toMap(FacetValue::getVal, FacetValue::getCount))) .containsExactlyInAnyOrder(
@Test public void search_by_application_and_project_and_leak() { Date now = new Date(); RuleDefinitionDto rule = db.rules().insert(); ComponentDto application = db.components().insertApplication(db.getDefaultOrganization()); // Project 1 ComponentDto project1 = db.components().insertPublicProject(); db.components().insertSnapshot(project1, s -> s.setPeriodDate(addDays(now, -14).getTime())); db.components().insertComponents(newProjectCopy("PC1", project1, application)); IssueDto project1Issue1 = db.issues().insert(rule, project1, project1, i -> i.setIssueCreationDate(addDays(now, -10))); IssueDto project1Issue2 = db.issues().insert(rule, project1, project1, i -> i.setIssueCreationDate(addDays(now, -20))); // Project 2 ComponentDto project2 = db.components().insertPublicProject(); db.components().insertSnapshot(project2, s -> s.setPeriodDate(addDays(now, -25).getTime())); db.components().insertComponents(newProjectCopy("PC2", project2, application)); IssueDto project2Issue1 = db.issues().insert(rule, project2, project2, i -> i.setIssueCreationDate(addDays(now, -15))); IssueDto project2Issue2 = db.issues().insert(rule, project2, project2, i -> i.setIssueCreationDate(addDays(now, -30))); // Permissions and index allowAnyoneOnProjects(project1, project2, application); indexIssuesAndViews(); SearchWsResponse result = ws.newRequest() .setParam(PARAM_COMPONENT_KEYS, application.getDbKey()) .setParam(PARAM_PROJECT_KEYS, project1.getDbKey()) .setParam(PARAM_SINCE_LEAK_PERIOD, "true") .executeProtobuf(SearchWsResponse.class); assertThat(result.getIssuesList()).extracting(Issue::getKey) .containsExactlyInAnyOrder(project1Issue1.getKey()) .doesNotContain(project1Issue2.getKey(), project2Issue1.getKey(), project2Issue2.getKey()); }