Рабочий вариант с ответами

This commit is contained in:
uPagge 2021-02-13 01:16:41 +03:00
parent adb5c6124f
commit 82a730c616
No known key found for this signature in database
GPG Key ID: 964B40928E4C9088
11 changed files with 44 additions and 10 deletions

View File

@ -36,6 +36,7 @@ public class SchedulerService {
@Scheduled(cron = "*/30 * * * * *") @Scheduled(cron = "*/30 * * * * *")
public void newDiscussion() { public void newDiscussion() {
discussionParser.scanOldDiscussions();
discussionParser.scanNewDiscussion(); discussionParser.scanNewDiscussion();
} }

View File

@ -42,3 +42,4 @@ gitlab-bot:
url-commit: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/commits?&page=1&per_page=1" url-commit: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/commits?&page=1&per_page=1"
url-new-note: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/discussions/{2}/notes?body={3}" url-new-note: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/discussions/{2}/notes?body={3}"
url-discussion: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/discussions?&page={2,number,integer}&per_page=100" url-discussion: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/discussions?&page={2,number,integer}&per_page=100"
url-one-discussion: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/discussions/{2}"

View File

@ -173,9 +173,6 @@
</column> </column>
<column name="noteable_iid" type="int"/> <column name="noteable_iid" type="int"/>
<column name="web_url" type="varchar(300)"/> <column name="web_url" type="varchar(300)"/>
<column name="responsible_id" type="integer">
<constraints foreignKeyName="note_responsible_id_person_id" references="person(id)"/>
</column>
<column name="discussion_id" type="varchar(200)"> <column name="discussion_id" type="varchar(200)">
<constraints foreignKeyName="note_discussion_id" references="discussion(id)" deleteCascade="true"/> <constraints foreignKeyName="note_discussion_id" references="discussion(id)" deleteCascade="true"/>
</column> </column>

View File

@ -8,6 +8,7 @@ import org.sadtech.haiti.context.domain.BasicEntity;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.JoinTable; import javax.persistence.JoinTable;
@ -46,6 +47,7 @@ public class Discussion implements BasicEntity<String> {
@OneToMany( @OneToMany(
mappedBy = "discussion", mappedBy = "discussion",
fetch = FetchType.EAGER,
cascade = { cascade = {
CascadeType.PERSIST, CascadeType.PERSIST,
CascadeType.MERGE CascadeType.MERGE

View File

@ -55,6 +55,9 @@ public class Note implements BasicEntity<Long> {
@Column(name = "web_url") @Column(name = "web_url")
private String webUrl; private String webUrl;
@Column(name = "resolvable")
private boolean resolvable;
@Column(name = "resolved") @Column(name = "resolved")
private Boolean resolved; private Boolean resolved;

View File

@ -58,4 +58,6 @@ public class GitlabProperty {
private String urlDiscussion; private String urlDiscussion;
private String urlOneDiscussion;
} }

View File

@ -31,6 +31,7 @@ public class NoteJsonConvert implements Converter<NoteJson, Note> {
note.setNoteableIid(source.getNoteableIid()); note.setNoteableIid(source.getNoteableIid());
note.setSystem(source.isSystem()); note.setSystem(source.isSystem());
note.setResolved(source.getResolved()); note.setResolved(source.getResolved());
note.setResolvable(source.isResolvable());
if (source.getResolvedBy() != null) { if (source.getResolvedBy() != null) {
note.setResolvedBy(personConverter.convert(source.getResolvedBy())); note.setResolvedBy(personConverter.convert(source.getResolvedBy()));
} }

View File

@ -69,10 +69,13 @@ public class DiscussionServiceImpl extends AbstractSimpleManagerService<Discussi
@Override @Override
public Discussion update(@NonNull Discussion discussion) { public Discussion update(@NonNull Discussion discussion) {
final Map<Long, Note> noteMap = discussionRepository.findById(discussion.getId()).orElseThrow(() -> new NotFoundException("Дискуссия не найдена")) final Discussion oldDiscussion = discussionRepository.findById(discussion.getId()).orElseThrow(() -> new NotFoundException("Дискуссия не найдена"));
final Map<Long, Note> noteMap = oldDiscussion
.getNotes().stream() .getNotes().stream()
.collect(Collectors.toMap(Note::getId, note -> note)); .collect(Collectors.toMap(Note::getId, note -> note));
discussion.setMergeRequest(oldDiscussion.getMergeRequest());
discussion.setResponsible(oldDiscussion.getResponsible());
discussion.getNotes().forEach(note -> updateNote(note, noteMap)); discussion.getNotes().forEach(note -> updateNote(note, noteMap));
return discussionRepository.save(discussion); return discussionRepository.save(discussion);
@ -81,9 +84,9 @@ public class DiscussionServiceImpl extends AbstractSimpleManagerService<Discussi
private void updateNote(Note note, Map<Long, Note> noteMap) { private void updateNote(Note note, Map<Long, Note> noteMap) {
if (noteMap.containsKey(note.getId())) { if (noteMap.containsKey(note.getId())) {
final Note oldNote = noteMap.get(note.getId()); final Note oldNote = noteMap.get(note.getId());
if (!oldNote.getUpdated().equals(note.getUpdated())) {
note.setWebUrl(oldNote.getWebUrl()); note.setWebUrl(oldNote.getWebUrl());
} } else {
notificationPersonal(note);
} }
} }

View File

@ -9,6 +9,7 @@ import org.sadtech.bot.gitlab.core.config.properties.GitlabProperty;
import org.sadtech.bot.gitlab.core.config.properties.PersonProperty; import org.sadtech.bot.gitlab.core.config.properties.PersonProperty;
import org.sadtech.bot.gitlab.sdk.domain.DiscussionJson; import org.sadtech.bot.gitlab.sdk.domain.DiscussionJson;
import org.sadtech.haiti.context.domain.ExistsContainer; import org.sadtech.haiti.context.domain.ExistsContainer;
import org.sadtech.haiti.context.exception.ConvertException;
import org.sadtech.haiti.context.page.Sheet; import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.core.page.PaginationImpl; import org.sadtech.haiti.core.page.PaginationImpl;
import org.sadtech.haiti.utils.network.HttpParse; import org.sadtech.haiti.utils.network.HttpParse;
@ -104,4 +105,27 @@ public class DiscussionParser {
.executeList(DiscussionJson.class); .executeList(DiscussionJson.class);
} }
public void scanOldDiscussions() {
int page = 0;
Sheet<Discussion> discussionSheet = discussionService.getAll(PaginationImpl.of(page, 100));
while (discussionSheet.hasContent()) {
final List<Discussion> discussions = discussionSheet.getContent();
for (Discussion discussion : discussions) {
final Discussion newDiscussion = HttpParse.request(MessageFormat.format(gitlabProperty.getUrlOneDiscussion(), discussion.getMergeRequest().getProjectId(), discussion.getMergeRequest().getTwoId(), discussion.getId()))
.header(ACCEPT)
.header(AUTHORIZATION, BEARER + personProperty.getToken())
.execute(DiscussionJson.class)
.map(json -> conversionService.convert(json, Discussion.class))
.orElseThrow(() -> new ConvertException("Ошибка парсинга дискуссии"));
discussionService.update(newDiscussion);
}
discussionSheet = discussionService.getAll(PaginationImpl.of(++page, 100));
}
}
} }

View File

@ -35,7 +35,7 @@ public class NoteJson {
@JsonProperty("noteable_type") @JsonProperty("noteable_type")
private String noteableType; private String noteableType;
private Boolean resolvable; private boolean resolvable;
private Boolean resolved; private Boolean resolved;

View File

@ -95,7 +95,7 @@ public class UnitConfig {
final String discussionId = note.getDiscussion().getId(); final String discussionId = note.getDiscussion().getId();
discussionService.answer(discussionId, MessageFormat.format("@{0}, {1}", note.getAuthor().getUserName(), message.getText())); discussionService.answer(discussionId, MessageFormat.format("@{0}, {1}", note.getAuthor().getUserName(), message.getText()));
} }
return BoxAnswer.of("Победа"); return BoxAnswer.of("");
} }
} }
return BoxAnswer.of("Ошибка"); return BoxAnswer.of("Ошибка");