Доработка уведомлений в тимсити

This commit is contained in:
upagge 2020-10-11 09:27:20 +03:00
parent 54492e551b
commit 464e34c1fd
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
33 changed files with 303 additions and 82 deletions

View File

@ -7,6 +7,7 @@ package org.sadtech.bot.vcs.bitbucket.app.scheduler;
*/
import lombok.RequiredArgsConstructor;
import org.sadtech.bot.vcs.core.domain.EntityType;
import org.sadtech.bot.vcs.core.domain.entity.Person;
import org.sadtech.bot.vcs.core.domain.notify.SimpleTextNotify;
import org.sadtech.bot.vcs.core.service.NotifyService;
@ -38,8 +39,9 @@ public class RatingScheduler {
final String message = ratingService.getRatingTop(person.getLogin());
notifyService.send(
SimpleTextNotify.builder()
.entityType(EntityType.PERSON)
.message(message)
.logins(Collections.singleton(person.getLogin()))
.recipients(Collections.singleton(person.getLogin()))
.build()
);
}

View File

@ -10,5 +10,6 @@
<include file="liquibase/v.2.0.0/2020-10-01-rating.xml"/>
<include file="liquibase/v.2.0.0/2020-10-02-add-column-status-teamcity.xml"/>
<include file="liquibase/v.2.0.0/2020-10-07-add-colum-reviewer-date.xml"/>
<include file="liquibase/v.2.0.0/2020-10-11-teamcity-refactoring.xml"/>
</databaseChangeLog>

View File

@ -0,0 +1,40 @@
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="2020-10-11-drop-column-teamcity-setting-chat-id" author="upagge">
<dropColumn tableName="teamcity_setting" columnName="chat_id"/>
</changeSet>
<changeSet id="2020-10-11-create-table-chat" author="upagge">
<createTable tableName="chat">
<column name="key" type="varchar(64)">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="telegram_id" type="int">
<constraints unique="true" nullable="false"/>
</column>
<column name="description" type="varchar(200)"/>
</createTable>
</changeSet>
<changeSet id="2020-10-11-add-column-teamcity" author="upagge">
<addColumn tableName="teamcity_setting">
<column name="build_type_id" type="varchar(200)"/>
</addColumn>
<addColumn tableName="teamcity_setting">
<column name="recipient_id" type="varchar(64)">
<constraints nullable="false"/>
</column>
</addColumn>
<addColumn tableName="teamcity_setting">
<column name="recipient_type" type="varchar(20)">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
</databaseChangeLog>

View File

@ -1,13 +1,13 @@
package org.sadtech.bot.vcs.core.domain.notify;
package org.sadtech.bot.vcs.core.domain;
/**
* // TODO: 21.09.2020 Добавить описание.
*
* @author upagge 21.09.2020
*/
public enum TypeNotify {
public enum EntityType {
SERVICE,
CHAT,
PERSON
}

View File

@ -0,0 +1,32 @@
package org.sadtech.bot.vcs.core.domain.entity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* // TODO: 11.10.2020 Добавить описание.
*
* @author upagge 11.10.2020
*/
@Getter
@Setter
@Entity
@Table(name = "chat")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Chat {
@Id
@Column(name = "key")
@EqualsAndHashCode.Include
private String key;
@Column(name = "telegram_id")
private Long telegramId;
}

View File

@ -2,6 +2,7 @@ package org.sadtech.bot.vcs.core.domain.notify;
import lombok.Builder;
import lombok.Getter;
import org.sadtech.bot.vcs.core.domain.EntityType;
import org.sadtech.bot.vcs.core.domain.entity.PullRequest;
import org.sadtech.bot.vcs.core.utils.MessageUtils;
import org.sadtech.bot.vcs.core.utils.Smile;
@ -27,11 +28,11 @@ public class GoodMorningNotify extends Notify {
@Builder
protected GoodMorningNotify(
Set<String> logins,
Set<String> recipients,
List<PullRequest> pullRequestsReviews,
List<PullRequest> pullRequestsNeedWork
) {
super(logins);
super(EntityType.PERSON, recipients);
this.pullRequestsReviews = pullRequestsReviews;
this.pullRequestsNeedWork = pullRequestsNeedWork;
}

View File

@ -2,6 +2,7 @@ package org.sadtech.bot.vcs.core.domain.notify;
import lombok.Getter;
import lombok.Setter;
import org.sadtech.bot.vcs.core.domain.EntityType;
import java.util.Set;
@ -9,12 +10,12 @@ import java.util.Set;
@Setter
public abstract class Notify {
protected TypeNotify typeNotify;
protected Set<String> logins;
protected EntityType entityType;
protected Set<String> recipients;
protected Notify(Set<String> logins) {
this.typeNotify = TypeNotify.PERSON;
this.logins = logins;
protected Notify(EntityType entityType, Set<String> recipients) {
this.entityType = entityType;
this.recipients = recipients;
}
public abstract String generateMessage();

View File

@ -2,6 +2,7 @@ package org.sadtech.bot.vcs.core.domain.notify;
import lombok.Builder;
import lombok.Getter;
import org.sadtech.bot.vcs.core.domain.EntityType;
import java.util.Set;
@ -16,8 +17,8 @@ public class SimpleTextNotify extends Notify {
private final String message;
@Builder
private SimpleTextNotify(Set<String> logins, String message) {
super(logins);
private SimpleTextNotify(EntityType entityType, Set<String> recipients, String message) {
super(entityType, recipients);
this.message = message;
}
@ -25,4 +26,5 @@ public class SimpleTextNotify extends Notify {
public String generateMessage() {
return message;
}
}

View File

@ -4,6 +4,7 @@ import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.sadtech.bot.vcs.core.domain.Answer;
import org.sadtech.bot.vcs.core.domain.EntityType;
import org.sadtech.bot.vcs.core.domain.notify.Notify;
import org.sadtech.bot.vcs.core.utils.Smile;
@ -22,12 +23,12 @@ public class AnswerCommentNotify extends Notify {
@Builder
protected AnswerCommentNotify(
Set<String> logins,
Set<String> recipients,
String youMessage,
String url,
List<Answer> answers
) {
super(logins);
super(EntityType.PERSON, recipients);
this.youMessage = youMessage;
this.url = url;
this.answers = answers;

View File

@ -3,6 +3,7 @@ package org.sadtech.bot.vcs.core.domain.notify.comment;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.sadtech.bot.vcs.core.domain.EntityType;
import org.sadtech.bot.vcs.core.domain.notify.Notify;
import org.sadtech.bot.vcs.core.utils.Smile;
@ -19,12 +20,12 @@ public class CommentNotify extends Notify {
@Builder
private CommentNotify(
Set<String> logins,
Set<String> recipients,
String url,
String authorName,
String message
) {
super(logins);
super(EntityType.PERSON, recipients);
this.authorName = authorName;
this.message = message;
this.url = url;

View File

@ -12,11 +12,11 @@ public class ConflictPrNotify extends PrNotify {
@Builder
private ConflictPrNotify(
Set<String> logins,
Set<String> recipients,
String name,
String url
) {
super(logins, name, url);
super(recipients, name, url);
}
@Override

View File

@ -17,12 +17,12 @@ public class NewPrNotify extends PrNotify {
@Builder
private NewPrNotify(
Set<String> logins,
Set<String> recipients,
String title,
String url,
String description,
String author) {
super(logins, title, url);
super(recipients, title, url);
this.description = description;
this.author = author;
}

View File

@ -2,6 +2,7 @@ package org.sadtech.bot.vcs.core.domain.notify.pullrequest;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.sadtech.bot.vcs.core.domain.EntityType;
import org.sadtech.bot.vcs.core.domain.notify.Notify;
import java.util.Set;
@ -13,8 +14,8 @@ public abstract class PrNotify extends Notify {
protected final String title;
protected final String url;
protected PrNotify(Set<String> logins, String title, String url) {
super(logins);
protected PrNotify(Set<String> recipients, String title, String url) {
super(EntityType.PERSON, recipients);
this.title = title;
this.url = url;
}

View File

@ -23,11 +23,11 @@ public class ReviewersPrNotify extends PrNotify {
@Builder
private ReviewersPrNotify(
Set<String> logins,
Set<String> recipients,
String title,
String url,
List<ReviewerChange> reviewerChanges) {
super(logins, title, url);
super(recipients, title, url);
this.reviewerChanges = reviewerChanges;
}

View File

@ -18,12 +18,12 @@ public class StatusPrNotify extends PrNotify {
@Builder
private StatusPrNotify(
Set<String> logins,
Set<String> recipients,
String name,
String url,
PullRequestStatus oldStatus,
PullRequestStatus newStatus) {
super(logins, name, url);
super(recipients, name, url);
this.oldStatus = oldStatus;
this.newStatus = newStatus;
}

View File

@ -16,10 +16,10 @@ public class UpdatePrNotify extends PrNotify {
@Builder
private UpdatePrNotify(
Set<String> logins,
Set<String> recipients,
String name,
String url, String author) {
super(logins, name, url);
super(recipients, name, url);
this.author = author;
}

View File

@ -15,12 +15,12 @@ public class TaskCloseNotify extends TaskNotify {
@Builder
protected TaskCloseNotify(
Set<String> logins,
Set<String> recipients,
String authorName,
String url,
String messageTask
) {
super(logins, authorName, url, messageTask);
super(recipients, authorName, url, messageTask);
}
@Override

View File

@ -17,12 +17,12 @@ public class TaskNewNotify extends TaskNotify {
@Builder
protected TaskNewNotify(
Set<String> logins,
Set<String> recipients,
String authorName,
String url,
String messageTask
) {
super(logins, authorName, url, messageTask);
super(recipients, authorName, url, messageTask);
}
@Override

View File

@ -2,6 +2,7 @@ package org.sadtech.bot.vcs.core.domain.notify.task;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.sadtech.bot.vcs.core.domain.EntityType;
import org.sadtech.bot.vcs.core.domain.notify.Notify;
import java.util.Set;
@ -15,12 +16,12 @@ public abstract class TaskNotify extends Notify {
protected final String messageTask;
protected TaskNotify(
Set<String> logins,
Set<String> recipients,
String authorName,
String url,
String messageTask
) {
super(logins);
super(EntityType.PERSON, recipients);
this.authorName = authorName;
this.url = url;
this.messageTask = messageTask;

View File

@ -0,0 +1,18 @@
package org.sadtech.bot.vcs.core.repository;
import lombok.NonNull;
import org.sadtech.basic.context.repository.SimpleManagerRepository;
import org.sadtech.bot.vcs.core.domain.entity.Chat;
import java.util.Set;
/**
* // TODO: 11.10.2020 Добавить описание.
*
* @author upagge 11.10.2020
*/
public interface ChatRepository extends SimpleManagerRepository<Chat, String> {
Set<Long> findAllTelegramIdByKey(@NonNull Set<String> keys);
}

View File

@ -0,0 +1,32 @@
package org.sadtech.bot.vcs.core.repository.impl;
import lombok.NonNull;
import org.sadtech.basic.database.repository.manager.AbstractSimpleManagerRepository;
import org.sadtech.bot.vcs.core.domain.entity.Chat;
import org.sadtech.bot.vcs.core.repository.ChatRepository;
import org.sadtech.bot.vcs.core.repository.jpa.ChatJpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Set;
/**
* // TODO: 11.10.2020 Добавить описание.
*
* @author upagge 11.10.2020
*/
@Repository
public class ChatRepositoryImpl extends AbstractSimpleManagerRepository<Chat, String> implements ChatRepository {
private final ChatJpaRepository jpaRepository;
public ChatRepositoryImpl(ChatJpaRepository jpaRepository) {
super(jpaRepository);
this.jpaRepository = jpaRepository;
}
@Override
public Set<Long> findAllTelegramIdByKey(@NonNull Set<String> keys) {
return jpaRepository.findAllTelegramIdByKey(keys);
}
}

View File

@ -0,0 +1,20 @@
package org.sadtech.bot.vcs.core.repository.jpa;
import org.sadtech.bot.vcs.core.domain.entity.Chat;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Set;
/**
* // TODO: 11.10.2020 Добавить описание.
*
* @author upagge 11.10.2020
*/
public interface ChatJpaRepository extends JpaRepository<Chat, String> {
@Query("SELECT c.telegramId FROM Chat c WHERE c.key IN :keys AND c.telegramId IS NOT NULL")
Set<Long> findAllTelegramIdByKey(@Param("keys") Set<String> keys);
}

View File

@ -1,6 +1,7 @@
package org.sadtech.bot.vcs.core.scheduler;
import lombok.RequiredArgsConstructor;
import org.sadtech.bot.vcs.core.domain.EntityType;
import org.sadtech.bot.vcs.core.domain.PullRequestStatus;
import org.sadtech.bot.vcs.core.domain.ReviewerStatus;
import org.sadtech.bot.vcs.core.domain.entity.Person;
@ -49,7 +50,7 @@ public class NotificationScheduler {
GoodMorningNotify.builder()
.pullRequestsNeedWork(pullRequestsNeedWork)
.pullRequestsReviews(pullRequestsReviews)
.logins(Collections.singleton(user.getLogin()))
.recipients(Collections.singleton(user.getLogin()))
.build()
);
}
@ -63,7 +64,7 @@ public class NotificationScheduler {
notifyService.send(
SimpleTextNotify
.builder()
.logins(
.recipients(
usersTks.stream()
.map(Person::getLogin)
.collect(Collectors.toSet())
@ -79,9 +80,10 @@ public class NotificationScheduler {
List<Person> allRegister = personService.getAllRegister();
notifyService.send(
SimpleTextNotify.builder()
.entityType(EntityType.PERSON)
.message("Ну вот и все! Веселых выходных " + Smile.MIG + Smile.BR +
"До понедельника" + Smile.BUY + Smile.TWO_BR)
.logins(
.recipients(
allRegister.stream()
.map(Person::getLogin)
.collect(Collectors.toSet())

View File

@ -0,0 +1,16 @@
package org.sadtech.bot.vcs.core.service;
import lombok.NonNull;
import java.util.Set;
/**
* // TODO: 11.10.2020 Добавить описание.
*
* @author upagge 11.10.2020
*/
public interface ChatService {
Set<Long> getAllTelegramIdByKey(@NonNull Set<String> keys);
}

View File

@ -0,0 +1,27 @@
package org.sadtech.bot.vcs.core.service.impl;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.bot.vcs.core.repository.ChatRepository;
import org.sadtech.bot.vcs.core.service.ChatService;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* // TODO: 11.10.2020 Добавить описание.
*
* @author upagge 11.10.2020
*/
@Service
@RequiredArgsConstructor
public class ChatServiceImpl implements ChatService {
private final ChatRepository chatRepository;
@Override
public Set<Long> getAllTelegramIdByKey(@NonNull Set<String> keys) {
return chatRepository.findAllTelegramIdByKey(keys);
}
}

View File

@ -90,7 +90,7 @@ public class CommentServiceImpl extends AbstractSimpleManagerService<Comment, Lo
CommentNotify.builder()
.authorName(comment.getAuthor())
.url(comment.getUrl())
.logins(recipientsLogins)
.recipients(recipientsLogins)
.message(comment.getMessage())
.build()
);
@ -142,7 +142,7 @@ public class CommentServiceImpl extends AbstractSimpleManagerService<Comment, Lo
oldComment.setAnswers(existsNewAnswersIds);
notifyService.send(
AnswerCommentNotify.builder()
.logins(Collections.singleton(newComment.getAuthor()))
.recipients(Collections.singleton(newComment.getAuthor()))
.url(oldComment.getUrl())
.youMessage(newComment.getMessage())
.answers(

View File

@ -2,6 +2,7 @@ package org.sadtech.bot.vcs.core.service.impl;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.bot.vcs.core.domain.EntityType;
import org.sadtech.bot.vcs.core.domain.entity.NotifySetting;
import org.sadtech.bot.vcs.core.domain.notify.Notify;
import org.sadtech.bot.vcs.core.repository.NotifySettingRepository;
@ -22,8 +23,10 @@ public class NotifyServiceImpl implements NotifyService {
@Override
public <T extends Notify> void send(T notify) {
final Set<String> recipientLogins = settingRepository.isNotification(notify.getLogins());
notify.setLogins(recipientLogins);
if (EntityType.PERSON.equals(notify.getEntityType())) {
final Set<String> recipientLogins = settingRepository.isNotification(notify.getRecipients());
notify.setRecipients(recipientLogins);
}
messageSendService.send(notify);
}

View File

@ -78,7 +78,7 @@ public class PullRequestsServiceImpl extends AbstractSimpleManagerService<PullRe
.description(newPullRequest.getDescription())
.title(newPullRequest.getTitle())
.url(newPullRequest.getUrl())
.logins(
.recipients(
newPullRequest.getReviewers().stream()
.map(Reviewer::getPersonLogin)
.collect(Collectors.toSet())
@ -106,7 +106,7 @@ public class PullRequestsServiceImpl extends AbstractSimpleManagerService<PullRe
UpdatePrNotify.builder()
.author(oldPullRequest.getAuthorLogin())
.name(newPullRequest.getTitle())
.logins(
.recipients(
newPullRequest.getReviewers().stream()
.map(Reviewer::getPersonLogin)
.collect(Collectors.toSet())
@ -125,7 +125,7 @@ public class PullRequestsServiceImpl extends AbstractSimpleManagerService<PullRe
ConflictPrNotify.builder()
.name(pullRequest.getTitle())
.url(pullRequest.getUrl())
.logins(Collections.singleton(pullRequest.getAuthorLogin()))
.recipients(Collections.singleton(pullRequest.getAuthorLogin()))
.build()
);
}
@ -143,7 +143,7 @@ public class PullRequestsServiceImpl extends AbstractSimpleManagerService<PullRe
.url(oldPullRequest.getUrl())
.newStatus(newStatus)
.oldStatus(oldStatus)
.logins(Collections.singleton(oldPullRequest.getAuthorLogin()))
.recipients(Collections.singleton(oldPullRequest.getAuthorLogin()))
.build()
);
oldPullRequest.setStatus(newStatus);
@ -202,7 +202,7 @@ public class PullRequestsServiceImpl extends AbstractSimpleManagerService<PullRe
ReviewersPrNotify.builder()
.title(newPullRequest.getTitle())
.url(newPullRequest.getUrl())
.logins(Collections.singleton(newPullRequest.getAuthorLogin()))
.recipients(Collections.singleton(newPullRequest.getAuthorLogin()))
.reviewerChanges(reviewerChanges)
.build()
);

View File

@ -100,7 +100,7 @@ public class TaskServiceImpl extends AbstractSimpleManagerService<Task, Long> im
.messageTask(task.getDescription())
.authorName(oldTask.getAuthor())
.url(oldTask.getUrl())
.logins(Collections.singleton(oldTask.getResponsible()))
.recipients(Collections.singleton(oldTask.getResponsible()))
.build()
);
break;
@ -110,7 +110,7 @@ public class TaskServiceImpl extends AbstractSimpleManagerService<Task, Long> im
.messageTask(oldTask.getDescription())
.authorName(oldTask.getAuthor())
.url(oldTask.getUrl())
.logins(Collections.singleton(oldTask.getAuthor()))
.recipients(Collections.singleton(oldTask.getAuthor()))
.build()
);
break;
@ -134,7 +134,7 @@ public class TaskServiceImpl extends AbstractSimpleManagerService<Task, Long> im
oldTask.setAnswers(existsNewAnswersIds);
notifyService.send(
AnswerCommentNotify.builder()
.logins(Collections.singleton(oldTask.getAuthor()))
.recipients(Collections.singleton(oldTask.getAuthor()))
.url(oldTask.getUrl())
.youMessage(oldTask.getDescription())
.answers(
@ -181,7 +181,7 @@ public class TaskServiceImpl extends AbstractSimpleManagerService<Task, Long> im
.authorName(task.getAuthor())
.messageTask(task.getDescription())
.url(task.getUrl())
.logins(Collections.singleton(pullRequest.getAuthorLogin()))
.recipients(Collections.singleton(pullRequest.getAuthorLogin()))
.build()
);
}
@ -197,7 +197,7 @@ public class TaskServiceImpl extends AbstractSimpleManagerService<Task, Long> im
CommentNotify.builder()
.authorName(task.getAuthor())
.url(task.getUrl())
.logins(recipientsLogins)
.recipients(recipientsLogins)
.message(task.getDescription())
.build()
);

View File

@ -2,14 +2,14 @@ package org.sadtech.bot.vcs.teamcity.core.domain;
import lombok.Builder;
import lombok.Getter;
import org.sadtech.bot.vcs.core.domain.EntityType;
import org.sadtech.bot.vcs.core.domain.notify.Notify;
import org.sadtech.bot.vcs.core.domain.notify.TypeNotify;
import org.sadtech.bot.vcs.core.utils.Smile;
import org.sadtech.bot.vcs.teamcity.core.domain.entity.BuildShort;
import org.sadtech.bot.vcs.teamcity.sdk.BuildStatus;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.Set;
/**
* // TODO: 21.09.2020 Добавить описание.
@ -17,17 +17,14 @@ import java.util.Collections;
* @author upagge 21.09.2020
*/
@Getter
public class ServiceNotify extends Notify {
public class TeamcityBuildNotify extends Notify {
private final Long chatId;
private final BuildShort buildShort;
@Builder
private ServiceNotify(Long chatId, BuildShort buildShort) {
super(Collections.emptySet());
this.chatId = chatId;
private TeamcityBuildNotify(EntityType entityType, Set<String> recipients, BuildShort buildShort) {
super(entityType, recipients);
this.buildShort = buildShort;
this.typeNotify = TypeNotify.SERVICE;
}
@Override

View File

@ -3,9 +3,12 @@ package org.sadtech.bot.vcs.teamcity.core.domain.entity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.sadtech.bot.vcs.core.domain.EntityType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@ -29,12 +32,19 @@ public class TeamcitySetting {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "chat_id")
private Long chatId;
@Column(name = "recipient_id")
private String recipientId;
@Enumerated(EnumType.STRING)
@Column(name = "recipient_type")
private EntityType recipientType;
@Column(name = "project_id")
private String projectId;
@Column(name = "build_type_id")
private String buildTypeId;
@Column(name = "success")
private boolean success;

View File

@ -3,7 +3,7 @@ package org.sadtech.bot.vcs.teamcity.core.service.impl;
import lombok.NonNull;
import org.sadtech.basic.core.service.AbstractSimpleManagerService;
import org.sadtech.bot.vcs.core.service.NotifyService;
import org.sadtech.bot.vcs.teamcity.core.domain.ServiceNotify;
import org.sadtech.bot.vcs.teamcity.core.domain.TeamcityBuildNotify;
import org.sadtech.bot.vcs.teamcity.core.domain.entity.BuildShort;
import org.sadtech.bot.vcs.teamcity.core.domain.entity.TeamcitySetting;
import org.sadtech.bot.vcs.teamcity.core.repository.BuildShortRepository;
@ -12,6 +12,7 @@ import org.sadtech.bot.vcs.teamcity.core.service.TeamcitySettingService;
import org.sadtech.bot.vcs.teamcity.sdk.BuildStatus;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.Set;
/**
@ -49,19 +50,27 @@ public class BuildShortServiceImpl extends AbstractSimpleManagerService<BuildSho
}
private void sendNotification(TeamcitySetting teamcitySetting, BuildShort buildShort) {
if (
(teamcitySetting.isFailure() && BuildStatus.FAILURE.equals(buildShort.getStatus()))
|| (teamcitySetting.isSuccess() && BuildStatus.SUCCESS.equals(buildShort.getStatus()))
) {
if (isStatusBuild(teamcitySetting, buildShort.getStatus()) && isTypeBuild(teamcitySetting, buildShort.getBuildTypeId())) {
notifyService.send(
ServiceNotify.builder()
TeamcityBuildNotify.builder()
.entityType(teamcitySetting.getRecipientType())
.recipients(Collections.singleton(teamcitySetting.getRecipientId()))
.buildShort(buildShort)
.chatId(teamcitySetting.getChatId())
.build()
);
}
}
private boolean isTypeBuild(TeamcitySetting teamcitySetting, String buildTypeId) {
return teamcitySetting.getBuildTypeId() == null
|| (teamcitySetting.getBuildTypeId().equals(buildTypeId));
}
private boolean isStatusBuild(TeamcitySetting teamcitySetting, BuildStatus buildStatus) {
return (teamcitySetting.isFailure() && BuildStatus.FAILURE.equals(buildStatus))
|| (teamcitySetting.isSuccess() && BuildStatus.SUCCESS.equals(buildStatus));
}
@Override
public BuildShort update(@NonNull BuildShort buildShort) {
return buildShortRepository.save(buildShort);

View File

@ -3,9 +3,10 @@ package org.sadtech.bot.vcs.telegram.service;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.bot.vcs.core.domain.notify.Notify;
import org.sadtech.bot.vcs.core.exception.NotFoundException;
import org.sadtech.bot.vcs.core.service.ChatService;
import org.sadtech.bot.vcs.core.service.MessageSendService;
import org.sadtech.bot.vcs.core.service.PersonService;
import org.sadtech.bot.vcs.teamcity.core.domain.ServiceNotify;
import org.sadtech.social.core.domain.BoxAnswer;
import org.sadtech.social.core.service.sender.Sending;
import org.springframework.stereotype.Service;
@ -24,22 +25,25 @@ public class MessageSendTelegramService implements MessageSendService {
private final Sending sending;
private final PersonService personService;
private final ChatService chatService;
@Override
public void send(@NonNull Notify notify) {
switch (notify.getTypeNotify()) {
case PERSON:
final Set<Long> telegramIds = personService.getAllTelegramIdByLogin(notify.getLogins());
telegramIds.forEach(
telegramId -> sending.send(telegramId, BoxAnswer.of(notify.generateMessage()))
);
break;
case SERVICE:
ServiceNotify serviceNotify = (ServiceNotify) notify;
sending.send(serviceNotify.getChatId(), BoxAnswer.of(notify.generateMessage()));
break;
}
final Set<Long> telegramIds = getTelegramIds(notify);
telegramIds.forEach(
telegramId -> sending.send(telegramId, BoxAnswer.of(notify.generateMessage()))
);
}
private Set<Long> getTelegramIds(Notify notify) {
switch (notify.getEntityType()) {
case PERSON:
return personService.getAllTelegramIdByLogin(notify.getRecipients());
case CHAT:
return chatService.getAllTelegramIdByKey(notify.getRecipients());
default:
throw new NotFoundException("Отправка сообщения этому типу не возможна");
}
}
}