forked from quarkusio/quarkus-github-lottery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistoryService.java
48 lines (39 loc) · 2.11 KB
/
HistoryService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package io.quarkus.github.lottery.history;
import static io.quarkus.github.lottery.util.UncheckedIOFunction.uncheckedIO;
import java.io.IOException;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import io.quarkus.github.lottery.config.LotteryConfig;
import io.quarkus.github.lottery.draw.DrawRef;
import io.quarkus.github.lottery.draw.LotteryReport;
import io.quarkus.github.lottery.github.GitHubRepository;
import io.quarkus.github.lottery.github.GitHubService;
import io.quarkus.github.lottery.message.MessageFormatter;
import io.quarkus.github.lottery.notification.NotificationService;
@ApplicationScoped
public class HistoryService {
@Inject
GitHubService gitHubService;
@Inject
MessageFormatter messageFormatter;
public LotteryHistory fetch(DrawRef drawRef, LotteryConfig config) throws IOException {
var persistenceRepo = persistenceRepo(drawRef, config);
var history = new LotteryHistory(drawRef.instant(), config.buckets());
String historyTopic = messageFormatter.formatHistoryTopicText(drawRef);
persistenceRepo.extractCommentsFromDedicatedIssue(persistenceRepo.selfLogin(), historyTopic, history.since())
.flatMap(uncheckedIO(message -> messageFormatter.extractPayloadFromHistoryBodyMarkdown(message).stream()))
.forEach(history::add);
return history;
}
public void append(DrawRef drawRef, LotteryConfig config, List<LotteryReport.Serialized> reports) throws IOException {
var persistenceRepo = persistenceRepo(drawRef, config);
String historyTopic = messageFormatter.formatHistoryTopicText(drawRef);
String commentBody = messageFormatter.formatHistoryBodyMarkdown(drawRef, reports);
persistenceRepo.commentOnDedicatedIssue(persistenceRepo.selfLogin(), historyTopic, commentBody);
}
GitHubRepository persistenceRepo(DrawRef drawRef, LotteryConfig config) {
// We persist history to the same repository we send notifications to
return NotificationService.notificationRepository(gitHubService, drawRef, config.notifications());
}
}