Permalink
Browse files

issue #149 - send notification when a new challenge added

  • Loading branch information...
ufuk committed Nov 19, 2016
1 parent 80eab43 commit e67b7d3d5543cf6ee264e84e33e330bce1b4d8cb
View
20 pom.xml
@@ -612,26 +612,6 @@
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- Codecov Integration -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
@@ -9,7 +9,10 @@
import com.expercise.interpreter.TestCasesWithSourceModel;
import com.expercise.service.configuration.ConfigurationService;
import com.expercise.service.language.SignatureGeneratorService;
import com.expercise.service.notification.SlackMessage;
import com.expercise.service.notification.SlackNotificationService;
import com.expercise.service.user.AuthenticationService;
import com.expercise.service.util.UrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -39,6 +42,12 @@
@Autowired
private ConfigurationService configurationService;
@Autowired
private SlackNotificationService slackNotificationService;
@Autowired
private UrlService urlService;
@Value("${coding-expercise.challenge-approval-strategy}")
private String challengeApprovalStrategy;
@@ -78,16 +87,38 @@ public Long saveChallenge(ChallengeModel challengeModel) {
if (challenge.isPersisted()) {
challengeDao.update(challenge);
} else {
if ("auto".equalsIgnoreCase(challengeApprovalStrategy)) {
challenge.setApproved(true);
}
checkAndAutoApprove(challenge);
challenge.setUser(authenticationService.getCurrentUser());
challengeDao.save(challenge);
sendNewChallengeNotification(challenge);
}
return challenge.getId();
}
private void checkAndAutoApprove(Challenge challenge) {
if ("auto".equalsIgnoreCase(challengeApprovalStrategy)) {
challenge.setApproved(true);
}
}
private void sendNewChallengeNotification(Challenge challenge) {
User currentUser = authenticationService.getCurrentUser();
SlackMessage slackMessage = new SlackMessage();
slackMessage.setChannel("#general");
slackMessage.setText(
String.format(
"New challenge <%s|%s> by <%s|%s>.",
urlService.challengeUrl(challenge),
challenge.getTitle(),
urlService.createUrlFor(currentUser.getBookmarkableUrl()),
currentUser.getFullName()
));
slackNotificationService.sendMessage(slackMessage);
}
private Challenge prepareChallengeForSaving(ChallengeModel challengeModel) {
Challenge challenge;
if (challengeModel.getChallengeId() != null) {
@@ -43,6 +43,10 @@ public String getGoogleAnalyticsApplicationKey() {
return getValue("googleAnalytics.applicationKey");
}
public String getSlackIncomingWebhookUrl() {
return getValue("slack.incomingWebhookUrl");
}
public Optional<Long> getIdOfDefaultChallenge() {
Long challengeId = getValueAsLong("defaultChallenge");
return Optional.ofNullable(challengeId);
@@ -0,0 +1,33 @@
package com.expercise.service.notification;
public class SlackMessage {
private String channel;
private String text;
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return "SlackMessage{" +
"channel='" + channel + '\'' +
", text='" + text + '\'' +
'}';
}
}
@@ -0,0 +1,36 @@
package com.expercise.service.notification;
import com.expercise.service.configuration.ConfigurationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class SlackNotificationService {
private static final Logger LOGGER = LoggerFactory.getLogger(SlackNotificationService.class);
@Autowired
private ConfigurationService configurationService;
@Async
public void sendMessage(SlackMessage slackMessage) {
try {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-type", MediaType.APPLICATION_JSON_VALUE + "; charset=utf-8");
HttpEntity<SlackMessage> httpEntity = new HttpEntity<>(slackMessage, httpHeaders);
new RestTemplate().postForLocation(configurationService.getSlackIncomingWebhookUrl(), httpEntity);
} catch (Exception e) {
LOGGER.error("Slack notification couldn't sent: " + slackMessage, e);
}
}
}
@@ -29,4 +29,8 @@ public String updateChallenge(Challenge challenge) {
return "/challenges/updateChallenge/" + challenge.getId();
}
public String challengeUrl(Challenge challenge) {
return createUrlFor("/challenges/" + challenge.getId());
}
}
@@ -18,7 +18,7 @@ hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/coding-expercise?useUnicode=true&characterEncoding=utf-8
jdbc.url=jdbc:mysql://localhost:3306/codingexpercise?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=
@@ -1,10 +1,13 @@
-- ufuk - 07/05/2016 23:00
CREATE SCHEMA `expercise`
CREATE SCHEMA `codingexpercise`
DEFAULT CHARACTER SET utf8
COLLATE utf8_turkish_ci;
-- batu - 11/02/2015 22:50
INSERT INTO Configuration (name, value) VALUES ('googleAnalytics.applicationKey', 'UA-59509615-1');
INSERT INTO Configuration (name, value) VALUES ('googleAnalytics.applicationKey', '');
-- ufuk - 08/05/2016 02:49
INSERT INTO Configuration (name, value) VALUES ('defaultChallenge', '1');
INSERT INTO Configuration (name, value) VALUES ('defaultChallenge', '1');
-- ufuk - 19/11/2016 01:26
INSERT INTO Configuration (name, value) VALUES ('slack.incomingWebhookUrl', '');

0 comments on commit e67b7d3

Please sign in to comment.