2
2
3
3
import com .eternalcode .discordapp .config .AppConfig ;
4
4
import com .eternalcode .discordapp .config .ConfigManager ;
5
+ import com .eternalcode .discordapp .review .database .GitHubReviewMentionRepository ;
5
6
import io .sentry .Sentry ;
6
7
import java .io .IOException ;
7
8
import java .util .ArrayList ;
8
9
import java .util .List ;
10
+ import java .util .concurrent .CompletableFuture ;
9
11
import java .util .logging .Logger ;
10
12
import net .dv8tion .jda .api .JDA ;
11
13
import net .dv8tion .jda .api .entities .Guild ;
@@ -21,16 +23,20 @@ public class GitHubReviewService {
21
23
private final static Logger LOGGER = Logger .getLogger (GitHubReviewService .class .getName ());
22
24
23
25
private static final String DM_REVIEW_MESSAGE = "You have been assigned as a reviewer for this pull request: %s" ;
24
- private static final String SERVER_REVIEW_MESSAGE =
25
- "%s, you have been assigned as a reviewer for this pull request: %s" ;
26
+ private static final String SERVER_REVIEW_MESSAGE = "%s, you have been assigned as a reviewer for this pull request: %s" ;
26
27
27
28
private final AppConfig appConfig ;
28
29
private final ConfigManager configManager ;
29
- private final GitHubReviewMentionRepository mentionRepository = new GitHubReviewMentionRepositoryImpl () ;
30
+ private final GitHubReviewMentionRepository mentionRepository ;
30
31
31
- public GitHubReviewService (AppConfig appConfig , ConfigManager configManager ) {
32
+ public GitHubReviewService (
33
+ AppConfig appConfig ,
34
+ ConfigManager configManager ,
35
+ GitHubReviewMentionRepository mentionRepository
36
+ ) {
32
37
this .appConfig = appConfig ;
33
38
this .configManager = configManager ;
39
+ this .mentionRepository = mentionRepository ;
34
40
}
35
41
36
42
public String createReview (Guild guild , String url , JDA jda ) {
@@ -94,6 +100,7 @@ public void mentionReviewers(JDA jda, GitHubPullRequest pullRequest, long forumI
94
100
}
95
101
96
102
StringBuilder reviewersMention = new StringBuilder ();
103
+ CompletableFuture <Void > mentionFuture = CompletableFuture .completedFuture (null );
97
104
98
105
for (String reviewer : assignedReviewers ) {
99
106
GitHubReviewUser gitHubReviewUser = this .getReviewUserByUsername (reviewer );
@@ -104,44 +111,52 @@ public void mentionReviewers(JDA jda, GitHubPullRequest pullRequest, long forumI
104
111
105
112
Long discordId = gitHubReviewUser .getDiscordId ();
106
113
107
- if (discordId != null && !this .mentionRepository .isMentioned (pullRequest , discordId )) {
108
- User user = jda .getUserById (discordId );
109
- GitHubReviewNotificationType notificationType = gitHubReviewUser .getNotificationType ();
110
-
111
- if (user == null ) {
112
- return ;
113
- }
114
-
115
- String message = String .format (DM_REVIEW_MESSAGE , pullRequest .toUrl ());
116
-
117
- if (notificationType .isDmNotify ()) {
118
- try {
119
- LOGGER .info ("Sending message to: " + user .getName ());
120
- user .openPrivateChannel ().queue (
121
- privateChannel -> privateChannel .sendMessage (message ).queue (),
122
- throwable -> LOGGER .warning ("Cannot send message to: " + user .getName ()));
114
+ if (discordId != null ) {
115
+ mentionFuture = mentionFuture .thenComposeAsync (v ->
116
+ this .mentionRepository .isMentioned (pullRequest , discordId )
117
+ ).thenAcceptAsync (isMentioned -> {
118
+ if (!isMentioned ) {
119
+ User user = jda .getUserById (discordId );
120
+ GitHubReviewNotificationType notificationType = gitHubReviewUser .getNotificationType ();
121
+
122
+ if (user == null ) {
123
+ return ;
124
+ }
125
+
126
+ String message = String .format (DM_REVIEW_MESSAGE , pullRequest .toUrl ());
127
+
128
+ if (notificationType .isDmNotify ()) {
129
+ try {
130
+ LOGGER .info ("Sending message to: " + user .getName ());
131
+ user .openPrivateChannel ().queue (
132
+ privateChannel -> privateChannel .sendMessage (message ).queue (),
133
+ throwable -> LOGGER .warning ("Cannot send message to: " + user .getName ()));
134
+ }
135
+ catch (Exception exception ) {
136
+ Sentry .captureException (exception );
137
+ LOGGER .warning ("Cannot send message to: " + user .getName ());
138
+ }
139
+ }
140
+ if (notificationType .isServerNotify ()) {
141
+ reviewersMention .append (user .getAsMention ()).append (" " );
142
+ }
143
+
144
+ this .mentionRepository .markReviewerAsMentioned (pullRequest , discordId );
123
145
}
124
- catch (Exception exception ) {
125
- Sentry .captureException (exception );
126
- LOGGER .warning ("Cannot send message to: " + user .getName ());
127
- }
128
- }
129
- if (notificationType .isServerNotify ()) {
130
- reviewersMention .append (user .getAsMention ()).append (" " );
131
- }
132
-
133
- this .mentionRepository .markReviewerAsMentioned (pullRequest , discordId );
146
+ });
134
147
}
135
148
}
136
149
137
- if (!reviewersMention .isEmpty ()) {
138
- String message = String .format (SERVER_REVIEW_MESSAGE , reviewersMention , pullRequest .toUrl ());
139
- ThreadChannel threadChannel = jda .getThreadChannelById (forumId );
150
+ mentionFuture .thenRunAsync (() -> {
151
+ if (!reviewersMention .isEmpty ()) {
152
+ String message = String .format (SERVER_REVIEW_MESSAGE , reviewersMention , pullRequest .toUrl ());
153
+ ThreadChannel threadChannel = jda .getThreadChannelById (forumId );
140
154
141
- if (threadChannel != null ) {
142
- threadChannel .sendMessage (message ).queue ();
155
+ if (threadChannel != null ) {
156
+ threadChannel .sendMessage (message ).queue ();
157
+ }
143
158
}
144
- }
159
+ });
145
160
}
146
161
147
162
public void mentionReviewersOnAllReviewChannels (JDA jda ) {
@@ -204,17 +219,17 @@ public void archiveMergedPullRequest(JDA jda) {
204
219
205
220
if (GitHubReviewUtil .isPullRequestMerged (pullRequest , this .appConfig .githubToken )) {
206
221
threadChannel .getManager ()
222
+ .setAppliedTags (ForumTagSnowflake .fromId (reviewSystem .mergedTagId ))
207
223
.setLocked (true )
208
224
.setArchived (true )
209
- .setAppliedTags (ForumTagSnowflake .fromId (reviewSystem .mergedTagId ))
210
225
.queue ();
211
226
}
212
227
213
228
if (GitHubReviewUtil .isPullRequestClosed (pullRequest , this .appConfig .githubToken )) {
214
229
threadChannel .getManager ()
230
+ .setAppliedTags (ForumTagSnowflake .fromId (reviewSystem .closedTagId ))
215
231
.setLocked (true )
216
232
.setArchived (true )
217
- .setAppliedTags (ForumTagSnowflake .fromId (reviewSystem .closedTagId ))
218
233
.queue ();
219
234
}
220
235
}
0 commit comments