Рефакторинг pipline

This commit is contained in:
Struchkov Mark 2022-12-05 16:50:05 +03:00
parent e985fe2cbd
commit 0d57bee785
17 changed files with 97 additions and 95 deletions

View File

@ -0,0 +1,41 @@
package dev.struchkov.bot.gitlab.context.domain;
import lombok.NonNull;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class ExistContainer<Entity, Key> {
protected final List<Entity> container;
protected final boolean allFound;
protected final Set<Key> idNoFound;
protected ExistContainer(List<Entity> container, boolean allFound, Set<Key> idNoFound) {
this.container = container;
this.allFound = allFound;
this.idNoFound = idNoFound;
}
public static <T, K> ExistContainer<T, K> allFind(@NonNull List<T> container) {
return new ExistContainer<>(container, true, Collections.emptySet());
}
public static <T, K> ExistContainer<T, K> notAllFind(@NonNull List<T> container, @NonNull Set<K> idNoFound) {
return new ExistContainer<>(container, false, idNoFound);
}
public List<Entity> getContainer() {
return container;
}
public boolean isAllFound() {
return allFound;
}
public Set<Key> getIdNoFound() {
return idNoFound;
}
}

View File

@ -1,40 +0,0 @@
package dev.struchkov.bot.gitlab.context.domain;
import lombok.NonNull;
import java.util.Collection;
import java.util.Collections;
public class ExistsContainer<Entity, Key> {
protected final Collection<Entity> container;
protected final boolean allFound;
protected final Collection<Key> idNoFound;
protected ExistsContainer(Collection<Entity> container, boolean allFound, Collection<Key> idNoFound) {
this.container = container;
this.allFound = allFound;
this.idNoFound = idNoFound;
}
public static <T, K> ExistsContainer<T, K> allFind(@NonNull Collection<T> container) {
return new ExistsContainer<>(container, true, Collections.emptyList());
}
public static <T, K> ExistsContainer<T, K> notAllFind(@NonNull Collection<T> container, @NonNull Collection<K> idNoFound) {
return new ExistsContainer<>(container, false, idNoFound);
}
public Collection<Entity> getContainer() {
return container;
}
public boolean isAllFound() {
return allFound;
}
public Collection<Key> getIdNoFound() {
return idNoFound;
}
}

View File

@ -6,6 +6,7 @@ import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
@ -53,7 +54,7 @@ public class Pipeline {
@JoinColumn(name = "project_id")
private Project project;
@ManyToOne
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name = "person_id")
private Person person;

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.context.service;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.entity.Discussion;
import lombok.NonNull;
import org.springframework.data.domain.Page;
@ -31,7 +31,7 @@ public interface DiscussionService {
*/
List<Discussion> getAllByMergeRequestId(@NonNull Long mergeRequestId);
ExistsContainer<Discussion, String> existsById(@NonNull Set<String> discussionIds);
ExistContainer<Discussion, String> existsById(@NonNull Set<String> discussionIds);
List<Discussion> createAll(@NonNull List<Discussion> newDiscussions);

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.context.service;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr;
import dev.struchkov.bot.gitlab.context.domain.MergeRequestState;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
@ -32,7 +32,7 @@ public interface MergeRequestsService {
Page<MergeRequest> getAll(@NonNull MergeRequestFilter filter, Pageable pagination);
ExistsContainer<MergeRequest, Long> existsById(@NonNull Set<Long> mergeRequestIds);
ExistContainer<MergeRequest, Long> existsById(@NonNull Set<Long> mergeRequestIds);
List<MergeRequest> createAll(List<MergeRequest> newMergeRequests);

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.context.service;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.entity.Person;
import lombok.NonNull;
@ -18,7 +18,7 @@ public interface PersonService {
Person getByIdOrThrown(@NonNull Long personId);
ExistsContainer<Person, Long> existsById(Set<Long> personIds);
ExistContainer<Person, Long> existsById(Set<Long> personIds);
List<Person> createAll(List<Person> newPersons);

View File

@ -1,9 +1,9 @@
package dev.struchkov.bot.gitlab.context.service;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.PipelineStatus;
import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline;
import dev.struchkov.bot.gitlab.context.domain.filter.PipelineFilter;
import dev.struchkov.haiti.context.domain.ExistsContainer;
import lombok.NonNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@ -25,7 +25,7 @@ public interface PipelineService {
Page<Pipeline> getAll(@NonNull PipelineFilter filter, @NonNull Pageable pagination);
ExistsContainer<Pipeline, Long> existsById(@NonNull Set<Long> pipelineIds);
ExistContainer<Pipeline, Long> existsById(@NonNull Set<Long> pipelineIds);
void deleteAllById(Set<Long> pipelineIds);

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.context.service;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.entity.Project;
import lombok.NonNull;
import org.springframework.data.domain.Page;
@ -26,6 +26,6 @@ public interface ProjectService {
boolean existsById(Long projectId);
ExistsContainer<Project, Long> existsById(Set<Long> projectIds);
ExistContainer<Project, Long> existsById(Set<Long> projectIds);
}

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.core.service.impl;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.PersonInformation;
import dev.struchkov.bot.gitlab.context.domain.entity.Discussion;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
@ -212,16 +212,16 @@ public class DiscussionServiceImpl implements DiscussionService {
}
@Override
public ExistsContainer<Discussion, String> existsById(@NonNull Set<String> discussionIds) {
public ExistContainer<Discussion, String> existsById(@NonNull Set<String> discussionIds) {
final List<Discussion> existsEntity = repository.findAllById(discussionIds);
final Set<String> existsIds = existsEntity.stream().map(Discussion::getId).collect(Collectors.toSet());
if (existsIds.containsAll(discussionIds)) {
return dev.struchkov.bot.gitlab.context.domain.ExistsContainer.allFind(existsEntity);
return ExistContainer.allFind(existsEntity);
} else {
final Set<String> noExistsId = discussionIds.stream()
.filter(id -> !existsIds.contains(id))
.collect(Collectors.toSet());
return ExistsContainer.notAllFind(existsEntity, noExistsId);
return ExistContainer.notAllFind(existsEntity, noExistsId);
}
}

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.core.service.impl;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr;
import dev.struchkov.bot.gitlab.context.domain.MergeRequestState;
import dev.struchkov.bot.gitlab.context.domain.PersonInformation;
@ -184,16 +184,16 @@ public class MergeRequestsServiceImpl implements MergeRequestsService {
}
@Override
public ExistsContainer<MergeRequest, Long> existsById(@NonNull Set<Long> mergeRequestIds) {
public ExistContainer<MergeRequest, Long> existsById(@NonNull Set<Long> mergeRequestIds) {
final List<MergeRequest> existsEntity = repository.findAllById(mergeRequestIds);
final Set<Long> existsIds = existsEntity.stream().map(MergeRequest::getId).collect(Collectors.toSet());
if (existsIds.containsAll(mergeRequestIds)) {
return ExistsContainer.allFind(existsEntity);
return ExistContainer.allFind(existsEntity);
} else {
final Set<Long> noExistsId = mergeRequestIds.stream()
.filter(id -> !existsIds.contains(id))
.collect(Collectors.toSet());
return ExistsContainer.notAllFind(existsEntity, noExistsId);
return ExistContainer.notAllFind(existsEntity, noExistsId);
}
}

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.core.service.impl;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.entity.Person;
import dev.struchkov.bot.gitlab.context.repository.PersonRepository;
import dev.struchkov.bot.gitlab.context.service.PersonService;
@ -40,16 +40,16 @@ public class PersonServiceImpl implements PersonService {
}
@Override
public ExistsContainer<Person, Long> existsById(Set<Long> personIds) {
public ExistContainer<Person, Long> existsById(Set<Long> personIds) {
final List<Person> existsEntity = repository.findAllById(personIds);
final Set<Long> existsIds = existsEntity.stream().map(Person::getId).collect(Collectors.toSet());
if (existsIds.containsAll(personIds)) {
return ExistsContainer.allFind(existsEntity);
return ExistContainer.allFind(existsEntity);
} else {
final Set<Long> noExistsId = personIds.stream()
.filter(id -> !existsIds.contains(id))
.collect(Collectors.toSet());
return ExistsContainer.notAllFind(existsEntity, noExistsId);
return ExistContainer.notAllFind(existsEntity, noExistsId);
}
}

View File

@ -1,5 +1,6 @@
package dev.struchkov.bot.gitlab.core.service.impl;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.PersonInformation;
import dev.struchkov.bot.gitlab.context.domain.PipelineStatus;
import dev.struchkov.bot.gitlab.context.domain.entity.Person;
@ -8,15 +9,14 @@ import dev.struchkov.bot.gitlab.context.domain.filter.PipelineFilter;
import dev.struchkov.bot.gitlab.context.domain.notify.pipeline.PipelineNotify;
import dev.struchkov.bot.gitlab.context.repository.PipelineRepository;
import dev.struchkov.bot.gitlab.context.service.NotifyService;
import dev.struchkov.bot.gitlab.context.service.PersonService;
import dev.struchkov.bot.gitlab.context.service.PipelineService;
import dev.struchkov.bot.gitlab.core.service.impl.filter.PipelineFilterService;
import dev.struchkov.haiti.context.domain.ExistsContainer;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set;
@ -27,6 +27,7 @@ import static dev.struchkov.bot.gitlab.context.domain.PipelineStatus.FAILED;
import static dev.struchkov.bot.gitlab.context.domain.PipelineStatus.SKIPPED;
import static dev.struchkov.bot.gitlab.context.domain.PipelineStatus.SUCCESS;
import static dev.struchkov.haiti.context.exception.NotFoundException.notFoundException;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
/**
* Реализация сервиса для работы с пайплайнами.
@ -42,14 +43,13 @@ public class PipelineServiceImpl implements PipelineService {
private final NotifyService notifyService;
private final PipelineRepository repository;
private final PersonService personService;
private final PipelineFilterService pipelineFilterService;
private final PersonInformation personInformation;
@Override
@Transactional
public Pipeline create(@NonNull Pipeline pipeline) {
personService.create(pipeline.getPerson());
final Pipeline newPipeline = repository.save(pipeline);
notifyNewPipeline(pipeline, "n/a");
return newPipeline;
@ -87,7 +87,7 @@ public class PipelineServiceImpl implements PipelineService {
private boolean isNeedNotifyNewPipeline(@NonNull Pipeline pipeline) {
final Person personPipelineCreator = pipeline.getPerson();
return notificationStatus.contains(pipeline.getStatus()) // Пайплайн имеет статус необходимый для уведомления
&& personPipelineCreator != null
&& checkNotNull(personPipelineCreator) // Создатель пайплайна не null
&& personInformation.getId().equals(personPipelineCreator.getId()); // Пользователь приложения является инициатором пайплайна
}
@ -102,16 +102,16 @@ public class PipelineServiceImpl implements PipelineService {
}
@Override
public ExistsContainer<Pipeline, Long> existsById(@NonNull Set<Long> pipelineIds) {
public ExistContainer<Pipeline, Long> existsById(@NonNull Set<Long> pipelineIds) {
final List<Pipeline> existsEntity = repository.findAllById(pipelineIds);
final Set<Long> existsIds = existsEntity.stream().map(Pipeline::getId).collect(Collectors.toSet());
if (existsIds.containsAll(pipelineIds)) {
return ExistsContainer.allFind(existsEntity);
return ExistContainer.allFind(existsEntity);
} else {
final Set<Long> noExistsId = pipelineIds.stream()
.filter(id -> !existsIds.contains(id))
.collect(Collectors.toSet());
return ExistsContainer.notAllFind(existsEntity, noExistsId);
return ExistContainer.notAllFind(existsEntity, noExistsId);
}
}

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.core.service.impl;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.PersonInformation;
import dev.struchkov.bot.gitlab.context.domain.entity.Project;
import dev.struchkov.bot.gitlab.context.domain.notify.NewProjectNotify;
@ -78,16 +78,16 @@ public class ProjectServiceImpl implements ProjectService {
}
@Override
public ExistsContainer<Project, Long> existsById(Set<Long> projectIds) {
public ExistContainer<Project, Long> existsById(Set<Long> projectIds) {
final List<Project> existsEntity = repository.findAllById(projectIds);
final Set<Long> existsIds = existsEntity.stream().map(Project::getId).collect(Collectors.toSet());
if (existsIds.containsAll(projectIds)) {
return ExistsContainer.allFind(existsEntity);
return ExistContainer.allFind(existsEntity);
} else {
final Set<Long> noExistsId = projectIds.stream()
.filter(id -> !existsIds.contains(id))
.collect(Collectors.toSet());
return ExistsContainer.notAllFind(existsEntity, noExistsId);
return ExistContainer.notAllFind(existsEntity, noExistsId);
}
}

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.core.service.parser;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.entity.Discussion;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
import dev.struchkov.bot.gitlab.context.domain.entity.Note;
@ -75,10 +75,10 @@ public class DiscussionParser {
.map(DiscussionJson::getId)
.collect(Collectors.toUnmodifiableSet());
final ExistsContainer<Discussion, String> existsContainer = discussionService.existsById(discussionIds);
if (!existsContainer.isAllFound()) {
final ExistContainer<Discussion, String> existContainer = discussionService.existsById(discussionIds);
if (!existContainer.isAllFound()) {
final List<Discussion> newDiscussions = discussionJson.stream()
.filter(json -> existsContainer.getIdNoFound().contains(json.getId()))
.filter(json -> existContainer.getIdNoFound().contains(json.getId()))
.map(json -> {
final Discussion discussion = conversionService.convert(json, Discussion.class);
discussion.setMergeRequest(mergeRequest);

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.core.service.parser;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr;
import dev.struchkov.bot.gitlab.context.domain.MergeRequestState;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
@ -96,10 +96,10 @@ public class MergeRequestParser {
.map(MergeRequestJson::getId)
.collect(Collectors.toSet());
final ExistsContainer<MergeRequest, Long> existsContainer = mergeRequestsService.existsById(jsonIds);
if (!existsContainer.isAllFound()) {
final ExistContainer<MergeRequest, Long> existContainer = mergeRequestsService.existsById(jsonIds);
if (!existContainer.isAllFound()) {
final List<MergeRequest> newMergeRequests = mergeRequestJsons.stream()
.filter(json -> existsContainer.getIdNoFound().contains(json.getId()))
.filter(json -> existContainer.getIdNoFound().contains(json.getId()))
.map(json -> {
final MergeRequest mergeRequest = conversionService.convert(json, MergeRequest.class);
parsingCommits(mergeRequest);

View File

@ -1,5 +1,6 @@
package dev.struchkov.bot.gitlab.core.service.parser;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.PipelineStatus;
import dev.struchkov.bot.gitlab.context.domain.entity.Pipeline;
import dev.struchkov.bot.gitlab.context.domain.entity.Project;
@ -9,7 +10,6 @@ import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty;
import dev.struchkov.bot.gitlab.core.config.properties.PersonProperty;
import dev.struchkov.bot.gitlab.core.utils.StringUtils;
import dev.struchkov.bot.gitlab.sdk.domain.PipelineJson;
import dev.struchkov.haiti.context.domain.ExistsContainer;
import dev.struchkov.haiti.utils.network.HttpParse;
import lombok.RequiredArgsConstructor;
import org.springframework.core.convert.ConversionService;
@ -19,7 +19,6 @@ import org.springframework.stereotype.Service;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@ -31,6 +30,7 @@ import static dev.struchkov.bot.gitlab.context.domain.PipelineStatus.PREPARING;
import static dev.struchkov.bot.gitlab.context.domain.PipelineStatus.RUNNING;
import static dev.struchkov.bot.gitlab.context.domain.PipelineStatus.WAITING_FOR_RESOURCE;
import static dev.struchkov.haiti.context.exception.ConvertException.convertException;
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
import static dev.struchkov.haiti.utils.network.HttpParse.ACCEPT;
/**
@ -75,17 +75,17 @@ public class PipelineParser {
LocalDateTime newLastUpdate = LocalDateTime.now();
List<PipelineJson> pipelineJsons = getPipelineJsons(project.getId(), page, lastUpdate);
while (!pipelineJsons.isEmpty()) {
while (checkNotEmpty(pipelineJsons)) {
final Set<Long> jsonIds = pipelineJsons.stream()
.map(PipelineJson::getId)
.collect(Collectors.toSet());
final ExistsContainer<Pipeline, Long> existsContainer = pipelineService.existsById(jsonIds);
final ExistContainer<Pipeline, Long> existContainer = pipelineService.existsById(jsonIds);
if (!existsContainer.isAllFound()) {
if (!existContainer.isAllFound()) {
final Collection<Long> idsNotFound = existsContainer.getIdNoFound();
final Set<Long> idsNotFound = existContainer.getIdNoFound();
for (Long newId : idsNotFound) {
final Pipeline newPipeline = HttpParse.request(

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.core.service.parser;
import dev.struchkov.bot.gitlab.context.domain.ExistsContainer;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.entity.Person;
import dev.struchkov.bot.gitlab.context.domain.entity.Project;
import dev.struchkov.bot.gitlab.context.service.PersonService;
@ -69,9 +69,9 @@ public class ProjectParser {
createNewPersons(projectJsons);
final ExistsContainer<Project, Long> existsContainer = projectService.existsById(projectIds);
final ExistContainer<Project, Long> existContainer = projectService.existsById(projectIds);
final List<Project> newProjects = projectJsons.stream()
.filter(json -> existsContainer.getIdNoFound().contains(json.getId()))
.filter(json -> existContainer.getIdNoFound().contains(json.getId()))
.map(json -> conversionService.convert(json, Project.class))
.toList();
@ -102,10 +102,10 @@ public class ProjectParser {
.map(ProjectJson::getCreatorId)
.collect(Collectors.toSet());
final ExistsContainer<Person, Long> existsContainer = personService.existsById(personCreatorId);
final ExistContainer<Person, Long> existContainer = personService.existsById(personCreatorId);
if (!existsContainer.isAllFound()) {
final Collection<Long> notFoundId = existsContainer.getIdNoFound();
if (!existContainer.isAllFound()) {
final Collection<Long> notFoundId = existContainer.getIdNoFound();
final List<Person> newPersons = notFoundId.stream()
.map(