Добавлено уведомление об отсутствии ревьюверов

This commit is contained in:
Struchkov Mark 2024-09-03 23:51:44 +03:00
parent e5727f2b30
commit 07b5687cf2
No known key found for this signature in database
GPG Key ID: A3F0AC3F0FA52F3C
8 changed files with 194 additions and 29 deletions

View File

@ -0,0 +1,36 @@
package dev.struchkov.bot.gitlab.context.domain.notify.group.mr;
import dev.struchkov.bot.gitlab.context.domain.notify.GroupNotify;
import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames;
import lombok.Getter;
import static dev.struchkov.bot.gitlab.context.domain.notify.group.mr.ConflictMrGroupNotifyFields.CLASS_NAME;
@Getter
@FieldNames
public class ConflictMrGroupNotify implements GroupNotify {
public static final String TYPE = CLASS_NAME;
protected final String sourceBranch;
protected final String projectName;
protected final String title;
protected final String url;
protected final String milestone;
protected final String authorTelegramUsername;
public ConflictMrGroupNotify(String sourceBranch, String projectName, String title, String url, String milestone, String authorTelegramUsername) {
this.sourceBranch = sourceBranch;
this.projectName = projectName;
this.title = title;
this.url = url;
this.milestone = milestone;
this.authorTelegramUsername = authorTelegramUsername;
}
@Override
public String getType() {
return TYPE;
}
}

View File

@ -12,7 +12,7 @@ import static dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NewMergeRe
@Getter @Getter
@FieldNames @FieldNames
public class NewMergeRequestGroupPersonalNotify implements GroupNotify { public class NewMergeRequestGroupNotify implements GroupNotify {
public static final String TYPE = CLASS_NAME; public static final String TYPE = CLASS_NAME;
@ -29,7 +29,7 @@ public class NewMergeRequestGroupPersonalNotify implements GroupNotify {
protected final Person assignee; protected final Person assignee;
@Builder @Builder
public NewMergeRequestGroupPersonalNotify(Long mrId, String projectName, String title, String url, String milestone, String description, String author, String targetBranch, String sourceBranch, Map<Long, String> reviewerTelegramUsernames, Person assignee) { public NewMergeRequestGroupNotify(Long mrId, String projectName, String title, String url, String milestone, String description, String author, String targetBranch, String sourceBranch, Map<Long, String> reviewerTelegramUsernames, Person assignee) {
this.mrId = mrId; this.mrId = mrId;
this.projectName = projectName; this.projectName = projectName;
this.title = title; this.title = title;

View File

@ -0,0 +1,41 @@
package dev.struchkov.bot.gitlab.context.domain.notify.group.mr;
import dev.struchkov.bot.gitlab.context.domain.entity.Person;
import dev.struchkov.bot.gitlab.context.domain.notify.GroupNotify;
import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames;
import lombok.Builder;
import lombok.Getter;
@Getter
@FieldNames
public class NoReviewerGroupNotify implements GroupNotify {
public static final String TYPE = "NoReviewersGroupNotify";
protected final String projectName;
protected final String title;
protected final String url;
protected final String milestone;
protected final String targetBranch;
protected final String sourceBranch;
protected final Person assignee;
protected final String ownerTelegramUsername;
@Builder
public NoReviewerGroupNotify(String projectName, String title, String url, String milestone, String targetBranch, String sourceBranch, Person assignee, String ownerTelegramUsername) {
this.projectName = projectName;
this.title = title;
this.url = url;
this.milestone = milestone;
this.targetBranch = targetBranch;
this.sourceBranch = sourceBranch;
this.assignee = assignee;
this.ownerTelegramUsername = ownerTelegramUsername;
}
@Override
public String getType() {
return TYPE;
}
}

View File

@ -6,11 +6,13 @@ import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import static dev.struchkov.bot.gitlab.context.domain.notify.group.pipeline.PipelineGroupNotifyFields.CLASS_NAME;
@Getter @Getter
@FieldNames @FieldNames
public class PipelineGroupNotify implements GroupNotify { public class PipelineGroupNotify implements GroupNotify {
public static final String TYPE = "PipelineGroupNotify"; public static final String TYPE = CLASS_NAME;
private final String projectName; private final String projectName;
private final String refName; private final String refName;

View File

@ -11,7 +11,8 @@ import dev.struchkov.bot.gitlab.context.domain.entity.Person;
import dev.struchkov.bot.gitlab.context.domain.entity.Project; import dev.struchkov.bot.gitlab.context.domain.entity.Project;
import dev.struchkov.bot.gitlab.context.domain.event.NewMergeRequestEvent; import dev.struchkov.bot.gitlab.context.domain.event.NewMergeRequestEvent;
import dev.struchkov.bot.gitlab.context.domain.event.UpdateMergeRequestEvent; import dev.struchkov.bot.gitlab.context.domain.event.UpdateMergeRequestEvent;
import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NewMergeRequestGroupPersonalNotify; import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NewMergeRequestGroupNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NoReviewerGroupNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ApprovalChangedMrPersonalNotify; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ApprovalChangedMrPersonalNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictMrPersonalNotify; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictMrPersonalNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictResolveMrPersonalNotify; import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictResolveMrPersonalNotify;
@ -56,32 +57,53 @@ public class MergeRequestHandler {
final boolean userReviewer = mergeRequest.isUserReviewer(); final boolean userReviewer = mergeRequest.isUserReviewer();
final boolean userAssignee = mergeRequest.isUserAssignee(); final boolean userAssignee = mergeRequest.isUserAssignee();
if (!mergeRequest.isConflict()) { if (mergeRequest.isConflict()) {
} else {
if (mergeRequest.isNotification()) { if (mergeRequest.isNotification()) {
if (userReviewer || userAssignee) { if (userReviewer || userAssignee) {
if (userReviewer) sendNotifyNewMrReview(mergeRequest, projectName); if (userReviewer) sendNotifyNewMrReview(mergeRequest, projectName);
if (userAssignee) sendNotifyNewAssignee(mergeRequest, projectName, null); if (userAssignee) sendNotifyNewAssignee(mergeRequest, projectName, null);
} }
} }
final Map<Long, String> reviewerTelegramUsernames = personService.getTelegramUsernamesByPersonIds(mergeRequest.getReviewers().stream().map(Person::getId).collect(Collectors.toSet()));
if (!reviewerTelegramUsernames.isEmpty()) { if (checkNotEmpty(mergeRequest.getReviewers())) {
notifyService.send( final Map<Long, String> reviewerTelegramUsernames = personService.getTelegramUsernamesByPersonIds(mergeRequest.getReviewers().stream().map(Person::getId).collect(Collectors.toSet()));
NewMergeRequestGroupPersonalNotify.builder() if (!reviewerTelegramUsernames.isEmpty()) {
.mrId(mergeRequest.getId()) notifyService.send(
.title(mergeRequest.getTitle()) NewMergeRequestGroupNotify.builder()
.url(mergeRequest.getWebUrl()) .mrId(mergeRequest.getId())
.author(mergeRequest.getAuthor().getName()) .title(mergeRequest.getTitle())
.reviewerTelegramUsernames(reviewerTelegramUsernames) .url(mergeRequest.getWebUrl())
.milestone(mergeRequest.getMilestone()) .author(mergeRequest.getAuthor().getName())
.projectName(projectName) .reviewerTelegramUsernames(reviewerTelegramUsernames)
.targetBranch(mergeRequest.getTargetBranch()) .milestone(mergeRequest.getMilestone())
.sourceBranch(mergeRequest.getSourceBranch()) .projectName(projectName)
.description(mergeRequest.getDescription()) .targetBranch(mergeRequest.getTargetBranch())
.build() .sourceBranch(mergeRequest.getSourceBranch())
); .description(mergeRequest.getDescription())
.build()
);
}
} else {
final Optional<String> optAuthorUserName = personService.getTelegramUsernamesByPersonIds(mergeRequest.getAuthor().getId());
if (optAuthorUserName.isPresent()) {
final String authorUsername = optAuthorUserName.get();
notifyService.send(
NoReviewerGroupNotify.builder()
.ownerTelegramUsername(authorUsername)
.title(mergeRequest.getTitle())
.url(mergeRequest.getWebUrl())
.milestone(mergeRequest.getMilestone())
.projectName(projectName)
.targetBranch(mergeRequest.getTargetBranch())
.sourceBranch(mergeRequest.getSourceBranch())
.build()
);
}
} }
} else {
//TODO [03.09.2024|uPagge]: Уведомление о конфликте
} }
} }

View File

@ -46,8 +46,8 @@ gitlab-bot:
person: person:
telegram-id: ${TELEGRAM_PERSON_ID} telegram-id: ${TELEGRAM_PERSON_ID}
group-notify: group-notify:
chat-id: "-1002233809566" chat-id: ${TELEGRAM_CHAT_ID:}
thread-id: "2" thread-id: ${TELEGRAM_THREAD_ID:}
gitlab-sdk: gitlab-sdk:
access-token: ${GITLAB_PERSONAL_TOKEN} access-token: ${GITLAB_PERSONAL_TOKEN}
base-url: ${GITLAB_URL} base-url: ${GITLAB_URL}

View File

@ -1,6 +1,6 @@
package dev.struchkov.bot.gitlab.telegram.service.notify.group; package dev.struchkov.bot.gitlab.telegram.service.notify.group;
import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NewMergeRequestGroupPersonalNotify; import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NewMergeRequestGroupNotify;
import dev.struchkov.bot.gitlab.context.utils.Icons; import dev.struchkov.bot.gitlab.context.utils.Icons;
import dev.struchkov.bot.gitlab.telegram.service.notify.NotifyBoxAnswerGenerator; import dev.struchkov.bot.gitlab.telegram.service.notify.NotifyBoxAnswerGenerator;
import dev.struchkov.godfather.simple.domain.BoxAnswer; import dev.struchkov.godfather.simple.domain.BoxAnswer;
@ -16,10 +16,10 @@ import static dev.struchkov.haiti.utils.Strings.escapeMarkdown;
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
public class NewMergeRequestGroupNotifyGenerator implements NotifyBoxAnswerGenerator<NewMergeRequestGroupPersonalNotify> { public class NewMergeRequestGroupNotifyGenerator implements NotifyBoxAnswerGenerator<NewMergeRequestGroupNotify> {
@Override @Override
public BoxAnswer generate(NewMergeRequestGroupPersonalNotify notify) { public BoxAnswer generate(NewMergeRequestGroupNotify notify) {
final String reviewerUsernames = String.join(", ", notify.getReviewerTelegramUsernames().values()); final String reviewerUsernames = String.join(", ", notify.getReviewerTelegramUsernames().values());
final StringBuilder builder = new StringBuilder(Icons.FUN) final StringBuilder builder = new StringBuilder(Icons.FUN)
.append("*You are reviewers:* ").append(reviewerUsernames) .append("*You are reviewers:* ").append(reviewerUsernames)
@ -59,7 +59,7 @@ public class NewMergeRequestGroupNotifyGenerator implements NotifyBoxAnswerGener
@Override @Override
public String getNotifyType() { public String getNotifyType() {
return NewMergeRequestGroupPersonalNotify.TYPE; return NewMergeRequestGroupNotify.TYPE;
} }
} }

View File

@ -0,0 +1,64 @@
package dev.struchkov.bot.gitlab.telegram.service.notify.group;
import dev.struchkov.bot.gitlab.context.domain.notify.group.mr.NoReviewerGroupNotify;
import dev.struchkov.bot.gitlab.context.utils.Icons;
import dev.struchkov.bot.gitlab.telegram.service.notify.NotifyBoxAnswerGenerator;
import dev.struchkov.godfather.simple.domain.BoxAnswer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import static dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard;
import static dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine.keyBoardLine;
import static dev.struchkov.godfather.telegram.domain.keyboard.button.UrlButton.urlButton;
import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.ENABLE_MARKDOWN;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.Strings.escapeMarkdown;
@Component
@RequiredArgsConstructor
public class NoReviewerGroupNotifyGenerator implements NotifyBoxAnswerGenerator<NoReviewerGroupNotify> {
@Override
public BoxAnswer generate(NoReviewerGroupNotify notify) {
final StringBuilder builder = new StringBuilder(Icons.DANGEROUS)
.append(notify.getOwnerTelegramUsername())
.append(", *your merge request does not have reviewers*")
.append(Icons.HR)
.append(escapeMarkdown(notify.getTitle()));
builder.append(Icons.HR);
if (checkNotNull(notify.getProjectName())) {
builder.append(Icons.PROJECT).append(": ").append(escapeMarkdown(notify.getProjectName())).append("\n");
}
if (checkNotNull(notify.getMilestone())) {
builder.append(Icons.MILESTONE).append(": ").append(notify.getMilestone()).append("\n");
}
builder
.append(Icons.TREE).append(": ").append(escapeMarkdown(notify.getSourceBranch())).append(Icons.ARROW).append(escapeMarkdown(notify.getTargetBranch())).append("\n");
if (checkNotNull(notify.getAssignee())) {
builder.append(Icons.ASSIGNEE).append(": ").append(notify.getAssignee());
}
final String notifyMessage = builder.toString();
return BoxAnswer.builder()
.message(notifyMessage)
.keyBoard(inlineKeyBoard(
keyBoardLine(
urlButton(Icons.LINK, notify.getUrl())
)
))
.payload(ENABLE_MARKDOWN)
.build();
}
@Override
public String getNotifyType() {
return NoReviewerGroupNotify.TYPE;
}
}