Skip to content

Commit

Permalink
Added blocked users property to stop sending Slack messages to users …
Browse files Browse the repository at this point in the history
…in that list
  • Loading branch information
mattwilshire committed Oct 14, 2024
1 parent 9c29c22 commit da01163
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ private Map<String, BranchNotificationMessageSenderSlack> createSlackInstances(
branchNotificationMessageBuilderSlack,
slackConfigurationProperties.getUserEmailPattern(),
slackConfigurationProperties.isUseDirectMessage(),
slackConfigurationProperties.isGithubPR());
slackConfigurationProperties.isGithubPR(),
slackConfigurationProperties.getBlockedUsernames());

return new SimpleEntry<String, BranchNotificationMessageSenderSlack>(
id, branchNotificationMessageSenderSlack);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.box.l10n.mojito.service.branch.notification;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -73,6 +75,8 @@ static class SlackConfigurationProperties {

boolean githubPR = false;

List<String> blockedUsernames = new ArrayList<>();

public String getSlackClientId() {
return slackClientId;
}
Expand Down Expand Up @@ -113,6 +117,14 @@ public void setGithubPR(boolean githubPR) {
this.githubPR = githubPR;
}

public List<String> getBlockedUsernames() {
return blockedUsernames;
}

public void setBlockedUsernames(List<String> blockedUsernames) {
this.blockedUsernames = blockedUsernames;
}

public static class MessageBuilderConfigurationProperties {

String newStrings =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import com.box.l10n.mojito.service.branch.BranchTextUnitStatisticRepository;
import com.box.l10n.mojito.service.branch.notification.job.BranchNotificationMissingScreenshotsJob;
import com.box.l10n.mojito.service.branch.notification.job.BranchNotificationMissingScreenshotsJobInput;
import com.box.l10n.mojito.service.branch.notification.slack.BranchNotificationMessageSenderSlack;
import com.box.l10n.mojito.service.tm.search.TextUnitDTO;
import com.box.l10n.mojito.utils.DateTimeUtils;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -154,6 +156,15 @@ void sendNotificationsForBranchWithSender(
notifierId);
BranchNotification branchNotification = getOrCreateBranchNotification(branch, notifierId);

if (branchNotificationMessageSender
instanceof BranchNotificationMessageSenderSlack branchNotificationMessageSenderSlack) {
String userName = getUsername(branch);
if (Strings.isNullOrEmpty(userName)
|| branchNotificationMessageSenderSlack.isUsernameBlocked(userName)) {
return;
}
}

if (shouldSendNewMessage(branchNotification, branchNotificationInfo)) {
sendNewMessage(
branchNotificationMessageSender, branch, branchNotification, branchNotificationInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.box.l10n.mojito.thirdpartynotification.slack.SlackChannels;
import com.google.common.base.Preconditions;
import java.util.List;
import java.util.regex.Pattern;
import org.slf4j.Logger;

public class BranchNotificationMessageSenderSlack implements BranchNotificationMessageSender {
Expand All @@ -29,14 +30,17 @@ public class BranchNotificationMessageSenderSlack implements BranchNotificationM
boolean useDirectMessage;
boolean githubPR;

List<String> blockedUsernames;

public BranchNotificationMessageSenderSlack(
String id,
SlackClient slackClient,
SlackChannels slackChannels,
BranchNotificationMessageBuilderSlack branchNotificationMessageBuilderSlack,
String userEmailPattern,
boolean useDirectMessage,
boolean isGithubPR) {
boolean isGithubPR,
List<String> blockedUsernames) {
this.id = id;
this.slackClient = Preconditions.checkNotNull(slackClient);
this.slackChannels = Preconditions.checkNotNull(slackChannels);
Expand All @@ -45,6 +49,7 @@ public BranchNotificationMessageSenderSlack(
this.userEmailPattern = Preconditions.checkNotNull(userEmailPattern);
this.useDirectMessage = useDirectMessage;
this.githubPR = isGithubPR;
this.blockedUsernames = blockedUsernames;
}

@Override
Expand Down Expand Up @@ -126,6 +131,17 @@ public void sendScreenshotMissingMessage(String branchName, String messageId, St
}
}

public boolean isUsernameBlocked(String username) {
for (String blockedUsernameRegex : blockedUsernames) {
if (Pattern.compile(blockedUsernameRegex, Pattern.CASE_INSENSITIVE)
.matcher(username)
.matches()) {
return true;
}
}
return false;
}

@Override
public String getId() {
return id;
Expand Down

0 comments on commit da01163

Please sign in to comment.