public Map<ShortBranchIssue, DefaultIssue> loadDefaultIssuesWithChanges(Collection<ShortBranchIssue> lightIssues) { if (lightIssues.isEmpty()) { return Collections.emptyMap(); } Map<String, ShortBranchIssue> issuesByKey = lightIssues.stream().collect(Collectors.toMap(ShortBranchIssue::getKey, i -> i)); try (DbSession session = dbClient.openSession(false)) { List<DefaultIssue> issues = dbClient.issueDao().selectByKeys(session, issuesByKey.keySet()) .stream() .map(IssueDto::toDefaultIssue) .collect(toList(issuesByKey.size())); componentIssuesLoader.loadChanges(session, issues); return issues.stream() .collect(uniqueIndex(i -> issuesByKey.get(i.key()), i -> i, issues.size())); } }
private IssueDto doInsert(DbSession session, long now, DefaultIssue issue) { ComponentDto component = component(session, issue); ComponentDto project = project(session, issue); int ruleId = requireNonNull(getRuleId(issue), "Rule not found: " + issue.ruleKey()); IssueDto dto = IssueDto.toDtoForServerInsert(issue, component, project, ruleId, now); getDbClient().issueDao().insert(session, dto); return dto; }
public IssueDto selectOrFailByKey(DbSession session, String key) { Optional<IssueDto> issue = selectByKey(session, key); if (!issue.isPresent()) { throw new RowNotFoundException(String.format("Issue with key '%s' does not exist", key)); } return issue.get(); }
switch (component.scope()) { case Scopes.PROJECT: issueDtos.addAll(dbClient.issueDao().selectNonClosedByModuleOrProjectExcludingExternalsAndSecurityHotspots(dbSession, component)); break; case Scopes.FILE: issueDtos.addAll(dbClient.issueDao().selectNonClosedByComponentUuidExcludingExternalsAndSecurityHotspots(dbSession, component.uuid())); break; default:
/** * Gets a list issues by their keys. The result does NOT contain {@code null} values for issues not found, so * the size of result may be less than the number of keys. A single issue is returned * if input keys contain multiple occurrences of a key. * <p>Results may be in a different order as input keys.</p> */ public List<IssueDto> selectByKeys(DbSession session, Collection<String> keys) { return executeLargeInputs(keys, mapper(session)::selectByKeys); }
public Collection<ShortBranchIssue> loadCandidateIssuesForMergingInTargetBranch(Component component) { String componentKey = ComponentDto.removeBranchAndPullRequestFromKey(component.getDbKey()); Set<String> uuids = shortBranchComponentsWithIssues.getUuids(componentKey); if (uuids.isEmpty()) { return Collections.emptyList(); } try (DbSession session = dbClient.openSession(false)) { return dbClient.issueDao().selectOpenByComponentUuids(session, uuids) .stream() .map(ShortBranchIssuesLoader::toShortBranchIssue) .collect(Collectors.toList()); } }
/** * Uuids of all the components that have open issues on this project. */ public Set<String> loadUuidsOfComponentsWithOpenIssues() { try (DbSession dbSession = dbClient.openSession(false)) { return dbClient.issueDao().selectComponentUuidsOfOpenIssuesForProjectUuid(dbSession, treeRootHolder.getRoot().getUuid()); } } }
FormulaContextImpl context = new FormulaContextImpl(matrix, debtRatingGrid); components.forEach(c -> { IssueCounter issueCounter = new IssueCounter(dbClient.issueDao().selectIssueGroupsByBaseComponent(dbSession, c, beginningOfLeakPeriod.orElse(Long.MAX_VALUE))); for (IssueMetricFormula formula : formulaFactory.getFormulas()) {
@Test public void scrollNonClosedByComponentUuid() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); IssueDto openIssue1OnFile = db.issues().insert(rule, project, file, i -> i.setStatus("OPEN").setResolution(null).setType(randomRuleTypeExceptHotspot())); IssueDto openIssue2OnFile = db.issues().insert(rule, project, file, i -> i.setStatus("OPEN").setResolution(null).setType(randomRuleTypeExceptHotspot())); IssueDto closedIssueOnFile = db.issues().insert(rule, project, file, i -> i.setStatus("CLOSED").setResolution("FIXED").setType(randomRuleTypeExceptHotspot())); IssueDto openIssueOnProject = db.issues().insert(rule, project, project, i -> i.setStatus("OPEN").setResolution(null).setType(randomRuleTypeExceptHotspot())); IssueDto securityHotspot = db.issues().insert(rule, project, file, i -> i.setType(RuleType.SECURITY_HOTSPOT)); IssueDto manualVulnerability = db.issues().insert(rule, project, file, i -> i.setType(RuleType.VULNERABILITY).setIsFromHotspot(true)); RuleDefinitionDto external = db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto.setIsExternal(true)); IssueDto issueFromExteralruleOnFile = db.issues().insert(external, project, file, i -> i.setKee("ON_FILE_FROM_EXTERNAL").setType(randomRuleTypeExceptHotspot())); assertThat(underTest.selectNonClosedByComponentUuidExcludingExternalsAndSecurityHotspots(db.getSession(), file.uuid())) .extracting(IssueDto::getKey) .containsExactlyInAnyOrder(Arrays.stream(new IssueDto[] {openIssue1OnFile, openIssue2OnFile}).map(IssueDto::getKey).toArray(String[]::new)); assertThat(underTest.selectNonClosedByComponentUuidExcludingExternalsAndSecurityHotspots(db.getSession(), project.uuid())) .extracting(IssueDto::getKey) .containsExactlyInAnyOrder(Arrays.stream(new IssueDto[] {openIssueOnProject}).map(IssueDto::getKey).toArray(String[]::new)); assertThat(underTest.selectNonClosedByComponentUuidExcludingExternalsAndSecurityHotspots(db.getSession(), "does_not_exist")).isEmpty(); }
public Collection<IssueGroupDto> selectIssueGroupsByBaseComponent(DbSession dbSession, ComponentDto baseComponent, long leakPeriodBeginningDate) { return mapper(dbSession).selectIssueGroupsByBaseComponent(baseComponent, leakPeriodBeginningDate); }
@Test public void selectOpenByComponentUuid_should_correctly_map_required_fields() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertMainBranch(); ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo") .setBranchType(BranchType.SHORT)); ComponentDto file = db.components().insertComponent(newFileDto(projectBranch)); IssueDto fpIssue = db.issues().insert(rule, projectBranch, file, i -> i.setStatus("RESOLVED").setResolution("FALSE-POSITIVE")); ShortBranchIssueDto fp = underTest.selectOpenByComponentUuids(db.getSession(), Collections.singletonList(file.uuid())).get(0); assertThat(fp.getLine()).isEqualTo(fpIssue.getLine()); assertThat(fp.getMessage()).isEqualTo(fpIssue.getMessage()); assertThat(fp.getChecksum()).isEqualTo(fpIssue.getChecksum()); assertThat(fp.getRuleKey()).isEqualTo(fpIssue.getRuleKey()); assertThat(fp.getStatus()).isEqualTo(fpIssue.getStatus()); assertThat(fp.getLine()).isNotNull(); assertThat(fp.getLine()).isNotZero(); assertThat(fp.getMessage()).isNotNull(); assertThat(fp.getChecksum()).isNotNull(); assertThat(fp.getChecksum()).isNotEmpty(); assertThat(fp.getRuleKey()).isNotNull(); assertThat(fp.getStatus()).isNotNull(); assertThat(fp.getBranchName()).isEqualTo("feature/foo"); assertThat(fp.getIssueCreationDate()).isNotNull(); }
/** * Uuids of all the components that have open issues on this project. */ public Set<String> loadUuidsOfComponentsWithOpenIssues() { try (DbSession dbSession = dbClient.openSession(false)) { return dbClient.issueDao().selectComponentUuidsOfOpenIssuesForProjectUuid(dbSession, treeRootHolder.getRoot().getUuid()); } } }
FormulaContextImpl context = new FormulaContextImpl(matrix, debtRatingGrid); components.forEach(c -> { IssueCounter issueCounter = new IssueCounter(dbClient.issueDao().selectIssueGroupsByBaseComponent(dbSession, c, beginningOfLeakPeriod.orElse(Long.MAX_VALUE))); for (IssueMetricFormula formula : formulaFactory.getFormulas()) {
public IssueDto insertIssue(IssueDto issueDto) { db.getDbClient().issueDao().insert(db.getSession(), issueDto); db.commit(); return issueDto; }
public IssueDto getByKey(DbSession session, String issueKey) { IssueDto issue = dbClient.issueDao().selectByKey(session, issueKey).orElseThrow(() -> new NotFoundException(format("Issue with key '%s' does not exist", issueKey))); userSession.checkComponentUuidPermission(UserRole.USER, requireNonNull(issue.getProjectUuid())); return issue; }
private List<IssueDto> loadIssues(SearchResponseData preloadedResponseData, Collector collector, DbSession dbSession) { List<IssueDto> preloadedIssues = preloadedResponseData.getIssues(); Set<String> preloadedIssueKeys = preloadedIssues.stream().map(IssueDto::getKey).collect(MoreCollectors.toSet(preloadedIssues.size())); ImmutableSet<String> issueKeys = ImmutableSet.copyOf(collector.getIssueKeys()); Set<String> issueKeysToLoad = copyOf(difference(issueKeys, preloadedIssueKeys)); if (issueKeysToLoad.isEmpty()) { return issueKeys.stream() .map(new KeyToIssueFunction(preloadedIssues)::apply).filter(Objects::nonNull) .collect(Collectors.toList()); } List<IssueDto> loadedIssues = dbClient.issueDao().selectByKeys(dbSession, issueKeysToLoad); List<IssueDto> unorderedIssues = concat(preloadedIssues.stream(), loadedIssues.stream()) .collect(toList(preloadedIssues.size() + loadedIssues.size())); return issueKeys.stream() .map(new KeyToIssueFunction(unorderedIssues)::apply).filter(Objects::nonNull) .collect(Collectors.toList()); }
public void update(DbSession session, IssueDto dto) { mapper(session).update(dto); }
@Test public void selectOpenByComponentUuid() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertMainBranch(); ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo") .setBranchType(BranchType.SHORT)); ComponentDto file = db.components().insertComponent(newFileDto(projectBranch)); IssueDto openIssue = db.issues().insert(rule, projectBranch, file, i -> i.setStatus(Issue.STATUS_OPEN).setResolution(null)); IssueDto closedIssue = db.issues().insert(rule, projectBranch, file, i -> i.setStatus(Issue.STATUS_CLOSED).setResolution(Issue.RESOLUTION_FIXED)); IssueDto reopenedIssue = db.issues().insert(rule, projectBranch, file, i -> i.setStatus(Issue.STATUS_REOPENED).setResolution(null)); IssueDto confirmedIssue = db.issues().insert(rule, projectBranch, file, i -> i.setStatus(Issue.STATUS_CONFIRMED).setResolution(null)); IssueDto wontfixIssue = db.issues().insert(rule, projectBranch, file, i -> i.setStatus(Issue.STATUS_RESOLVED).setResolution(Issue.RESOLUTION_WONT_FIX)); IssueDto fpIssue = db.issues().insert(rule, projectBranch, file, i -> i.setStatus(Issue.STATUS_RESOLVED).setResolution(Issue.RESOLUTION_FALSE_POSITIVE)); assertThat(underTest.selectOpenByComponentUuids(db.getSession(), Collections.singletonList(file.uuid()))) .extracting("kee") .containsOnly(openIssue.getKey(), reopenedIssue.getKey(), confirmedIssue.getKey(), wontfixIssue.getKey(), fpIssue.getKey()); }
@Test public void sort_by_updated_at() { RuleDto rule = newRule(); OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org-2")); ComponentDto project = db.components().insertComponent(ComponentTesting.newPublicProjectDto(organization, "PROJECT_ID").setDbKey("PROJECT_KEY")); indexPermissions(); ComponentDto file = db.components().insertComponent(newFileDto(project, null, "FILE_ID").setDbKey("FILE_KEY")); dbClient.issueDao().insert(session, newDto(rule, file, project) .setKee("82fd47d4-b650-4037-80bc-7b112bd4eac1") .setIssueUpdateDate(parseDateTime("2014-11-02T00:00:00+0100"))); dbClient.issueDao().insert(session, newDto(rule, file, project) .setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2") .setIssueUpdateDate(parseDateTime("2014-11-01T00:00:00+0100"))); dbClient.issueDao().insert(session, newDto(rule, file, project) .setKee("82fd47d4-b650-4037-80bc-7b112bd4eac3") .setIssueUpdateDate(parseDateTime("2014-11-03T00:00:00+0100"))); session.commit(); indexIssues(); TestResponse response = ws.newRequest() .setParam("sort", IssueQuery.SORT_BY_UPDATE_DATE) .setParam("asc", "false") .execute(); JsonElement parse = new JsonParser().parse(response.getInput()); assertThat(parse.getAsJsonObject().get("issues").getAsJsonArray()) .extracting(o -> o.getAsJsonObject().get("key").getAsString()) .containsExactly("82fd47d4-b650-4037-80bc-7b112bd4eac3", "82fd47d4-b650-4037-80bc-7b112bd4eac1", "82fd47d4-b650-4037-80bc-7b112bd4eac2"); }
private void checkIssueAssignee(String issueKey, @Nullable String expectedAssignee) { IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueKey).get(); assertThat(issueReloaded.getAssigneeUuid()).isEqualTo(expectedAssignee); assertThat(issueReloaded.getIssueUpdateTime()).isEqualTo(NOW); assertThat(issueReloaded.getUpdatedAt()).isEqualTo(NOW); } }