Skip to content

Commit aef8cfd

Browse files
authored
Increase Database Usage (#46)
* chore: bump version * Update build.yml * feat: Use Database API for Requests * chore: Remove Unused ID Parameter + Add Create Request Function * Update Unit Tests
1 parent 4458534 commit aef8cfd

File tree

9 files changed

+134
-169
lines changed

9 files changed

+134
-169
lines changed

.github/workflows/build.yml

+2-9
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,8 @@ jobs:
7272
run: |
7373
rm -rf /tmp/admin.password
7474
75-
docker stop jenkins-rest
76-
docker rm jenkins-rest
77-
78-
docker stop nexus-rest
79-
docker rm nexus-rest
80-
81-
docker stop mariadb
82-
docker rm mariadb
83-
75+
docker stop jenkins-rest nexus-rest mariadb
76+
docker rm jenkins-rest nexus-rest mariadb
8477
docker network rm codemc
8578
- name: Archive Test Reports
8679
uses: actions/upload-artifact@v4

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ plugins {
2424
}
2525

2626
group 'io.codemc'
27-
version '2.1.0'
27+
version '2.1.1'
2828

2929
compileJava.options.encoding('UTF-8')
3030

@@ -47,7 +47,7 @@ dependencies {
4747
implementation group: 'pw.chew', name: 'jda-chewtils-command', version: '2.1'
4848
implementation group: 'org.spongepowered', name: 'configurate-gson', version: '4.1.2'
4949

50-
implementation group: 'io.codemc.api', name: 'codemc-api', version: '1.2.1'
50+
implementation group: 'io.codemc.api', name: 'codemc-api', version: '1.2.2'
5151
implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.10.1'
5252
implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-serialization-json', version: '1.8.0'
5353
implementation group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '3.5.2'

src/main/java/io/codemc/bot/listeners/ModalListener.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package io.codemc.bot.listeners;
2020

21+
import io.codemc.api.database.DatabaseAPI;
2122
import io.codemc.api.jenkins.JenkinsAPI;
2223
import io.codemc.bot.CodeMCBot;
2324
import io.codemc.bot.utils.ApplicationHandler;
@@ -100,7 +101,7 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event){
100101
String repoLink = MarkdownUtil.maskedLink(repo, repoLinkValue);
101102
String submitter = String.format("`%s` (%s)", event.getUser().getEffectiveName(), event.getUser().getAsMention());
102103

103-
MessageEmbed embed = CommandUtil.requestEmbed(userLink, repoLink, submitter, description, event.getUser().getId());
104+
MessageEmbed embed = CommandUtil.requestEmbed(userLink, repoLink, submitter, description);
104105

105106
requestChannel.sendMessageEmbeds(embed)
106107
.setActionRow(
@@ -116,6 +117,8 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event){
116117
message.addReaction(Emoji.fromCustom("like", 935126958193405962L, false)).queue();
117118
message.addReaction(Emoji.fromCustom("dislike", 935126958235344927L, false)).queue();
118119

120+
DatabaseAPI.createRequest(message.getIdLong(), event.getUser().getIdLong(), user, repo);
121+
119122
logger.info("[Access Request] User {} requested access to the CI.", event.getUser().getEffectiveName());
120123
},
121124
e -> {

src/main/java/io/codemc/bot/utils/ApplicationHandler.java

+60-105
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import io.codemc.bot.CodeMCBot;
2222
import io.codemc.api.database.DatabaseAPI;
23+
import io.codemc.api.database.Request;
2324
import net.dv8tion.jda.api.EmbedBuilder;
2425
import net.dv8tion.jda.api.entities.*;
2526
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
@@ -34,8 +35,6 @@
3435
import org.slf4j.Logger;
3536
import org.slf4j.LoggerFactory;
3637

37-
import java.util.List;
38-
3938
public class ApplicationHandler{
4039

4140
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationHandler.class);
@@ -49,89 +48,57 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
4948

5049
hook.editOriginal(
5150
"[1/5] Handling Join Request...\n" +
52-
"- [1/1] Retrieving Request Message..."
51+
"- [1/1] Retrieving Request..."
5352
).queue();
53+
5454
requestChannel.retrieveMessageById(messageId).queue(message -> {
55-
hook.editOriginal(
56-
"""
57-
[2/5] Handling Join Request...
58-
- [<:like:935126958193405962>] Message retrieved!
59-
- [1/3] Validating Message...
60-
- Retrieving Embed...
61-
"""
62-
).queue();
63-
List<MessageEmbed> embeds = message.getEmbeds();
64-
if(embeds.isEmpty()){
65-
CommandUtil.EmbedReply.from(hook).error("Provided Message does not have any embeds.").send();
55+
Request req = DatabaseAPI.getRequest(messageId);
56+
if(req == null){
57+
CommandUtil.EmbedReply.from(hook).error("Request not found in Database.").send();
6658
return;
6759
}
6860

69-
MessageEmbed embed = embeds.get(0);
70-
if(embed.getFooter() == null || embed.getFields().isEmpty()){
71-
CommandUtil.EmbedReply.from(hook).error("Embed does not have a Footer or any Embed Fields").send();
72-
return;
73-
}
74-
7561
hook.editOriginal(
7662
"""
7763
[2/5] Handling Join Request...
78-
- [<:like:935126958193405962>] Message retrieved!
79-
- [2/3] Validating Message...
80-
- Embed found!
81-
- Retrieving User ID...
64+
- [<:like:935126958193405962>] Request retrieved!
65+
- [1/2] Validating Request...
8266
"""
8367
).queue();
8468

85-
String userId = embed.getFooter().getText() == null ? null : embed.getFooter().getText().trim();
86-
if(userId == null || userId.isEmpty()){
87-
CommandUtil.EmbedReply.from(hook).error("Embed does not have a valid footer.").send();
69+
long userId = req.getUserId();
70+
if(userId <= 0){
71+
CommandUtil.EmbedReply.from(hook).error("Request does not have a valid user.").send();
8872
return;
8973
}
9074

9175
hook.editOriginalFormat(
9276
"""
9377
[2/5] Handling Join Request...
94-
- [<:like:935126958193405962>] Message retrieved!
95-
- [3/3] Validating Message...
96-
- Embed found!
97-
- Found User ID `%s`.
98-
- Find and validate User and Repository link...
78+
- [<:like:935126958193405962>] Request retrieved!
79+
- [2/2] Validating Request...
80+
- Found User ID `%d`.
81+
- Find and validate User and Repository link...
9982
""", userId
10083
).queue();
10184

102-
if (embed.getFields().size() < 2) {
103-
CommandUtil.EmbedReply.from(hook).error("Embed does not have all valid Fields.").send();
104-
return;
105-
}
106-
107-
MessageEmbed.Field userField = embed.getFields().get(0);
108-
MessageEmbed.Field repoField = embed.getFields().get(1);
109-
if(userField == null || repoField == null){
110-
CommandUtil.EmbedReply.from(hook).error("Embed does not have all valid Fields.").send();
111-
return;
112-
}
113-
114-
String user = userField.getValue();
115-
String repo = repoField.getValue();
116-
117-
String username = user.substring(1, user.indexOf("]"));
118-
String userLink = user.substring(user.indexOf("(") + 1, user.length() - 1);
119-
String repoName = repo.substring(1, repo.indexOf("]"));
120-
String repoLink = repo.substring(repo.indexOf("(") + 1, repo.length() - 1);
85+
String username = req.getGithubName();
86+
String repoName = req.getRepoName();
12187

122-
if(username == null || userLink == null || repoName == null || repoLink == null){
123-
CommandUtil.EmbedReply.from(hook).error("Embed does not have any valid Fields.").send();
88+
if(username.isEmpty() || repoName.isEmpty()){
89+
CommandUtil.EmbedReply.from(hook).error("Database Request is missing values.").send();
12490
return;
12591
}
92+
93+
String userLink = "https://github.com/" + username;
94+
String repoLink = userLink + "/" + repoName;
12695

12796
hook.editOriginalFormat(
12897
"""
12998
[3/5] Handling Join Request...
130-
- [<:like:935126958193405962>] Message retrieved!
131-
- [<:like:935126958193405962>] Message validated!
132-
- Embed found!
133-
- Found User ID `%s`.
134-
- User and Repository Link found and validated!
99+
- [<:like:935126958193405962>] Request retrieved!
100+
- Found User ID `%s`.
101+
- User and Repository found and validated!
135102
- [1/1] Finding `%s-requests` channel...
136103
""", userId, (accepted ? "accepted" : "rejected")
137104
).queue();
@@ -150,14 +117,12 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
150117
hook.editOriginalFormat(
151118
"""
152119
[4/5] Handling Join Request...
153-
- [<:like:935126958193405962>] Message retrieved!
154-
- [<:like:935126958193405962>] Message validated!
155-
- Embed found!
156-
- Found User ID `%s`.
157-
- User and Repository Link found and validated!
120+
- [<:like:935126958193405962>] Request retrieved!
121+
- Found User ID `%d`.
122+
- User and Repository Link found and validated!
158123
- [<:like:935126958193405962>] `%s-requests` channel found!
159124
- [1/2] Removing Join Request...
160-
- Archive Thread...
125+
- Archive Thread...
161126
""", userId, (accepted ? "accepted" : "rejected")
162127
).queue();
163128

@@ -179,8 +144,8 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
179144
DatabaseAPI.addUser(username, member.getIdLong());
180145
}
181146
}
182-
183-
channel.sendMessage(getMessage(bot, userId, userLink, repoLink, str == null ? jenkinsUrl : str, hook.getInteraction().getUser(), accepted)).queue(m -> {
147+
148+
channel.sendMessage(getMessage(bot, String.valueOf(userId), userLink, repoLink, str == null ? jenkinsUrl : str, hook.getInteraction().getUser(), accepted)).queue(m -> {
184149
ThreadChannel thread = message.getStartedThread();
185150
if(thread != null && !thread.isArchived()){
186151
thread.getManager().setArchived(true)
@@ -191,15 +156,13 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
191156
hook.editOriginalFormat(
192157
"""
193158
[4/5] Handling Join Request...
194-
- [<:like:935126958193405962>] Message retrieved!
195-
- [<:like:935126958193405962>] Message validated!
196-
- Embed found!
197-
- Found User ID `%s`.
198-
- User and Repository Link found and validated!
159+
- [<:like:935126958193405962>] Request retrieved!
160+
- Found User ID `%d`.
161+
- User and Repository found and validated!
199162
- [<:like:935126958193405962>] `%s-requests` channel found!
200163
- [2/2] Removing Join Request...
201-
- Thread archived!
202-
- Delete Request Message...
164+
- Thread archived!
165+
- Delete Request Message...
203166
""", userId, (accepted ? "accepted" : "rejected")
204167
).queue();
205168

@@ -208,15 +171,13 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
208171
hook.editOriginalFormat(
209172
"""
210173
[5/5] Handling Join Request...
211-
- [<:like:935126958193405962>] Message retrieved!
212-
- [<:like:935126958193405962>] Message validated!
213-
- Embed found!
214-
- Found User ID `%s`.
215-
- User and Repository Link found and validated!
174+
- [<:like:935126958193405962>] Request retrieved!
175+
- Found User ID `%d`.
176+
- User and Repository Link found and validated!
216177
- [<:like:935126958193405962>] `%s-requests` channel found!
217178
- [<:like:935126958193405962>] Join Request removed!
218-
- Thread archived!
219-
- Request Message deleted!
179+
- Thread archived!
180+
- Request Message deleted!
220181
- %s
221182
""", userId, (accepted ? "accepted" : "rejected"), (accepted ? "[1/2] Giving User role...\n - Finding Author role..." : "[1/1] Finishing...")
222183
).queue();
@@ -225,15 +186,13 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
225186
hook.editOriginalFormat(
226187
"""
227188
[<:like:935126958193405962>] Handling of Join Request complete!
228-
- [<:like:935126958193405962>] Message retrieved!
229-
- [<:like:935126958193405962>] Message validated!
230-
- Embed found!
231-
- Found User ID `%s`.
232-
- User and Repository Link found and validated!
189+
- [<:like:935126958193405962>] Request retrieved!
190+
- Found User ID `%d`.
191+
- User and Repository found and validated!
233192
- [<:like:935126958193405962>] `rejected-requests` channel found!
234193
- [<:like:935126958193405962>] Join Request removed!
235-
- Thread archived!
236-
- Request Message deleted!
194+
- Thread archived!
195+
- Request Message deleted!
237196
- [<:like:935126958193405962>] Finished rejecting join request of %s!
238197
""", userId, (member == null ? "*Unknown*" : member.getUser().getEffectiveName())
239198
).queue();
@@ -249,18 +208,16 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
249208
hook.editOriginalFormat(
250209
"""
251210
[5/5] Handling Join Request...
252-
- [<:like:935126958193405962>] Message retrieved!
253-
- [<:like:935126958193405962>] Message validated!
254-
- Embed found!
255-
- Found User ID `%s`.
256-
- User and Repository Link found and validated!
211+
- [<:like:935126958193405962>] Request retrieved!
212+
- Found User ID `%d`.
213+
- User and Repository found and validated!
257214
- [<:like:935126958193405962>] `accepted-requests` channel found!
258215
- [<:like:935126958193405962>] Join Request removed!
259-
- Thread archived!
260-
- Request Message deleted!
216+
- Thread archived!
217+
- Request Message deleted!
261218
- [2/2] Giving User role...
262-
- Found Author Role!
263-
- Applying role to user...
219+
- Found Author Role!
220+
- Applying role to user...
264221
""", userId
265222
).queue();
266223

@@ -274,18 +231,16 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
274231
.queue(v -> hook.editOriginalFormat(
275232
"""
276233
[5/5] Handling Join Request...
277-
- [<:like:935126958193405962>] Message retrieved!
278-
- [<:like:935126958193405962>] Message validated!
279-
- Embed found!
280-
- Found User ID `%s`.
281-
- User and Repository Link found and validated!
234+
- [<:like:935126958193405962>] Request retrieved!
235+
- Found User ID `%d`.
236+
- User and Repository Link found and validated!
282237
- [<:like:935126958193405962>] `accepted-requests` channel found!
283238
- [<:like:935126958193405962>] Join Request removed!
284-
- Thread archived!
285-
- Request Message deleted!
239+
- Thread archived!
240+
- Request Message deleted!
286241
- [<:like:935126958193405962>] Gave User Role!
287-
- Found Author Role!
288-
- Applied Author Role to User!
242+
- Found Author Role!
243+
- Applied Author Role to User!
289244
290245
**Successfully accepted Join Request of user %s!**
291246
""", userId, member.getUser().getEffectiveName()

src/main/java/io/codemc/bot/utils/CommandUtil.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ public static MessageEmbed embedSuccess(String... lines){
5858
return EmbedReply.empty().success(lines).build();
5959
}
6060

61-
public static MessageEmbed requestEmbed(String userLink, String repoLink, String submitter, String description, String id) {
61+
public static MessageEmbed requestEmbed(String userLink, String repoLink, String submitter, String description) {
6262
return getEmbed()
6363
.addField("User/Organisation:", userLink, true)
6464
.addField("Repository:", repoLink, true)
6565
.addField("Submitted by:", submitter, true)
6666
.addField("Description", description, false)
67-
.setFooter(id)
6867
.setTimestamp(Instant.now())
6968
.build();
7069
}

0 commit comments

Comments
 (0)