Уведомления по Project

This commit is contained in:
uPagge 2021-01-15 22:53:15 +03:00
parent 038d25a513
commit 0e15b27095
No known key found for this signature in database
GPG Key ID: 964B40928E4C9088
16 changed files with 201 additions and 24 deletions

View File

@ -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<MergeRequestJson, MergeRequest> {
private final PersonJsonConverter convertPerson;
@Override
public MergeRequest convert(MergeRequestJson source) {
final MergeRequest mergeRequest = new MergeRequest();
@ -32,20 +34,13 @@ public class MergeRequestJsonConverter implements Converter<MergeRequestJson, Me
mergeRequest.setProjectId(source.getProjectId());
mergeRequest.setWebUrl(source.getWebUrl());
mergeRequest.setLabels(source.getLabels());
mergeRequest.setAssignee(convertPerson(source.getAssignee()));
mergeRequest.setAuthor(convertPerson(source.getAssignee()));
mergeRequest.setAssignee(convertPerson.convert(source.getAssignee()));
mergeRequest.setAuthor(convertPerson.convert(source.getAssignee()));
mergeRequest.setSourceBranch(source.getSourceBranch());
mergeRequest.setTargetBranch(source.getTargetBranch());
return mergeRequest;
}
private Person convertPerson(PersonJson personJson) {
final Person person = new Person();
person.setId(personJson.getId());
person.setName(personJson.getName());
person.setUserName(personJson.getUsername());
person.setWebUrl(personJson.getWebUrl());
return person;
}
private MergeRequestState convertState(MergeRequestStateJson state) {
switch (state) {
case CLOSED:

View File

@ -0,0 +1,26 @@
package org.sadtech.bot.gitlab.app.service.convert;
import org.sadtech.bot.gitlab.context.domain.entity.Person;
import org.sadtech.bot.gitlab.sdk.domain.PersonJson;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
/**
* // TODO: 15.01.2021 Добавить описание.
*
* @author upagge 15.01.2021
*/
@Component
public class PersonJsonConverter implements Converter<PersonJson, Person> {
@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;
}
}

View File

@ -21,6 +21,7 @@ public class ProjectJsonConverter implements Converter<ProjectJson, Project> {
project.setCreatorId(source.getCreatorId());
project.setDescription(source.getDescription());
project.setName(source.getName());
project.setWebUrl(source.getWebUrl());
return project;
}

View File

@ -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<Project, Long> existsContainer = projectService.existsById(jsonIds);
final List<Project> newProjects = projectJsons.stream()
.filter(json -> existsContainer.getIdNoFound().contains(json.getId()))
@ -65,4 +74,33 @@ public class ProjectParser {
}
}
private void createNewPersons(List<ProjectJson> projectJsons) {
final Set<Long> jsonIds = projectJsons.stream()
.map(ProjectJson::getCreatorId)
.collect(Collectors.toSet());
final ExistsContainer<Person, Long> existsContainer = personService.existsById(jsonIds);
if (!existsContainer.isAllFound()) {
final Collection<Long> notFoundId = existsContainer.getIdNoFound();
final List<Person> newPersons = notFoundId.stream()
.map(
userId -> {
final Optional<PersonJson> execute = HttpParse.request(gitlabProperty.getUsersUrl() + "/" + userId)
.header(ACCEPT)
.header(AUTHORIZATION, BEARER + personProperty.getToken())
.execute(PersonJson.class);
final Optional<Person> person = execute
.map(json -> conversionService.convert(json, Person.class));
return person
.orElseThrow(() -> new ConvertException("Ошибка преобразования нового пользователя"));
}
).collect(Collectors.toList());
personService.createAll(newPersons);
}
}
}

View File

@ -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

View File

@ -12,8 +12,15 @@
<constraints nullable="false"/>
</column>
<column name="description" type="varchar(1000)"/>
<column name="created_date" type="datetime"/>
<column name="creator_id" type="int"/>
<column name="created_date" type="datetime">
<constraints nullable="false"/>
</column>
<column name="creator_id" type="int">
<constraints nullable="false"/>
</column>
<column name="web_url" type="varchar(300)">
<constraints nullable="false" unique="true"/>
</column>
</createTable>
</changeSet>
@ -27,7 +34,7 @@
</column>
<column name="name" type="varchar(100)"/>
<column name="web_url" type="varchar(300)">
<constraints nullable="false"/>
<constraints nullable="false" unique="true"/>
</column>
</createTable>
</changeSet>
@ -58,7 +65,7 @@
<constraints nullable="false"/>
</column>
<column name="web_url" type="varchar(300)">
<constraints nullable="false"/>
<constraints nullable="false" unique="true"/>
</column>
<column name="conflict" type="boolean">
<constraints nullable="false"/>
@ -71,6 +78,12 @@
<constraints nullable="false" foreignKeyName="merge_request_assignee_id_person_id"
references="person(id)"/>
</column>
<column name="source_branch" type="varchar(100)">
<constraints nullable="false"/>
</column>
<column name="target_branch" type="varchar(100)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>

View File

@ -73,6 +73,12 @@ public class MergeRequest implements BasicEntity<Long> {
@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")

View File

@ -37,6 +37,9 @@ public class Project implements BasicEntity<Long> {
private LocalDateTime createdDate;
@Column(name = "creator_id")
private Integer creatorId;
private Long creatorId;
@Column(name = "web_url")
private String webUrl;
}

View File

@ -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
);
}
}

View File

@ -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<String> labels;
@Builder
@ -22,10 +24,15 @@ public class NewPrNotify extends PrNotify {
String description,
String author,
String projectName,
Set<String> labels) {
String targetBranch,
String sourceBranch,
Set<String> 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
);
}

View File

@ -41,6 +41,7 @@ public enum Smile {
BUILD("♻️"),
SMART("\uD83E\uDDE0"),
SADLY("\uD83D\uDE14"),
TREE("\uD83C\uDF33"),
TOP("\uD83D\uDD1D");
@Getter

View File

@ -40,4 +40,6 @@ public class GitlabProperty {
private String userUrl;
private String usersUrl;
}

View File

@ -75,6 +75,8 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService<Merge
.description(newMergeRequest.getDescription())
.title(newMergeRequest.getTitle())
.url(newMergeRequest.getWebUrl())
.targetBranch(newMergeRequest.getTargetBranch())
.sourceBranch(newMergeRequest.getSourceBranch())
.build()
);
}

View File

@ -1,9 +1,14 @@
package org.sadtech.bot.gitlab.core.service.impl;
import lombok.NonNull;
import org.sadtech.bot.gitlab.context.domain.PersonInformation;
import org.sadtech.bot.gitlab.context.domain.entity.Project;
import org.sadtech.bot.gitlab.context.domain.notify.NewProjectNotify;
import org.sadtech.bot.gitlab.context.repository.ProjectRepository;
import org.sadtech.bot.gitlab.context.service.NotifyService;
import org.sadtech.bot.gitlab.context.service.PersonService;
import org.sadtech.bot.gitlab.context.service.ProjectService;
import org.sadtech.haiti.context.exception.NotFoundException;
import org.sadtech.haiti.context.repository.SimpleManagerRepository;
import org.sadtech.haiti.core.service.AbstractSimpleManagerService;
import org.springframework.stereotype.Service;
@ -17,15 +22,42 @@ import org.springframework.stereotype.Service;
public class ProjectServiceImpl extends AbstractSimpleManagerService<Project, Long> implements ProjectService {
private final ProjectRepository projectRepository;
private final NotifyService notifyService;
private final PersonService personService;
private final PersonInformation personInformation;
public ProjectServiceImpl(SimpleManagerRepository<Project, Long> repository, ProjectRepository projectRepository) {
public ProjectServiceImpl(
SimpleManagerRepository<Project, Long> 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

View File

@ -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<String> labels;
}

View File

@ -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;
}