private void formatIssue(Issue.Builder issueBuilder, IssueDto dto, SearchResponseData data) { issueBuilder.setKey(dto.getKey()); ofNullable(dto.getType()).map(Common.RuleType::forNumber).ifPresent(issueBuilder::setType); ComponentDto component = data.getComponentByUuid(dto.getComponentUuid()); issueBuilder.setOrganization(data.getOrganizationKey(component.getOrganizationUuid())); issueBuilder.setComponent(component.getKey()); ofNullable(component.getBranch()).ifPresent(issueBuilder::setBranch); ofNullable(component.getPullRequest()).ifPresent(issueBuilder::setPullRequest); ComponentDto project = data.getComponentByUuid(dto.getProjectUuid()); if (project != null) { issueBuilder.setProject(project.getKey()); ComponentDto subProject = data.getComponentByUuid(dto.getModuleUuid()); if (subProject != null && !subProject.getDbKey().equals(project.getDbKey())) { issueBuilder.setSubProject(subProject.getKey()); issueBuilder.setRule(dto.getRuleKey().toString()); if (dto.isExternal()) { issueBuilder.setExternalRuleEngine(engineNameFrom(dto.getRuleKey())); issueBuilder.setFromHotspot(dto.isFromHotspot()); issueBuilder.setSeverity(Common.Severity.valueOf(dto.getSeverity())); ofNullable(data.getUserByUuid(dto.getAssigneeUuid())).ifPresent(assignee -> issueBuilder.setAssignee(assignee.getLogin())); ofNullable(emptyToNull(dto.getResolution())).ifPresent(issueBuilder::setResolution); issueBuilder.setStatus(dto.getStatus()); issueBuilder.setMessage(nullToEmpty(dto.getMessage())); issueBuilder.addAllTags(dto.getTags()); Long effort = dto.getEffort(); if (effort != null) { String effortValue = durations.encode(Duration.create(effort));
@Test public void should_reload_issue_and_resolve_conflict() { DefaultIssue issue = new DefaultIssue() .setKey("ABCDE") .setType(CODE_SMELL) .setRuleKey(RuleKey.of("squid", "AvoidCycles")) .setProjectUuid("U1") .setComponentUuid("U2") .setNew(false) .setStatus(STATUS_OPEN); // Issue as seen and changed by end-user IssueMapper mapper = mock(IssueMapper.class); IssueDto issueDto = new IssueDto() .setKee("ABCDE") .setType(CODE_SMELL) .setRuleId(10) .setRuleKey("squid", "AvoidCycles") .setProjectUuid("U1") .setComponentUuid("U2") .setLine(10) .setStatus(STATUS_OPEN) // field changed by user .setAssigneeUuid("arthur-uuid"); new UpdateConflictResolver().resolve(issue, issueDto, mapper); ArgumentCaptor<IssueDto> argument = ArgumentCaptor.forClass(IssueDto.class); verify(mapper).update(argument.capture()); IssueDto updatedIssue = argument.getValue(); assertThat(updatedIssue.getKee()).isEqualTo("ABCDE"); assertThat(updatedIssue.getAssigneeUuid()).isEqualTo("arthur-uuid"); }
@Test public void support_only_issues_with_issue_admin_permission() { IssueDto authorizedIssueDto = newIssue().setType(BUG); DefaultIssue authorizedIssue = authorizedIssueDto.toDefaultIssue(); setUserWithBrowseAndAdministerIssuePermission(authorizedIssueDto); DefaultIssue unauthorizedIssue = newIssue().setType(BUG).toDefaultIssue(); assertThat(action.supports(authorizedIssue.setResolution(null))).isTrue(); assertThat(action.supports(unauthorizedIssue.setResolution(null))).isFalse(); }
void collect(List<IssueDto> issues) { for (IssueDto issue : issues) { componentUuids.add(issue.getComponentUuid()); projectUuids.add(issue.getProjectUuid()); ruleIds.add(issue.getRuleId()); String issueAssigneeUuid = issue.getAssigneeUuid(); if (issueAssigneeUuid != null) { userUuids.add(issueAssigneeUuid); } collectComponentsFromIssueLocations(issue); } }
private boolean isMember(DbSession dbSession, IssueDto issue) { Optional<ComponentDto> project = dbClient.componentDao().selectByUuid(dbSession, issue.getProjectUuid()); checkState(project.isPresent(), "Cannot find the project with uuid %s from issue.id %s", issue.getProjectUuid(), issue.getId()); Optional<OrganizationDto> organization = dbClient.organizationDao().selectByUuid(dbSession, project.get().getOrganizationUuid()); checkState(organization.isPresent(), "Cannot find the organization with uuid %s from issue.id %s", project.get().getOrganizationUuid(), issue.getId()); return userSession.hasMembership(organization.get()); }
private IssueDto newIssue(String assignee) { IssueDto issue = db.issues().insertIssue( issueDto -> issueDto .setAssigneeUuid(assignee) .setCreatedAt(PAST).setIssueCreationTime(PAST) .setUpdatedAt(PAST).setIssueUpdateTime(PAST) .setType(RuleType.CODE_SMELL)); return issue; }
private static void handleIssue(IssueDto issue, ScannerInput.ServerIssue.Builder issueBuilder, Map<String, String> keysByUUid, OutputStream out) { issueBuilder.setKey(issue.getKey()); String moduleUuid = extractModuleUuid(issue); issueBuilder.setModuleKey(keysByUUid.get(moduleUuid)); ofNullable(issue.getFilePath()).ifPresent(issueBuilder::setPath); issueBuilder.setRuleRepository(issue.getRuleRepo()); issueBuilder.setRuleKey(issue.getRule()); ofNullable(issue.getChecksum()).ifPresent(issueBuilder::setChecksum); ofNullable(issue.getAssigneeUuid()).ifPresent(issueBuilder::setAssigneeLogin); ofNullable(issue.getLine()).ifPresent(issueBuilder::setLine); ofNullable(issue.getMessage()).ifPresent(issueBuilder::setMsg); issueBuilder.setSeverity(org.sonar.scanner.protocol.Constants.Severity.valueOf(issue.getSeverity())); issueBuilder.setManualSeverity(issue.isManualSeverity()); issueBuilder.setStatus(issue.getStatus()); ofNullable(issue.getResolution()).ifPresent(issueBuilder::setResolution); issueBuilder.setType(RuleType.valueOf(issue.getType()).name()); issueBuilder.setCreationDate(issue.getIssueCreationTime()); try { issueBuilder.build().writeDelimitedTo(out); } catch (IOException e) { throw new IllegalStateException("Unable to serialize issue", e); } issueBuilder.clear(); }
assertThat(result.getKey()).isEqualTo("ISSUE"); assertThat(result.getRuleKey()).isEqualTo(rule.getKey()); assertThat(result.getComponentUuid()).isEqualTo(file.uuid()); assertThat(result.getProjectUuid()).isEqualTo(project.uuid()); assertThat(result.getSeverity()).isEqualTo(BLOCKER); assertThat(result.getStatus()).isEqualTo(STATUS_OPEN); assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant()); assertThat(context.getStatistics().getAll()).containsOnly( entry("inserts", "1"), entry("updates", "0"), entry("merged", "0"), entry("untouched", "0"));
private void verifyContentOfPreloadedSearchResponseData(IssueDto issue) { SearchResponseData preloadedSearchResponseData = preloadedSearchResponseDataCaptor.getValue(); assertThat(preloadedSearchResponseData.getIssues()) .extracting(IssueDto::getKey) .containsOnly(issue.getKey()); assertThat(preloadedSearchResponseData.getRules()) .extracting(RuleDefinitionDto::getKey) .containsOnly(issue.getRuleKey()); assertThat(preloadedSearchResponseData.getComponents()) .extracting(ComponentDto::uuid) .containsOnly(issue.getComponentUuid(), issue.getProjectUuid()); }
@Test public void prefer_resolved_issues() { db.issues().insertIssue(IssueTesting.newIssue(rule, branch1Dto, fileOnBranch1Dto).setKee("issue1").setStatus(Issue.STATUS_REOPENED).setLine(1).setChecksum("checksum")); db.issues().insertIssue(IssueTesting.newIssue(rule, branch2Dto, fileOnBranch2Dto).setKee("issue2").setStatus(Issue.STATUS_CONFIRMED).setLine(1).setChecksum("checksum")); db.issues().insertIssue(IssueTesting.newIssue(rule, branch3Dto, fileOnBranch3Dto).setKee("issue3").setStatus(Issue.STATUS_RESOLVED) .setResolution(Issue.RESOLUTION_FALSE_POSITIVE).setLine(1).setChecksum("checksum")); DefaultIssue newIssue = createIssue("newIssue", rule.getKey(), Issue.STATUS_OPEN, null, new Date()); copier.tryMerge(FILE_1, Collections.singleton(newIssue)); ArgumentCaptor<DefaultIssue> issueToMerge = ArgumentCaptor.forClass(DefaultIssue.class); verify(issueLifecycle).mergeConfirmedOrResolvedFromShortLivingBranch(eq(newIssue), issueToMerge.capture(), eq("myBranch3")); assertThat(issueToMerge.getValue().key()).isEqualTo("issue3"); }
@Test public void set_tags() { IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag"))); logIn(issueDto); call(issueDto.getKey(), "bug", "todo"); verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class)); verifyContentOfPreloadedSearchResponseData(issueDto); IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueDto.getKey()).get(); assertThat(issueReloaded.getTags()).containsOnly("bug", "todo"); assertThat(issueChangePostProcessor.wasCalled()).isFalse(); }
@SafeVarargs private final IssueDto insertNewClosedIssue(ComponentDto component, RuleDefinitionDto rule, long issueCloseTime, Consumer<IssueDto>... consumers) { IssueDto res = new IssueDto() .setKee(UuidFactoryFast.getInstance().create()) .setRuleId(rule.getId()) .setType(rule.getType()) .setComponentUuid(component.uuid()) .setProjectUuid(component.projectUuid()) .setStatus(Issue.STATUS_CLOSED) .setIssueCloseTime(issueCloseTime); Arrays.asList(consumers).forEach(c -> c.accept(res)); underTest.insert(res); dbSession.commit(); return res; }
@Test public void set_rule() { IssueDto dto = new IssueDto() .setKee("100") .setRule(new RuleDefinitionDto().setId(1).setRuleKey("AvoidCycle").setRepositoryKey("squid").setIsExternal(true)) .setLanguage("xoo"); assertThat(dto.getRuleId()).isEqualTo(1); assertThat(dto.getRuleRepo()).isEqualTo("squid"); assertThat(dto.getRule()).isEqualTo("AvoidCycle"); assertThat(dto.getRuleKey().toString()).isEqualTo("squid:AvoidCycle"); assertThat(dto.getLanguage()).isEqualTo("xoo"); assertThat(dto.isExternal()).isEqualTo(true); }
@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(); }
assertThat(doc.getId()).isEqualTo(issue.getKey()); assertThat(doc.organizationUuid()).isEqualTo(organization.getUuid()); assertThat(doc.assigneeUuid()).isEqualTo(issue.getAssigneeUuid()); assertThat(doc.authorLogin()).isEqualTo(issue.getAuthorLogin()); assertThat(doc.componentUuid()).isEqualTo(file.uuid()); assertThat(doc.projectUuid()).isEqualTo(project.uuid()); assertThat(doc.branchUuid()).isEqualTo(project.uuid()); assertThat(doc.isMainBranch()).isTrue(); assertThat(doc.closeDate()).isEqualTo(issue.getIssueCloseDate()); assertThat(doc.creationDate()).isEqualToIgnoringMillis(issue.getIssueCreationDate()); assertThat(doc.directoryPath()).isEqualTo(dir.path()); assertThat(doc.filePath()).isEqualTo(file.path()); assertThat(doc.getParent()).isEqualTo(project.uuid()); assertThat(doc.getRouting()).isEqualTo(project.uuid()); assertThat(doc.language()).isEqualTo(issue.getLanguage()); assertThat(doc.line()).isEqualTo(issue.getLine()); assertThat(doc.updateDate()).isEqualToIgnoringMillis(new Date(issue.getIssueUpdateTime())); assertThat(doc.getCwe()).containsExactlyInAnyOrder(UNKNOWN_STANDARD); assertThat(doc.getOwaspTop10()).containsExactlyInAnyOrder(UNKNOWN_STANDARD);
@Test public void commitAndIndexIssues_removes_issue_from_index_if_it_does_not_exist_in_db() { IssueDto issue1 = new IssueDto().setKee("I1").setProjectUuid("P1"); addIssueToIndex(issue1.getProjectUuid(), issue1.getKey()); IssueDto issue2 = db.issues().insertIssue(organization); underTest.commitAndIndexIssues(db.getSession(), asList(issue1, issue2)); // issue1 is removed from index, issue2 is persisted and indexed assertThatIndexHasOnly(issue2); assertThatDbHasOnly(issue2); assertThatEsQueueTableHasSize(0); }
@Test public void remove_assignee() { UserDto user = db.users().insertUser(); userSession.logIn(user); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); addUserProjectPermissions(user, project, USER, ISSUE_ADMIN); RuleDefinitionDto rule = db.rules().insert(); UserDto assignee = db.users().insertUser(); IssueDto issue = db.issues().insert(rule, project, file, i -> i.setAssigneeUuid(assignee.getUuid()) .setStatus(STATUS_OPEN).setResolution(null)); BulkChangeWsResponse response = call(builder() .setIssues(singletonList(issue.getKey())) .setAssign("") .build()); checkResponse(response, 1, 1, 0, 0); IssueDto reloaded = getIssueByKeys(issue.getKey()).get(0); assertThat(reloaded.getAssigneeUuid()).isNull(); assertThat(reloaded.getUpdatedAt()).isEqualTo(NOW); // no need to refresh measures verifyPostProcessorNotCalled(); }
@Test public void assign_to_me_using_deprecated_me_param() { IssueDto issue = newIssueWithBrowsePermission(); ws.newRequest() .setParam("issue", issue.getKey()) .setParam("me", "true") .execute(); checkIssueAssignee(issue.getKey(), CURRENT_USER_UUID); Optional<IssueDto> optionalIssueDto = dbClient.issueDao().selectByKey(session, issue.getKey()); assertThat(optionalIssueDto).isPresent(); assertThat(optionalIssueDto.get().getAssigneeUuid()).isEqualTo(CURRENT_USER_UUID); }
@Test public void verify_no_notification_on_hotspot() { UserDto assignee = db.users().insertUser(); RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(RuleType.SECURITY_HOTSPOT)) .setSeverity(MAJOR) .setAssigneeUuid(assignee.getUuid()) .toDefaultIssue(); UserDto changeAuthor = db.users().insertUser(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid()); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, "increase severity"); verify(notificationManager, never()).scheduleForSending(any()); }
@Test public void add_tags() { UserDto user = db.users().insertUser(); userSession.logIn(user); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); addUserProjectPermissions(user, project, USER, ISSUE_ADMIN); RuleDefinitionDto rule = db.rules().insert(); IssueDto issue = db.issues().insert(rule, project, file, i -> i.setTags(asList("tag1", "tag2")) .setStatus(STATUS_OPEN).setResolution(null)); BulkChangeWsResponse response = call(builder() .setIssues(singletonList(issue.getKey())) .setAddTags(singletonList("tag3")) .build()); checkResponse(response, 1, 1, 0, 0); IssueDto reloaded = getIssueByKeys(issue.getKey()).get(0); assertThat(reloaded.getTags()).containsOnly("tag1", "tag2", "tag3"); assertThat(reloaded.getUpdatedAt()).isEqualTo(NOW); // no need to refresh measures verifyPostProcessorNotCalled(); }