private SearchResponseData doTransition(DbSession session, IssueDto issueDto, String transitionKey) { DefaultIssue defaultIssue = issueDto.toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(system2.now()), userSession.getUuid()); transitionService.checkTransitionPermission(transitionKey, defaultIssue); if (transitionService.doTransition(defaultIssue, context, transitionKey)) { return issueUpdater.saveIssueAndPreloadSearchResponseData(session, defaultIssue, context, null, true); } return new SearchResponseData(issueDto); } }
public IssueDto saveIssue(DbSession session, DefaultIssue issue, IssueChangeContext context, @Nullable String comment) { Optional<RuleDefinitionDto> rule = getRuleByKey(session, issue.getRuleKey()); ComponentDto project = getComponent(session, issue, issue.projectUuid()); ComponentDto component = getComponent(session, issue, issue.componentUuid()); return doSaveIssue(session, issue, context, comment, rule, project, component); }
@Test public void update_issue() { DefaultIssue issue = db.issues().insertIssue(i -> i.setSeverity(MAJOR)).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, null); IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issue.key()).get(); assertThat(issueReloaded.getSeverity()).isEqualTo(BLOCKER); }
@Test public void verify_notification() { UserDto assignee = db.users().insertUser(); RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(randomTypeExceptHotspot)) .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).scheduleForSending(notificationArgumentCaptor.capture()); IssueChangeNotification issueChangeNotification = notificationArgumentCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key()); assertThat(issueChangeNotification.getFieldValue("old.severity")).isEqualTo(MAJOR); assertThat(issueChangeNotification.getFieldValue("new.severity")).isEqualTo(BLOCKER); assertThat(issueChangeNotification.getFieldValue("componentKey")).isEqualTo(file.getDbKey()); assertThat(issueChangeNotification.getFieldValue("componentName")).isEqualTo(file.longName()); assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(project.getDbKey()); assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.name()); assertThat(issueChangeNotification.getFieldValue("ruleName")).isEqualTo(rule.getName()); assertThat(issueChangeNotification.getFieldValue("changeAuthor")).isEqualTo(changeAuthor.getLogin()); assertThat(issueChangeNotification.getFieldValue("comment")).isEqualTo("increase severity"); assertThat(issueChangeNotification.getFieldValue("assignee")).isEqualTo(assignee.getLogin()); }
/** * Same as {@link #saveIssue(DbSession, DefaultIssue, IssueChangeContext, String)} but populates the specified * {@link SearchResponseData} with the DTOs (rule and components) retrieved from DB to save the issue. */ public SearchResponseData saveIssueAndPreloadSearchResponseData(DbSession dbSession, DefaultIssue issue, IssueChangeContext context, @Nullable String comment, boolean refreshMeasures) { Optional<RuleDefinitionDto> rule = getRuleByKey(dbSession, issue.getRuleKey()); ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, issue.projectUuid()); ComponentDto component = getComponent(dbSession, issue, issue.componentUuid()); IssueDto issueDto = doSaveIssue(dbSession, issue, context, comment, rule, project, component); SearchResponseData result = new SearchResponseData(issueDto); rule.ifPresent(r -> result.addRules(singletonList(r))); result.addComponents(singleton(project)); result.addComponents(singleton(component)); if (refreshMeasures) { List<DefaultIssue> changedIssues = result.getIssues().stream().map(IssueDto::toDefaultIssue).collect(MoreCollectors.toList(result.getIssues().size())); issueChangePostProcessor.process(dbSession, changedIssues, singleton(component)); } return result; }
private SearchResponseData setTags(String issueKey, List<String> tags) { userSession.checkLoggedIn(); try (DbSession session = dbClient.openSession(false)) { IssueDto issueDto = issueFinder.getByKey(session, issueKey); DefaultIssue issue = issueDto.toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.getUuid()); if (issueFieldsSetter.setTags(issue, tags, context)) { return issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context, null, false); } return new SearchResponseData(issueDto); } }
@Test public void verify_notification_on_branch() { RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file = db.components().insertComponent(newFileDto(branch)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), branch, file) .setType(randomTypeExceptHotspot)).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, "increase severity"); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); IssueChangeNotification issueChangeNotification = notificationArgumentCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key()); assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(project.getDbKey()); assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.name()); assertThat(issueChangeNotification.getFieldValue("branch")).isEqualTo(branch.getBranch()); }
public IssueDto saveIssue(DbSession session, DefaultIssue issue, IssueChangeContext context, @Nullable String comment) { Optional<RuleDefinitionDto> rule = getRuleByKey(session, issue.getRuleKey()); ComponentDto project = getComponent(session, issue, issue.projectUuid()); ComponentDto component = getComponent(session, issue, issue.componentUuid()); return doSaveIssue(session, issue, context, comment, rule, project, component); }
private SearchResponseData assign(String issueKey, @Nullable String login) { try (DbSession dbSession = dbClient.openSession(false)) { IssueDto issueDto = issueFinder.getByKey(dbSession, issueKey); DefaultIssue issue = issueDto.toDefaultIssue(); checkArgument(issue.type() != RuleType.SECURITY_HOTSPOT,"It is not allowed to assign a security hotspot"); UserDto user = getUser(dbSession, login); if (user != null) { checkMembership(dbSession, issueDto, user); } IssueChangeContext context = IssueChangeContext.createUser(new Date(system2.now()), userSession.getUuid()); if (issueFieldsSetter.assign(issue, user, context)) { return issueUpdater.saveIssueAndPreloadSearchResponseData(dbSession, issue, context, null, false); } return new SearchResponseData(issueDto); } }
@Test public void verify_notification_when_issue_is_linked_on_removed_rule() { RuleDto rule = db.rules().insertRule(r -> r.setStatus(RuleStatus.REMOVED)); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(randomTypeExceptHotspot)).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, null); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); assertThat(notificationArgumentCaptor.getValue().getFieldValue("ruleName")).isNull(); }
/** * Same as {@link #saveIssue(DbSession, DefaultIssue, IssueChangeContext, String)} but populates the specified * {@link SearchResponseData} with the DTOs (rule and components) retrieved from DB to save the issue. */ public SearchResponseData saveIssueAndPreloadSearchResponseData(DbSession dbSession, DefaultIssue issue, IssueChangeContext context, @Nullable String comment, boolean refreshMeasures) { Optional<RuleDefinitionDto> rule = getRuleByKey(dbSession, issue.getRuleKey()); ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, issue.projectUuid()); ComponentDto component = getComponent(dbSession, issue, issue.componentUuid()); IssueDto issueDto = doSaveIssue(dbSession, issue, context, comment, rule, project, component); SearchResponseData result = new SearchResponseData(issueDto); rule.ifPresent(r -> result.setRules(singletonList(r))); result.addComponents(singleton(project)); result.addComponents(singleton(component)); if (refreshMeasures) { List<DefaultIssue> changedIssues = result.getIssues().stream().map(IssueDto::toDefaultIssue).collect(MoreCollectors.toList(result.getIssues().size())); issueChangePostProcessor.process(dbSession, changedIssues, singleton(component)); } return result; }
private SearchResponseData setType(DbSession session, String issueKey, String severity) { IssueDto issueDto = issueFinder.getByKey(session, issueKey); DefaultIssue issue = issueDto.toDefaultIssue(); userSession.checkComponentUuidPermission(ISSUE_ADMIN, issue.projectUuid()); IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.getUuid()); if (issueFieldsSetter.setManualSeverity(issue, severity, context)) { return issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context, null, true); } return new SearchResponseData(issueDto); } }
@Test public void verify_notification_when_assignee_has_changed() { UserDto oldAssignee = db.users().insertUser(); RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(randomTypeExceptHotspot)) .setAssigneeUuid(oldAssignee.getUuid()) .toDefaultIssue(); UserDto changeAuthor = db.users().insertUser(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid()); UserDto newAssignee = db.users().insertUser(); issueFieldsSetter.assign(issue, newAssignee, context); underTest.saveIssue(db.getSession(), issue, context, null); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); IssueChangeNotification issueChangeNotification = notificationArgumentCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key()); assertThat(issueChangeNotification.getFieldValue("new.assignee")).isEqualTo(newAssignee.getName()); assertThat(issueChangeNotification.getFieldValue("old.assignee")).isNull(); assertThat(issueChangeNotification.getFieldValue("assignee")).isEqualTo(newAssignee.getLogin()); }
private SearchResponseData setType(DbSession session, String issueKey, RuleType ruleType) { IssueDto issueDto = issueFinder.getByKey(session, issueKey); DefaultIssue issue = issueDto.toDefaultIssue(); if (issue.isFromHotspot()) { throw new IllegalArgumentException("Changing type of a security hotspot is not permitted"); } userSession.checkComponentUuidPermission(ISSUE_ADMIN, issue.projectUuid()); IssueChangeContext context = IssueChangeContext.createUser(new Date(system2.now()), userSession.getUuid()); if (issueFieldsSetter.setType(issue, ruleType, context)) { return issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context, null, true); } return new SearchResponseData(issueDto); }
@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()); }
@Override public void handle(Request request, Response response) { userSession.checkLoggedIn(); AddCommentRequest wsRequest = toWsRequest(request); try (DbSession dbSession = dbClient.openSession(false)) { IssueDto issueDto = issueFinder.getByKey(dbSession, wsRequest.getIssue()); IssueChangeContext context = IssueChangeContext.createUser(new Date(system2.now()), userSession.getUuid()); DefaultIssue defaultIssue = issueDto.toDefaultIssue(); issueFieldsSetter.addComment(defaultIssue, wsRequest.getText(), context); SearchResponseData preloadedSearchResponseData = issueUpdater.saveIssueAndPreloadSearchResponseData(dbSession, defaultIssue, context, wsRequest.getText(), false); responseWriter.write(defaultIssue.key(), preloadedSearchResponseData, request, response); } }
@Test public void saveIssue_populates_specified_SearchResponseData_with_no_rule_but_with_project_and_component_if_rule_is_removed() { RuleDto rule = db.rules().insertRule(r -> r.setStatus(RuleStatus.REMOVED)); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); IssueDto issueDto = IssueTesting.newIssue(rule.getDefinition(), project, file); DefaultIssue issue = db.issues().insertIssue(issueDto).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); SearchResponseData preloadedSearchResponseData = underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, null, false); assertThat(preloadedSearchResponseData.getIssues()) .hasSize(1); assertThat(preloadedSearchResponseData.getIssues().iterator().next()) .isNotSameAs(issueDto); assertThat(preloadedSearchResponseData.getRules()).isNullOrEmpty(); assertThat(preloadedSearchResponseData.getComponents()) .extracting(ComponentDto::uuid) .containsOnly(project.uuid(), file.uuid()); }
@Test public void saveIssue_populates_specified_SearchResponseData_with_rule_project_and_component_retrieved_from_DB() { RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); IssueDto issueDto = IssueTesting.newIssue(rule.getDefinition(), project, file); DefaultIssue issue = db.issues().insertIssue(issueDto).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); SearchResponseData preloadedSearchResponseData = underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, null, true); assertThat(preloadedSearchResponseData.getIssues()) .hasSize(1); assertThat(preloadedSearchResponseData.getIssues().iterator().next()) .isNotSameAs(issueDto); assertThat(preloadedSearchResponseData.getRules()) .extracting(RuleDefinitionDto::getKey) .containsOnly(rule.getKey()); assertThat(preloadedSearchResponseData.getComponents()) .extracting(ComponentDto::uuid) .containsOnly(project.uuid(), file.uuid()); assertThat(issueChangePostProcessor.calledComponents()).containsExactlyInAnyOrder(file); }
private SearchResponseData doTransition(DbSession session, IssueDto issueDto, String transitionKey) { DefaultIssue defaultIssue = issueDto.toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(system2.now()), userSession.getUuid()); transitionService.checkTransitionPermission(transitionKey, defaultIssue); if (transitionService.doTransition(defaultIssue, context, transitionKey)) { return issueUpdater.saveIssueAndPreloadSearchResponseData(session, defaultIssue, context, null, true); } return new SearchResponseData(issueDto); } }
private SearchResponseData setType(DbSession session, String issueKey, String severity) { IssueDto issueDto = issueFinder.getByKey(session, issueKey); DefaultIssue issue = issueDto.toDefaultIssue(); userSession.checkComponentUuidPermission(ISSUE_ADMIN, issue.projectUuid()); IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.getUuid()); if (issueFieldsSetter.setManualSeverity(issue, severity, context)) { return issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context, null, true); } return new SearchResponseData(issueDto); } }