diff --git a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/convert/MergeRequestJsonConverter.java b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/convert/MergeRequestJsonConverter.java index 829c949..3675b6c 100644 --- a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/convert/MergeRequestJsonConverter.java +++ b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/convert/MergeRequestJsonConverter.java @@ -1,11 +1,10 @@ package org.sadtech.bot.gitlab.app.service.convert; +import lombok.RequiredArgsConstructor; import org.sadtech.bot.gitlab.context.domain.MergeRequestState; import org.sadtech.bot.gitlab.context.domain.entity.MergeRequest; -import org.sadtech.bot.gitlab.context.domain.entity.Person; import org.sadtech.bot.gitlab.sdk.domain.MergeRequestJson; import org.sadtech.bot.gitlab.sdk.domain.MergeRequestStateJson; -import org.sadtech.bot.gitlab.sdk.domain.PersonJson; import org.sadtech.haiti.context.exception.ConvertException; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; @@ -16,8 +15,11 @@ import org.springframework.stereotype.Component; * @author upagge 15.01.2021 */ @Component +@RequiredArgsConstructor public class MergeRequestJsonConverter implements Converter { + private final PersonJsonConverter convertPerson; + @Override public MergeRequest convert(MergeRequestJson source) { final MergeRequest mergeRequest = new MergeRequest(); @@ -32,20 +34,13 @@ public class MergeRequestJsonConverter implements Converter { + + @Override + public Person convert(PersonJson source) { + final Person person = new Person(); + person.setId(source.getId()); + person.setName(source.getName()); + person.setUserName(source.getUsername()); + person.setWebUrl(source.getWebUrl()); + return person; + } + +} diff --git a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/convert/ProjectJsonConverter.java b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/convert/ProjectJsonConverter.java index 52a4ba0..2338469 100644 --- a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/convert/ProjectJsonConverter.java +++ b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/convert/ProjectJsonConverter.java @@ -21,6 +21,7 @@ public class ProjectJsonConverter implements Converter { project.setCreatorId(source.getCreatorId()); project.setDescription(source.getDescription()); project.setName(source.getName()); + project.setWebUrl(source.getWebUrl()); return project; } diff --git a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/parser/ProjectParser.java b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/parser/ProjectParser.java index 07700ca..16aa2dc 100644 --- a/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/parser/ProjectParser.java +++ b/bot-app/src/main/java/org/sadtech/bot/gitlab/app/service/parser/ProjectParser.java @@ -3,20 +3,26 @@ package org.sadtech.bot.gitlab.app.service.parser; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.sadtech.bot.gitlab.app.config.property.CommentSchedulerProperty; +import org.sadtech.bot.gitlab.context.domain.entity.Person; import org.sadtech.bot.gitlab.context.domain.entity.Project; +import org.sadtech.bot.gitlab.context.service.PersonService; import org.sadtech.bot.gitlab.context.service.ProjectService; import org.sadtech.bot.gitlab.core.config.properties.GitlabProperty; import org.sadtech.bot.gitlab.core.config.properties.InitProperty; import org.sadtech.bot.gitlab.core.config.properties.PersonProperty; +import org.sadtech.bot.gitlab.sdk.domain.PersonJson; import org.sadtech.bot.gitlab.sdk.domain.ProjectJson; import org.sadtech.haiti.context.domain.ExistsContainer; +import org.sadtech.haiti.context.exception.ConvertException; import org.sadtech.haiti.utils.network.HttpHeader; import org.sadtech.haiti.utils.network.HttpParse; import org.springframework.core.convert.ConversionService; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import java.util.Collection; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -35,6 +41,7 @@ import static org.sadtech.haiti.utils.network.HttpParse.BEARER; public class ProjectParser { private final ProjectService projectService; + private final PersonService personService; private final ConversionService conversionService; @@ -54,6 +61,8 @@ public class ProjectParser { .map(ProjectJson::getId) .collect(Collectors.toSet()); + createNewPersons(projectJsons); + final ExistsContainer existsContainer = projectService.existsById(jsonIds); final List newProjects = projectJsons.stream() .filter(json -> existsContainer.getIdNoFound().contains(json.getId())) @@ -65,4 +74,33 @@ public class ProjectParser { } } + private void createNewPersons(List projectJsons) { + final Set jsonIds = projectJsons.stream() + .map(ProjectJson::getCreatorId) + .collect(Collectors.toSet()); + + final ExistsContainer existsContainer = personService.existsById(jsonIds); + + if (!existsContainer.isAllFound()) { + final Collection notFoundId = existsContainer.getIdNoFound(); + + final List newPersons = notFoundId.stream() + .map( + userId -> { + final Optional execute = HttpParse.request(gitlabProperty.getUsersUrl() + "/" + userId) + .header(ACCEPT) + .header(AUTHORIZATION, BEARER + personProperty.getToken()) + .execute(PersonJson.class); + final Optional person = execute + .map(json -> conversionService.convert(json, Person.class)); + return person + .orElseThrow(() -> new ConvertException("Ошибка преобразования нового пользователя")); + } + ).collect(Collectors.toList()); + + personService.createAll(newPersons); + + } + } + } diff --git a/bot-app/src/main/resources/application.yaml b/bot-app/src/main/resources/application.yaml index 4fcfb57..7709810 100644 --- a/bot-app/src/main/resources/application.yaml +++ b/bot-app/src/main/resources/application.yaml @@ -38,6 +38,7 @@ gitlab-bot: url-pull-request-comment: ${GITLAB_URL} url-pull-request: ${GITLAB_URL} user-url: ${GITLAB_URL}/api/v4/user + users-url: ${GITLAB_URL}/api/v4/users teamcity: token: ${TEAMCITY_ADMIN_TOKEN} project-url: ${TEAMCITY_URL}/app/rest/projects diff --git a/bot-app/src/main/resources/liquibase/v.1.0.0/2021-01-14-create-tables.xml b/bot-app/src/main/resources/liquibase/v.1.0.0/2021-01-14-create-tables.xml index 717ac7c..ebf0001 100644 --- a/bot-app/src/main/resources/liquibase/v.1.0.0/2021-01-14-create-tables.xml +++ b/bot-app/src/main/resources/liquibase/v.1.0.0/2021-01-14-create-tables.xml @@ -12,8 +12,15 @@ - - + + + + + + + + + @@ -27,7 +34,7 @@ - + @@ -58,7 +65,7 @@ - + @@ -71,6 +78,12 @@ + + + + + + diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/MergeRequest.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/MergeRequest.java index 18db9bd..c450869 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/MergeRequest.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/MergeRequest.java @@ -73,6 +73,12 @@ public class MergeRequest implements BasicEntity { @JoinColumn(name = "assignee_id") private Person assignee; + @Column(name = "target_branch") + private String targetBranch; + + @Column(name = "source_branch") + private String sourceBranch; + @ElementCollection @CollectionTable(name = "merge_request_label", joinColumns = @JoinColumn(name = "merge_request_id")) @Column(name = "label") diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Project.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Project.java index 53359e8..42a8fe4 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Project.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/entity/Project.java @@ -37,6 +37,9 @@ public class Project implements BasicEntity { private LocalDateTime createdDate; @Column(name = "creator_id") - private Integer creatorId; + private Long creatorId; + + @Column(name = "web_url") + private String webUrl; } diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/NewProjectNotify.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/NewProjectNotify.java new file mode 100644 index 0000000..8a8857b --- /dev/null +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/NewProjectNotify.java @@ -0,0 +1,41 @@ +package org.sadtech.bot.gitlab.context.domain.notify; + +import lombok.Builder; +import org.sadtech.bot.gitlab.context.utils.Smile; + +import java.text.MessageFormat; + +/** + * // TODO: 15.01.2021 Добавить описание. + * + * @author upagge 15.01.2021 + */ +public class NewProjectNotify extends Notify { + + private final String projectName; + private final String projectUrl; + private final String projectDescription; + private final String authorName; + + @Builder + public NewProjectNotify(String projectName, String projectUrl, String projectDescription, String authorName) { + this.projectName = projectName; + this.projectUrl = projectUrl; + this.projectDescription = projectDescription; + this.authorName = authorName; + } + + @Override + public String generateMessage() { + return MessageFormat.format( + "{0} *Новый Проект*{1}" + + "[{2}]({3}){1}" + + "{4}" + + "{5}: {6}\n\n", + Smile.FUN, Smile.HR, projectName, projectUrl, + (projectDescription != null && !"".equals(projectDescription)) ? escapeMarkdown(projectDescription) + Smile.HR : "", + Smile.AUTHOR, authorName + ); + } + +} diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pullrequest/NewPrNotify.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pullrequest/NewPrNotify.java index ba371ae..41f41b4 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pullrequest/NewPrNotify.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/domain/notify/pullrequest/NewPrNotify.java @@ -13,6 +13,8 @@ public class NewPrNotify extends PrNotify { private final String description; private final String author; + private final String targetBranch; + private final String sourceBranch; private final Set labels; @Builder @@ -22,10 +24,15 @@ public class NewPrNotify extends PrNotify { String description, String author, String projectName, - Set labels) { + String targetBranch, + String sourceBranch, + Set labels + ) { super(projectName, title, url); this.description = description; this.author = author; + this.targetBranch = targetBranch; + this.sourceBranch = sourceBranch; this.labels = labels; } @@ -41,10 +48,10 @@ public class NewPrNotify extends PrNotify { "{0} *Новый PullRequest | {1}*{2}" + "[{3}]({4})" + "{5}" + - "{2}{7}: {8}\n\n", + "{2}{9}: {10} {12} {11}\n{7}: {8}", Smile.FUN, projectName, Smile.HR, title, url, labelText, (description != null && !"".equals(description)) ? escapeMarkdown(description) + Smile.HR : "", - Smile.AUTHOR, author + Smile.AUTHOR, author, Smile.TREE, sourceBranch, targetBranch, Smile.ARROW ); } diff --git a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/utils/Smile.java b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/utils/Smile.java index 713d965..12294a3 100644 --- a/bot-context/src/main/java/org/sadtech/bot/gitlab/context/utils/Smile.java +++ b/bot-context/src/main/java/org/sadtech/bot/gitlab/context/utils/Smile.java @@ -41,6 +41,7 @@ public enum Smile { BUILD("♻️"), SMART("\uD83E\uDDE0"), SADLY("\uD83D\uDE14"), + TREE("\uD83C\uDF33"), TOP("\uD83D\uDD1D"); @Getter diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/config/properties/GitlabProperty.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/config/properties/GitlabProperty.java index 93190f7..dfb6651 100644 --- a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/config/properties/GitlabProperty.java +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/config/properties/GitlabProperty.java @@ -40,4 +40,6 @@ public class GitlabProperty { private String userUrl; + private String usersUrl; + } diff --git a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java index a3dd832..0c5b1f5 100644 --- a/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java +++ b/bot-core/src/main/java/org/sadtech/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java @@ -75,6 +75,8 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService implements ProjectService { private final ProjectRepository projectRepository; + private final NotifyService notifyService; + private final PersonService personService; + private final PersonInformation personInformation; - public ProjectServiceImpl(SimpleManagerRepository repository, ProjectRepository projectRepository) { + public ProjectServiceImpl( + SimpleManagerRepository repository, + ProjectRepository projectRepository, + NotifyService notifyService, + PersonService personService, PersonInformation personInformation + ) { super(repository); this.projectRepository = projectRepository; + this.notifyService = notifyService; + this.personService = personService; + this.personInformation = personInformation; } @Override public Project create(@NonNull Project project) { - return projectRepository.save(project); + final Project newProject = projectRepository.save(project); + + if (!newProject.getCreatorId().equals(personInformation.getId())) { + final String authorName = personService.getById(newProject.getCreatorId()) + .orElseThrow(() -> new NotFoundException("Пользователь не найден")) + .getName(); + notifyService.send( + NewProjectNotify.builder() + .projectDescription(newProject.getDescription()) + .projectName(newProject.getName()) + .projectUrl(newProject.getWebUrl()) + .authorName(authorName) + .build() + ); + } + + return newProject; } @Override diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/MergeRequestJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/MergeRequestJson.java index 78b4669..1819687 100644 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/MergeRequestJson.java +++ b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/MergeRequestJson.java @@ -48,6 +48,12 @@ public class MergeRequestJson { @JsonProperty("has_conflicts") private boolean conflicts; + @JsonProperty("target_branch") + private String targetBranch; + + @JsonProperty("source_branch") + private String sourceBranch; + private Set labels; } diff --git a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/ProjectJson.java b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/ProjectJson.java index 35c8ca7..3abee80 100644 --- a/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/ProjectJson.java +++ b/gitlab-sdk/src/main/java/org/sadtech/bot/gitlab/sdk/domain/ProjectJson.java @@ -26,7 +26,10 @@ public class ProjectJson { @JsonProperty("created_at") private LocalDateTime createdDate; + @JsonProperty("web_url") + private String webUrl; + @JsonProperty("creator_id") - private Integer creatorId; + private Long creatorId; }