Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Then run docker-compose with environment variables
```console
TELEGRAM_BOT_TOKEN=${YOUR_TOKEN} \
TELEGRAM_BOT_ADMIN=${YOUR_TELEGRAM_ID} \
SLACK_WEBHOOK=${SLACK_WEBHOOK} \
WEBHOOK_URL=${WEBHOOK_URL} \
docker-compose up
```

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

```console
SLACK_WEBHOOK=${SLACK_WEBHOOK} \
WEBHOOK_URL=${WEBHOOK_URL} \
docker-compose up
```

Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- db
environment:
- SPRING_PROFILES_ACTIVE=mock,telegram
- SLACK_WEBHOOK
- WEBHOOK_URL
- TELEGRAM_BOT_ADMIN
- TELEGRAM_BOT_TOKEN
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/postgres
Expand All @@ -20,6 +20,8 @@ services:
db:
image: 'postgres:16.2'
container_name: db
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
2 changes: 2 additions & 0 deletions src/main/java/com/whiskels/notifier/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;
Expand All @@ -10,6 +11,7 @@
@SpringBootApplication
@EnableFeignClients
@EnableScheduling
@EnableConfigurationProperties
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
import org.springframework.context.annotation.Profile;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;

@Configuration
@Profile("!test")
class ClockConfiguration {
@Bean
@Profile("!mock")
Clock defaultClock(@Value("${common.timezone}") String timeZone) {
return Clock.system(ZoneId.of(timeZone));
}

@Bean
@Profile("mock")
Clock mockedClock() {
return Clock.fixed(Instant.parse("2025-01-01T10:15:30Z"), ZoneId.of("UTC"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.whiskels.notifier.infrastructure.report;

import com.whiskels.notifier.reporting.ReportType;
import com.whiskels.notifier.reporting.service.Report;

public interface ReportExecutor {
void send(ReportType type, Report report);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.whiskels.notifier.infrastructure.slack;
package com.whiskels.notifier.infrastructure.report.slack;

import com.slack.api.Slack;
import com.slack.api.webhook.Payload;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.whiskels.notifier.infrastructure.report.slack;

import com.slack.api.webhook.Payload;
import com.whiskels.notifier.infrastructure.report.slack.builder.SlackPayloadBuilder;
import com.whiskels.notifier.reporting.service.Report;
import org.springframework.stereotype.Service;

import static java.util.Objects.nonNull;

@Service
public class SlackPayloadMapper {
public Payload map(final Report report) {
var builder = SlackPayloadBuilder.builder()
.header(report.getHeader());
if (report.isNotifyChannel()) {
builder.notifyChannel();
}

if (nonNull(report.getBanner())) {
builder.header(report.getBanner());
}

report.getBody().forEach(block -> {
if (nonNull(block.mediaContentUrl())) {
builder.block(block.text(), block.mediaContentUrl());
} else {
builder.block(block.text());
}
});

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.whiskels.notifier.infrastructure.report.slack;

import com.whiskels.notifier.infrastructure.report.ReportExecutor;
import com.whiskels.notifier.reporting.ReportType;
import com.whiskels.notifier.reporting.service.Report;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Map;

import static org.springframework.util.CollectionUtils.isEmpty;

@Slf4j
@AllArgsConstructor
public class SlackReportExecutor implements ReportExecutor {
private final SlackClient slackClient;
private final SlackPayloadMapper slackPayloadMapper;
private final Map<ReportType, String> webhookMappings;

@Override
public void send(ReportType type, Report report) {
if (isEmpty(webhookMappings) || !webhookMappings.containsKey(type)) {
log.error("No webhook mapping for type {}", type);
return;
}
try {
slackClient.send(webhookMappings.get(type), slackPayloadMapper.map(report));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.whiskels.notifier.infrastructure.slack.builder;
package com.whiskels.notifier.infrastructure.report.slack.builder;

import com.slack.api.model.block.element.BlockElement;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.whiskels.notifier.infrastructure.slack.builder;
package com.whiskels.notifier.infrastructure.report.slack.builder;

import com.slack.api.model.block.HeaderBlock;
import com.slack.api.model.block.LayoutBlock;
Expand Down Expand Up @@ -54,7 +54,7 @@ public SlackPayloadBuilder block(String text) {
return this;
}

public <T> SlackPayloadBuilder block(String text, String picUrl) {
public SlackPayloadBuilder block(String text, String picUrl) {
MarkdownTextObject content = new MarkdownTextObject();
content.setText(text);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.whiskels.notifier.infrastructure.slack.config;
package com.whiskels.notifier.infrastructure.report.slack.config;

import com.slack.api.Slack;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.whiskels.notifier.infrastructure.report.webhook;

import feign.Headers;
import feign.RequestLine;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;

import java.net.URI;

public interface FeignWebhookSink {
@RequestLine("POST")
@Headers("Content-Type: application/json")
ResponseEntity<String> send(URI url, @RequestBody FeignWebhookSinkDto dto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.whiskels.notifier.infrastructure.report.webhook;

import feign.Feign;
import feign.Target;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(FeignClientsConfiguration.class)
class FeignWebhookSinkConfig {
@Bean
FeignWebhookSink feignWebhookSink(Encoder encoder, Decoder decoder) {
return Feign.builder()
.encoder(encoder)
.decoder(decoder)
.target(Target.EmptyTarget.create(FeignWebhookSink.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.whiskels.notifier.infrastructure.report.webhook;

import lombok.Data;

@Data
public class FeignWebhookSinkDto {
String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.whiskels.notifier.infrastructure.report.webhook;

import com.whiskels.notifier.infrastructure.report.ReportExecutor;
import com.whiskels.notifier.reporting.ReportType;
import com.whiskels.notifier.reporting.service.Report;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.net.URI;
import java.util.Map;

import static com.whiskels.notifier.utilities.formatters.StringFormatter.COLLECTOR_NEW_LINE;

@Slf4j
@RequiredArgsConstructor
public class WebhookSinkExecutor implements ReportExecutor {
private final FeignWebhookSink feignWebhookSink;
private final Map<ReportType, String> webhookMappings;

@Override
public void send(final ReportType type, final Report report) {
if (CollectionUtils.isEmpty(webhookMappings) || !webhookMappings.containsKey(type)) {
log.error("No webhook mapping for type {}", type);
return;
}
try {
final var dto = new FeignWebhookSinkDto();
var reportBody = report.getBody().stream()
.map(Report.ReportBodyBlock::text)
.collect(COLLECTOR_NEW_LINE);
dto.setMessage(STR."\{report.getHeader()}\n\n\{reportBody}");
final var response = feignWebhookSink.send(new URI(webhookMappings.get(type)), dto);

if (!response.getStatusCode().is2xxSuccessful()) {
log.error("Webhook sink {} failed: {}", type, response);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Map;
import java.util.TimeZone;

import static com.whiskels.notifier.utilities.formatters.StringFormatter.COLLECTOR_NEW_LINE;
import static org.springframework.util.CollectionUtils.isEmpty;

@RequiredArgsConstructor
Expand All @@ -32,11 +33,16 @@ private void scheduleReports() {
log.error("No cron triggers found for reports");
return;
}
log.info("Scheduling reports: \n{}",
reportCronMap.entrySet().stream()
.map(entry -> String.format(" %-8s -> %s", entry.getKey(), entry.getValue()))
.collect(COLLECTOR_NEW_LINE));

reportCronMap.forEach((type, cron) ->
executor.schedule(() -> service.executeReport(type),
new CronTrigger(cron, TimeZone.getTimeZone(ZoneId.of(timeZone))
)
executor.schedule(() -> service.executeReport(type),
new CronTrigger(cron, TimeZone.getTimeZone(ZoneId.of(timeZone))
)
);
)
);
}
}

This file was deleted.

Loading