Skip to content

Commit

Permalink
I18N-132 Mojito CLI failures should trigger an incident
Browse files Browse the repository at this point in the history
Used environment variables to send Slack messages
  • Loading branch information
DarKhaos committed Oct 11, 2024
1 parent adaecde commit d54cb02
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
11 changes: 0 additions & 11 deletions cli/src/main/java/com/box/l10n/mojito/cli/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ public abstract class Command {
description = HELP_DESCRIPTION)
private boolean help;

@Parameter(
names = {"--failure-slack-notification-channel", "-fsnc"},
arity = 1,
description =
"Slack channel to which notifications are sent, required if sending Slack notifications when CLI fails.")
private String failureSlackNotificationChannel;

/**
* Method to be overridden to implement the business logic of this command
*
Expand Down Expand Up @@ -121,8 +114,4 @@ public String getName() {
public void setOriginalArgs(List<String> originalArgs) {
this.originalArgs = originalArgs;
}

public String getFailureSlackNotificationChannel() {
return this.failureSlackNotificationChannel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;
import org.springframework.web.client.HttpClientErrorException;
Expand Down Expand Up @@ -48,6 +49,12 @@ public class L10nJCommander {
@Autowired(required = false)
SlackClient slackClient;

@Value("${FAILURE_SLACK_NOTIFICATION_CHANNEL:#{null}}")
String failureSlackNotificationChannel;

@Value("${FAILURE_URL:#{null}}")
String failureUrl;

static final String PROGRAM_NAME = "mojito";

boolean systemExitEnabled = true;
Expand Down Expand Up @@ -98,15 +105,17 @@ public void init() {
}

private void notifyFailure(Command command, String[] args, String errorMessage) {
boolean shouldNotifyFailure =
!Strings.isNullOrEmpty(command.getFailureSlackNotificationChannel());
boolean shouldNotifyFailure = !Strings.isNullOrEmpty(this.failureSlackNotificationChannel);
if (shouldNotifyFailure && this.slackClient != null) {
new SlackNotificationSender(this.slackClient)
.sendMessage(
command.getFailureSlackNotificationChannel(),
this.failureSlackNotificationChannel,
String.format(
":warning: *%s* command has failed\n\n*ARGUMENTS:*\n%s\n\n*ERROR MESSAGE:*\n%s",
command.getName(), String.join(", ", args), errorMessage));
":warning: *%s* command has failed\n\n*ARGUMENTS:*\n%s\n\n*URL:*\n%s\n\n*ERROR MESSAGE:*\n%s",
command.getName(),
String.join(", ", args),
this.failureUrl == null ? "" : this.failureUrl,
errorMessage));
} else if (shouldNotifyFailure) {
logger.error(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ private L10nJCommander getL10nJCommanderSpy() {
Mockito.reset(this.slackClientMock);
Mockito.reset(this.commandMock);
when(this.commandMock.getName()).thenReturn("repo-create");
when(this.commandMock.getFailureSlackNotificationChannel()).thenReturn("@testslackchannel");
L10nJCommander commander = Mockito.spy(this.getL10nJCommander());
commander.failureSlackNotificationChannel = "@username";
commander.failureUrl = "https://example.com";
when(commander.getCommand(anyString())).thenReturn(this.commandMock);
commander.slackClient = this.slackClientMock;
return commander;
Expand Down Expand Up @@ -101,8 +102,9 @@ public void testRunSlackClientIsNotCalled() throws SlackClientException {
assertEquals(1, commander.getExitCode());

commander = this.getL10nJCommanderSpy();
commander.failureSlackNotificationChannel = null;
commander.failureUrl = null;
doThrow(new ResourceAccessException("error message")).when(this.commandMock).run();
when(this.commandMock.getFailureSlackNotificationChannel()).thenReturn(null);
commander.run("repo-create", "-n", "test_repo", "-l", "ar-SA");

verify(this.slackClientMock, times(0)).sendInstantMessage(any());
Expand Down

0 comments on commit d54cb02

Please sign in to comment.