diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/Command.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/Command.java index 872739d2ad..cbfb3ab917 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/Command.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/Command.java @@ -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 * @@ -121,8 +114,4 @@ public String getName() { public void setOriginalArgs(List originalArgs) { this.originalArgs = originalArgs; } - - public String getFailureSlackNotificationChannel() { - return this.failureSlackNotificationChannel; - } } diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/L10nJCommander.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/L10nJCommander.java index b5317f585c..2eda4fdcfd 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/L10nJCommander.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/L10nJCommander.java @@ -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; @@ -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; @@ -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( diff --git a/cli/src/test/java/com/box/l10n/mojito/cli/command/L10nJCommanderTest.java b/cli/src/test/java/com/box/l10n/mojito/cli/command/L10nJCommanderTest.java index 1eca971cd9..e58e7b7393 100644 --- a/cli/src/test/java/com/box/l10n/mojito/cli/command/L10nJCommanderTest.java +++ b/cli/src/test/java/com/box/l10n/mojito/cli/command/L10nJCommanderTest.java @@ -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; @@ -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());