Исправил дублирующийся ответ на комментарии

This commit is contained in:
upagge 2020-10-01 16:30:15 +03:00
parent 9f687469ee
commit b8f4a5fb2a
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
7 changed files with 66 additions and 49 deletions

View File

@ -4,6 +4,7 @@ package org.sadtech.bot.vcs.bitbucket.app.service.converter;
import org.sadtech.bot.vcs.bitbucket.sdk.domain.CommentJson;
import org.sadtech.bot.vcs.bitbucket.sdk.domain.Severity;
import org.sadtech.bot.vcs.core.domain.entity.Comment;
import org.sadtech.bot.vcs.core.utils.StringUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@ -19,7 +20,7 @@ public class CommentJsonToComment implements Converter<CommentJson, Comment> {
comment.setCreateDate(source.getCreatedDate());
comment.setAuthor(source.getAuthor().getName());
comment.setPullRequestId(source.getCustomPullRequestId());
comment.setMessage(source.getText());
comment.setMessage(StringUtils.cutOff(source.getText(), 490));
comment.setUrlApi(source.getCustomCommentApiUrl());
comment.setBitbucketVersion(source.getVersion());
comment.setAnswers(

View File

@ -6,6 +6,7 @@ import org.sadtech.bot.vcs.bitbucket.sdk.domain.CommentState;
import org.sadtech.bot.vcs.bitbucket.sdk.domain.Severity;
import org.sadtech.bot.vcs.core.domain.TaskStatus;
import org.sadtech.bot.vcs.core.domain.entity.Task;
import org.sadtech.bot.vcs.core.utils.StringUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@ -19,7 +20,7 @@ public class CommentJsonToTaskConvert implements Converter<CommentJson, Task> {
final Task task = new Task();
task.setId(source.getId());
task.setAuthor(source.getAuthor().getName());
task.setDescription(source.getText());
task.setDescription(StringUtils.cutOff(source.getText(), 490));
task.setCreateDate(source.getCreatedDate());
task.setBitbucketVersion(source.getVersion());
task.setPullRequestId(source.getCustomPullRequestId());

View File

@ -12,6 +12,7 @@ import org.sadtech.bot.vcs.core.domain.PullRequestStatus;
import org.sadtech.bot.vcs.core.domain.ReviewerStatus;
import org.sadtech.bot.vcs.core.domain.entity.PullRequest;
import org.sadtech.bot.vcs.core.domain.entity.Reviewer;
import org.sadtech.bot.vcs.core.utils.StringUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@ -30,9 +31,9 @@ public class PullRequestJsonConverter implements Converter<PullRequestJson, Pull
pullRequest.setCreateDate(json.getCreatedDate());
pullRequest.setUpdateDate(json.getUpdatedDate());
pullRequest.setConflict(convertConflict(json.getProperties()));
pullRequest.setDescription(convertString(json.getDescription(), 180));
pullRequest.setDescription(StringUtils.cutOff(json.getDescription(), 180));
pullRequest.setAuthorLogin(json.getAuthor().getUser().getName());
pullRequest.setTitle(convertString(json.getTitle(), 90));
pullRequest.setTitle(StringUtils.cutOff(json.getTitle(), 90));
pullRequest.setUrl(json.getLinks().getSelf().get(0).getHref());
pullRequest.setStatus(convertPullRequestStatus(json.getState()));
pullRequest.setProjectKey(json.getFromRef().getRepository().getProject().getKey());
@ -50,13 +51,6 @@ public class PullRequestJsonConverter implements Converter<PullRequestJson, Pull
&& Outcome.CONFLICTED.equals(properties.getMergeResult().getOutcome());
}
private String convertString(String string, int length) {
if (string != null) {
return string.length() > length ? string.substring(0, length) + "..." : string;
}
return null;
}
public static PullRequestStatus convertPullRequestStatus(PullRequestState state) {
switch (state) {
case OPEN:

View File

@ -122,23 +122,25 @@ public class CommentServiceImpl extends AbstractSimpleManagerService<Comment, Lo
final Set<Long> newAnswerIds = newComment.getAnswers();
if (!oldAnswerIds.equals(newAnswerIds)) {
final Set<Long> existsNewAnswersIds = commentRepository.existsById(newAnswerIds);
final List<Comment> newAnswers = commentRepository.findAllById(existsNewAnswersIds).stream()
.filter(comment -> !oldAnswerIds.contains(comment.getId()))
.collect(Collectors.toList());
oldComment.getAnswers().clear();
oldComment.setAnswers(existsNewAnswersIds);
notifyService.send(
AnswerCommentNotify.builder()
.logins(Collections.singleton(newComment.getAuthor()))
.url(oldComment.getUrl())
.youMessage(newComment.getMessage())
.answers(
newAnswers.stream()
.map(answerComment -> Answer.of(answerComment.getAuthor(), answerComment.getMessage()))
.collect(Collectors.toList())
)
.build()
);
if (!existsNewAnswersIds.isEmpty()) {
final List<Comment> newAnswers = commentRepository.findAllById(existsNewAnswersIds).stream()
.filter(comment -> !oldAnswerIds.contains(comment.getId()))
.collect(Collectors.toList());
oldComment.getAnswers().clear();
oldComment.setAnswers(existsNewAnswersIds);
notifyService.send(
AnswerCommentNotify.builder()
.logins(Collections.singleton(newComment.getAuthor()))
.url(oldComment.getUrl())
.youMessage(newComment.getMessage())
.answers(
newAnswers.stream()
.map(answerComment -> Answer.of(answerComment.getAuthor(), answerComment.getMessage()))
.collect(Collectors.toList())
)
.build()
);
}
}
}

View File

@ -120,23 +120,25 @@ public class TaskServiceImpl extends AbstractSimpleManagerService<Task, Long> im
final Set<Long> newAnswerIds = task.getAnswers();
if (!oldAnswerIds.equals(newAnswerIds)) {
final Set<Long> existsNewAnswersIds = commentService.existsById(newAnswerIds);
final List<Comment> newAnswers = commentService.getAllById(existsNewAnswersIds).stream()
.filter(comment -> !oldAnswerIds.contains(comment.getId()))
.collect(Collectors.toList());
oldTask.getAnswers().clear();
oldTask.setAnswers(existsNewAnswersIds);
notifyService.send(
AnswerCommentNotify.builder()
.logins(Collections.singleton(oldTask.getAuthor()))
.url(oldTask.getUrl())
.youMessage(oldTask.getDescription())
.answers(
newAnswers.stream()
.map(answerComment -> Answer.of(answerComment.getAuthor(), answerComment.getMessage()))
.collect(Collectors.toList())
)
.build()
);
if (!existsNewAnswersIds.isEmpty()) {
final List<Comment> newAnswers = commentService.getAllById(existsNewAnswersIds).stream()
.filter(comment -> !oldAnswerIds.contains(comment.getId()))
.collect(Collectors.toList());
oldTask.getAnswers().clear();
oldTask.setAnswers(existsNewAnswersIds);
notifyService.send(
AnswerCommentNotify.builder()
.logins(Collections.singleton(oldTask.getAuthor()))
.url(oldTask.getUrl())
.youMessage(oldTask.getDescription())
.answers(
newAnswers.stream()
.map(answerComment -> Answer.of(answerComment.getAuthor(), answerComment.getMessage()))
.collect(Collectors.toList())
)
.build()
);
}
}
}

View File

@ -52,8 +52,4 @@ public enum Smile {
return value;
}
public static class Constants {
public static final String EMPTY = "";
}
}

View File

@ -0,0 +1,21 @@
package org.sadtech.bot.vcs.core.utils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* // TODO: 29.09.2020 Добавить описание.
*
* @author upagge 29.09.2020
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class StringUtils {
public static String cutOff(String string, int length) {
if (string != null) {
return string.length() > length ? string.substring(0, length) + "..." : string;
}
return null;
}
}