private boolean canEditOrDelete(IssueChangeDto dto) { return userSession.isLoggedIn() && requireNonNull(userSession.getUuid(), "User uuid should not be null").equals(dto.getUserUuid()); }
CommentData(DbSession dbSession, String commentKey) { this.issueChangeDto = dbClient.issueChangeDao().selectCommentByKey(dbSession, commentKey) .orElseThrow(() -> new NotFoundException(format("Comment with key '%s' does not exist", commentKey))); // Load issue now to quickly fail if user hasn't permission to see it this.issueDto = issueFinder.getByKey(dbSession, issueChangeDto.getIssueKey()); checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only delete your own comments"); }
CommentData(DbSession dbSession, EditCommentRequest request) { this.request = request; this.issueChangeDto = dbClient.issueChangeDao().selectCommentByKey(dbSession, request.getComment()) .orElseThrow(() -> new NotFoundException(format("Comment with key '%s' does not exist", request.getComment()))); // Load issue now to quickly fail if user hasn't permission to see it this.issueDto = issueFinder.getByKey(dbSession, issueChangeDto.getIssueKey()); checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only edit your own comments"); }
private void loadComments(Collector collector, DbSession dbSession, Set<SearchAdditionalField> fields, SearchResponseData result) { if (fields.contains(COMMENTS)) { List<IssueChangeDto> comments = dbClient.issueChangeDao().selectByTypeAndIssueKeys(dbSession, collector.getIssueKeys(), IssueChangeDto.TYPE_COMMENT); result.setComments(comments); for (IssueChangeDto comment : comments) { collector.addUserUuids(singletonList(comment.getUserUuid())); if (canEditOrDelete(comment)) { result.addUpdatableComment(comment.getKey()); } } } }
@Test public void create_from_comment() { DefaultIssueComment comment = DefaultIssueComment.create("ABCDE", "user_uuid", "the comment"); IssueChangeDto dto = IssueChangeDto.of(comment); assertThat(dto.getChangeData()).isEqualTo("the comment"); assertThat(dto.getChangeType()).isEqualTo("comment"); assertThat(dto.getCreatedAt()).isNotNull(); assertThat(dto.getUpdatedAt()).isNotNull(); assertThat(dto.getIssueChangeCreationDate()).isNotNull(); assertThat(dto.getIssueKey()).isEqualTo("ABCDE"); assertThat(dto.getUserUuid()).isEqualTo("user_uuid"); }
@Test public void create_from_diff() { FieldDiffs diffs = new FieldDiffs(); diffs.setDiff("severity", "INFO", "BLOCKER"); diffs.setUserUuid("user_uuid"); diffs.setCreationDate(parseDate("2015-01-13")); IssueChangeDto dto = IssueChangeDto.of("ABCDE", diffs); assertThat(dto.getChangeData()).isEqualTo("severity=INFO|BLOCKER"); assertThat(dto.getChangeType()).isEqualTo("diff"); assertThat(dto.getCreatedAt()).isNotNull(); assertThat(dto.getUpdatedAt()).isNotNull(); assertThat(dto.getIssueKey()).isEqualTo("ABCDE"); assertThat(dto.getUserUuid()).isEqualTo("user_uuid"); assertThat(dto.getIssueChangeCreationDate()).isEqualTo(parseDate("2015-01-13").getTime()); }
@Test public void select_comment_by_key() { IssueDto issue = db.issues().insertIssue(); IssueChangeDto issueChange = db.issues().insertChange(issue, c -> c.setChangeType(TYPE_COMMENT)); db.issues().insertChange(issue, c -> c.setChangeType(TYPE_COMMENT)); Optional<IssueChangeDto> issueChangeDto = underTest.selectCommentByKey(db.getSession(), issueChange.getKey()); assertThat(issueChangeDto).isPresent(); assertThat(issueChangeDto.get().getKey()).isEqualTo(issueChange.getKey()); assertThat(issueChangeDto.get().getChangeType()).isEqualTo(TYPE_COMMENT); assertThat(issueChangeDto.get().getUserUuid()).isEqualTo(issueChange.getUserUuid()); assertThat(issueChangeDto.get().getChangeData()).isEqualTo(issueChange.getChangeData()); assertThat(issueChangeDto.get().getIssueChangeCreationDate()).isNotNull(); assertThat(issueChangeDto.get().getCreatedAt()).isNotNull(); assertThat(issueChangeDto.get().getUpdatedAt()).isNotNull(); }
@Test public void add_comment() { IssueDto issueDto = issueDbTester.insertIssue(); loginWithBrowsePermission(issueDto, USER); call(issueDto.getKey(), "please fix it"); verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class)); verifyContentOfPreloadedSearchResponseData(issueDto); IssueChangeDto issueComment = dbClient.issueChangeDao().selectByTypeAndIssueKeys(dbTester.getSession(), singletonList(issueDto.getKey()), TYPE_COMMENT).get(0); assertThat(issueComment.getKey()).isNotNull(); assertThat(issueComment.getUserUuid()).isEqualTo(userSession.getUuid()); assertThat(issueComment.getChangeType()).isEqualTo(TYPE_COMMENT); assertThat(issueComment.getChangeData()).isEqualTo("please fix it"); assertThat(issueComment.getCreatedAt()).isNotNull(); assertThat(issueComment.getUpdatedAt()).isNotNull(); assertThat(issueComment.getIssueKey()).isEqualTo(issueDto.getKey()); assertThat(issueComment.getIssueChangeCreationDate()).isNotNull(); IssueDto issueReloaded = dbClient.issueDao().selectByKey(dbTester.getSession(), issueDto.getKey()).get(); assertThat(issueReloaded.getIssueUpdateTime()).isEqualTo(NOW); assertThat(issueChangePostProcessor.wasCalled()).isFalse(); }
private static void formatIssueComments(SearchResponseData data, Issue.Builder wsIssue, IssueDto dto) { Comments.Builder wsComments = Comments.newBuilder(); List<IssueChangeDto> comments = data.getCommentsForIssueKey(dto.getKey()); if (comments != null) { Comment.Builder wsComment = Comment.newBuilder(); for (IssueChangeDto comment : comments) { String markdown = comment.getChangeData(); wsComment .clear() .setKey(comment.getKey()) .setUpdatable(data.isUpdatableComment(comment.getKey())) .setCreatedAt(DateUtils.formatDateTime(new Date(comment.getIssueChangeCreationDate()))); ofNullable(data.getUserByUuid(comment.getUserUuid())).ifPresent(user -> wsComment.setLogin(user.getLogin())); if (markdown != null) { wsComment .setHtmlText(Markdown.convertToHtml(markdown)) .setMarkdown(markdown); } wsComments.addComments(wsComment); } } wsIssue.setComments(wsComments); }
@Test public void bulk_change_with_comment() { 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.setType(BUG) .setStatus(STATUS_OPEN).setResolution(null)); BulkChangeWsResponse response = call(builder() .setIssues(singletonList(issue.getKey())) .setDoTransition("confirm") .setComment("type was badly defined") .build()); checkResponse(response, 1, 1, 0, 0); IssueChangeDto issueComment = dbClient.issueChangeDao().selectByTypeAndIssueKeys(db.getSession(), singletonList(issue.getKey()), TYPE_COMMENT).get(0); assertThat(issueComment.getUserUuid()).isEqualTo(user.getUuid()); assertThat(issueComment.getChangeData()).isEqualTo("type was badly defined"); verifyPostProcessorCalled(file); }
private boolean canEditOrDelete(IssueChangeDto dto) { return userSession.isLoggedIn() && requireNonNull(userSession.getUuid(), "User uuid should not be null").equals(dto.getUserUuid()); }
CommentData(DbSession dbSession, String commentKey) { this.issueChangeDto = dbClient.issueChangeDao().selectCommentByKey(dbSession, commentKey) .orElseThrow(() -> new NotFoundException(format("Comment with key '%s' does not exist", commentKey))); // Load issue now to quickly fail if user hasn't permission to see it this.issueDto = issueFinder.getByKey(dbSession, issueChangeDto.getIssueKey()); checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only delete your own comments"); }
CommentData(DbSession dbSession, EditCommentRequest request) { this.request = request; this.issueChangeDto = dbClient.issueChangeDao().selectCommentByKey(dbSession, request.getComment()) .orElseThrow(() -> new NotFoundException(format("Comment with key '%s' does not exist", request.getComment()))); // Load issue now to quickly fail if user hasn't permission to see it this.issueDto = issueFinder.getByKey(dbSession, issueChangeDto.getIssueKey()); checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only edit your own comments"); }
private void loadComments(Collector collector, DbSession dbSession, SearchResponseData result) { if (collector.contains(COMMENTS)) { List<IssueChangeDto> comments = dbClient.issueChangeDao().selectByTypeAndIssueKeys(dbSession, collector.getIssueKeys(), IssueChangeDto.TYPE_COMMENT); result.setComments(comments); for (IssueChangeDto comment : comments) { collector.add(USERS, comment.getUserUuid()); if (canEditOrDelete(comment)) { result.addUpdatableComment(comment.getKey()); } } } }
private static void formatIssueComments(SearchResponseData data, Issue.Builder wsIssue, IssueDto dto) { Comments.Builder wsComments = Comments.newBuilder(); List<IssueChangeDto> comments = data.getCommentsForIssueKey(dto.getKey()); if (comments != null) { Comment.Builder wsComment = Comment.newBuilder(); for (IssueChangeDto comment : comments) { String markdown = comment.getChangeData(); wsComment .clear() .setKey(comment.getKey()) .setUpdatable(data.isUpdatableComment(comment.getKey())) .setCreatedAt(DateUtils.formatDateTime(new Date(comment.getIssueChangeCreationDate()))); setNullable(data.getUserByUuid(comment.getUserUuid()), user -> wsComment.setLogin(user.getLogin())); if (markdown != null) { wsComment .setHtmlText(Markdown.convertToHtml(markdown)) .setMarkdown(markdown); } wsComments.addComments(wsComment); } } wsIssue.setComments(wsComments); }