Skip to content

Commit 872a7d5

Browse files
committed
T-39: add additional sink for reports
1 parent 7d21664 commit 872a7d5

File tree

51 files changed

+546
-736
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+546
-736
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Then run docker-compose with environment variables
4141
```console
4242
TELEGRAM_BOT_TOKEN=${YOUR_TOKEN} \
4343
TELEGRAM_BOT_ADMIN=${YOUR_TELEGRAM_ID} \
44-
SLACK_WEBHOOK=${SLACK_WEBHOOK} \
44+
WEBHOOK_URL=${WEBHOOK_URL} \
4545
docker-compose up
4646
```
4747

@@ -51,7 +51,7 @@ Adjust application-mock with report.parameters.schedule to send reports to slack
5151
Adjust SPRING_PROFILES_ACTIVE in docker-compose to "mock" and run docker-compose with your webhook
5252

5353
```console
54-
SLACK_WEBHOOK=${SLACK_WEBHOOK} \
54+
WEBHOOK_URL=${WEBHOOK_URL} \
5555
docker-compose up
5656
```
5757

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
- db
1111
environment:
1212
- SPRING_PROFILES_ACTIVE=mock,telegram
13-
- SLACK_WEBHOOK
13+
- WEBHOOK_URL
1414
- TELEGRAM_BOT_ADMIN
1515
- TELEGRAM_BOT_TOKEN
1616
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/postgres
@@ -20,6 +20,8 @@ services:
2020
db:
2121
image: 'postgres:16.2'
2222
container_name: db
23+
ports:
24+
- "5432:5432"
2325
environment:
2426
- POSTGRES_USER=postgres
2527
- POSTGRES_PASSWORD=postgres

src/main/java/com/whiskels/notifier/App.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
56
import org.springframework.cloud.openfeign.EnableFeignClients;
67
import org.springframework.context.annotation.Profile;
78
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -10,6 +11,7 @@
1011
@SpringBootApplication
1112
@EnableFeignClients
1213
@EnableScheduling
14+
@EnableConfigurationProperties
1315
public class App {
1416
public static void main(String[] args) {
1517
SpringApplication.run(App.class, args);

src/main/java/com/whiskels/notifier/infrastructure/config/clock/ClockConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66
import org.springframework.context.annotation.Profile;
77

88
import java.time.Clock;
9+
import java.time.Instant;
910
import java.time.ZoneId;
1011

1112
@Configuration
1213
@Profile("!test")
1314
class ClockConfiguration {
1415
@Bean
16+
@Profile("!mock")
1517
Clock defaultClock(@Value("${common.timezone}") String timeZone) {
1618
return Clock.system(ZoneId.of(timeZone));
1719
}
20+
21+
@Bean
22+
@Profile("mock")
23+
Clock mockedClock() {
24+
return Clock.fixed(Instant.parse("2025-01-01T10:15:30Z"), ZoneId.of("UTC"));
25+
}
1826
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.whiskels.notifier.infrastructure.report;
2+
3+
import com.whiskels.notifier.reporting.ReportType;
4+
import com.whiskels.notifier.reporting.service.Report;
5+
6+
public interface ReportExecutor {
7+
void send(ReportType type, Report report);
8+
}

src/main/java/com/whiskels/notifier/infrastructure/slack/SlackClient.java renamed to src/main/java/com/whiskels/notifier/infrastructure/report/slack/SlackClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.whiskels.notifier.infrastructure.slack;
1+
package com.whiskels.notifier.infrastructure.report.slack;
22

33
import com.slack.api.Slack;
44
import com.slack.api.webhook.Payload;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.whiskels.notifier.infrastructure.report.slack;
2+
3+
import com.slack.api.webhook.Payload;
4+
import com.whiskels.notifier.infrastructure.report.slack.builder.SlackPayloadBuilder;
5+
import com.whiskels.notifier.reporting.service.Report;
6+
import org.springframework.stereotype.Service;
7+
8+
import static java.util.Objects.nonNull;
9+
10+
@Service
11+
public class SlackPayloadMapper {
12+
public Payload map(final Report report) {
13+
var builder = SlackPayloadBuilder.builder()
14+
.header(report.getHeader());
15+
if (report.isNotifyChannel()) {
16+
builder.notifyChannel();
17+
}
18+
19+
if (nonNull(report.getBanner())) {
20+
builder.header(report.getBanner());
21+
}
22+
23+
report.getBody().forEach(block -> {
24+
if (nonNull(block.mediaContentUrl())) {
25+
builder.block(block.text(), block.mediaContentUrl());
26+
} else {
27+
builder.block(block.text());
28+
}
29+
});
30+
31+
return builder.build();
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.whiskels.notifier.infrastructure.report.slack;
2+
3+
import com.whiskels.notifier.infrastructure.report.ReportExecutor;
4+
import com.whiskels.notifier.reporting.ReportType;
5+
import com.whiskels.notifier.reporting.service.Report;
6+
import lombok.AllArgsConstructor;
7+
import lombok.extern.slf4j.Slf4j;
8+
9+
import java.io.IOException;
10+
import java.util.Map;
11+
12+
import static org.springframework.util.CollectionUtils.isEmpty;
13+
14+
@Slf4j
15+
@AllArgsConstructor
16+
public class SlackReportExecutor implements ReportExecutor {
17+
private final SlackClient slackClient;
18+
private final SlackPayloadMapper slackPayloadMapper;
19+
private final Map<ReportType, String> webhookMappings;
20+
21+
@Override
22+
public void send(ReportType type, Report report) {
23+
if (isEmpty(webhookMappings) || !webhookMappings.containsKey(type)) {
24+
log.error("No webhook mapping for type {}", type);
25+
return;
26+
}
27+
try {
28+
slackClient.send(webhookMappings.get(type), slackPayloadMapper.map(report));
29+
} catch (IOException e) {
30+
throw new IllegalArgumentException(e);
31+
}
32+
}
33+
}

src/main/java/com/whiskels/notifier/infrastructure/slack/builder/AccessoryBlock.java renamed to src/main/java/com/whiskels/notifier/infrastructure/report/slack/builder/AccessoryBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.whiskels.notifier.infrastructure.slack.builder;
1+
package com.whiskels.notifier.infrastructure.report.slack.builder;
22

33
import com.slack.api.model.block.element.BlockElement;
44
import lombok.Data;

src/main/java/com/whiskels/notifier/infrastructure/slack/builder/SlackPayloadBuilder.java renamed to src/main/java/com/whiskels/notifier/infrastructure/report/slack/builder/SlackPayloadBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.whiskels.notifier.infrastructure.slack.builder;
1+
package com.whiskels.notifier.infrastructure.report.slack.builder;
22

33
import com.slack.api.model.block.HeaderBlock;
44
import com.slack.api.model.block.LayoutBlock;
@@ -54,7 +54,7 @@ public SlackPayloadBuilder block(String text) {
5454
return this;
5555
}
5656

57-
public <T> SlackPayloadBuilder block(String text, String picUrl) {
57+
public SlackPayloadBuilder block(String text, String picUrl) {
5858
MarkdownTextObject content = new MarkdownTextObject();
5959
content.setText(text);
6060

0 commit comments

Comments
 (0)