Skip to content

Commit 7fbc99a

Browse files
committed
fix: parse request data from message embed on database fail
1 parent 9ab305d commit 7fbc99a

File tree

3 files changed

+135
-6
lines changed

3 files changed

+135
-6
lines changed

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

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,19 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
5454
requestChannel.retrieveMessageById(messageId).queue(message -> {
5555
Request req = DatabaseAPI.getRequest(messageId);
5656
if(req == null){
57-
CommandUtil.EmbedReply.from(hook).error("Request not found in Database.").send();
58-
return;
57+
// try to parse from message
58+
if (message.getEmbeds().isEmpty()) {
59+
CommandUtil.EmbedReply.from(hook).error("Request not found in Database and Message has no embeds to parse from.").send();
60+
return;
61+
}
62+
63+
MessageEmbed embed = message.getEmbeds().get(0);
64+
req = fromEmbed(message.getIdLong(), embed);
65+
66+
if (req == null) {
67+
CommandUtil.EmbedReply.from(hook).error("Request not found in Database and data could not be parsed from embed.").send();
68+
return;
69+
}
5970
}
6071

6172
hook.editOriginal(
@@ -283,4 +294,91 @@ static MessageCreateData getMessage(CodeMCBot bot, String userId, String userLin
283294
.setEmbeds(embed)
284295
.build();
285296
}
297+
298+
private static final String GITHUB_PREFIX = "https://github.com/";
299+
300+
@VisibleForTesting
301+
static Request fromEmbed(long messageId, MessageEmbed embed) {
302+
if(embed == null) return null;
303+
304+
String userLink = null;
305+
String repoLink = null;
306+
Long userId = null;
307+
308+
for(MessageEmbed.Field field : embed.getFields()) {
309+
switch(field.getName()) {
310+
case CommandUtil.USER_ORGANISATION -> userLink = field.getValue();
311+
case CommandUtil.REPOSITORY -> repoLink = field.getValue();
312+
case CommandUtil.SUBMITTED_BY -> {
313+
String submitter = field.getValue();
314+
String[] parts = submitter.split("\\s+");
315+
if(parts.length == 2) {
316+
String mention = parts[1];
317+
String idStr;
318+
if(mention.startsWith("(<@") && mention.endsWith(">)")) {
319+
idStr = mention.substring(3, mention.length() - 2);
320+
} else if (mention.startsWith("<@") && mention.endsWith(">")) {
321+
idStr = mention.substring(2, mention.length() - 1);
322+
} else {
323+
LOGGER.warn("Unexpected mention format in submitter field: {}", submitter);
324+
break;
325+
}
326+
327+
try {
328+
userId = Long.parseLong(idStr);
329+
} catch (NumberFormatException e) {
330+
LOGGER.warn("Failed to parse user ID from mention: {}", mention, e);
331+
}
332+
}
333+
}
334+
}
335+
}
336+
337+
if(userLink == null || repoLink == null || userId == null) {
338+
LOGGER.warn("Embed is missing required fields: userLink={}, repoLink={}, userId={}", userLink, repoLink, userId);
339+
return null;
340+
}
341+
342+
String githubName = null;
343+
String repoName = null;
344+
try {
345+
if(userLink.startsWith("[") && userLink.contains("](") && userLink.endsWith(")")) {
346+
int start = userLink.indexOf("](") + 2;
347+
int end = userLink.length() - 1;
348+
String url = userLink.substring(start, end);
349+
if (url.startsWith(GITHUB_PREFIX)) {
350+
githubName = url.substring(GITHUB_PREFIX.length());
351+
} else {
352+
LOGGER.warn("User link does not start with expected prefix: {}", userLink);
353+
}
354+
} else {
355+
LOGGER.warn("User link is not in expected markdown format: {}", userLink);
356+
}
357+
358+
if(repoLink.startsWith("[") && repoLink.contains("](") && repoLink.endsWith(")")) {
359+
int start = repoLink.indexOf("](") + 2;
360+
int end = repoLink.length() - 1;
361+
String url = repoLink.substring(start, end);
362+
if (url.startsWith(GITHUB_PREFIX) && url.contains("/")) {
363+
String path = url.substring(GITHUB_PREFIX.length());
364+
int slashIndex = path.indexOf("/");
365+
githubName = path.substring(0, slashIndex);
366+
repoName = path.substring(slashIndex + 1);
367+
} else {
368+
LOGGER.warn("Repository link does not start with expected prefix or is malformed: {}", repoLink);
369+
}
370+
} else {
371+
LOGGER.warn("Repository link is not in expected markdown format: {}", repoLink);
372+
}
373+
} catch(Exception e) {
374+
LOGGER.warn("Failed to parse GitHub name or repository name from links: userLink={}, repoLink={}", userLink, repoLink, e);
375+
}
376+
377+
if(githubName == null || repoName == null) {
378+
LOGGER.warn("Failed to extract githubName or repoName: githubName={}, repoName={}", githubName, repoName);
379+
return null;
380+
}
381+
382+
return new Request(messageId, userId, githubName, repoName);
383+
}
286384
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@ public static MessageEmbed embedSuccess(String... lines){
5858
return EmbedReply.empty().success(lines).build();
5959
}
6060

61+
public static final String USER_ORGANISATION = "User/Organisation:";
62+
public static final String REPOSITORY = "Repository:";
63+
public static final String SUBMITTED_BY = "Submitted by:";
64+
public static final String DESCRIPTION = "Description";
65+
6166
public static MessageEmbed requestEmbed(String userLink, String repoLink, String submitter, String description) {
6267
return getEmbed()
63-
.addField("User/Organisation:", userLink, true)
64-
.addField("Repository:", repoLink, true)
65-
.addField("Submitted by:", submitter, true)
66-
.addField("Description", description, false)
68+
.addField(USER_ORGANISATION, userLink, true)
69+
.addField(REPOSITORY, repoLink, true)
70+
.addField(SUBMITTED_BY, submitter, true)
71+
.addField(DESCRIPTION, description, false)
6772
.setTimestamp(Instant.now())
6873
.build();
6974
}

src/test/java/io/codemc/bot/utils/TestApplicationHandler.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.codemc.bot.utils;
22

33
import io.codemc.api.database.DatabaseAPI;
4+
import io.codemc.api.database.Request;
45
import io.codemc.api.jenkins.JenkinsAPI;
56
import io.codemc.api.nexus.NexusAPI;
67
import io.codemc.bot.MockCodeMCBot;
@@ -10,6 +11,7 @@
1011
import net.dv8tion.jda.api.entities.MessageEmbed;
1112
import net.dv8tion.jda.api.interactions.InteractionHook;
1213
import net.dv8tion.jda.api.interactions.InteractionType;
14+
import net.dv8tion.jda.api.utils.MarkdownUtil;
1315
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
1416
import org.junit.jupiter.api.DisplayName;
1517
import org.junit.jupiter.api.Test;
@@ -145,4 +147,28 @@ public void testHandleErrors() {
145147
DatabaseAPI.removeRequest(2);
146148
}
147149

150+
@Test
151+
@DisplayName("Test ApplicationHandler#fromMessage")
152+
public void testFromMessage() {
153+
long messageId = 1234567890123456789L;
154+
long userId = 555555555555555555L;
155+
String username = "TestApplicationHandlerFromMessage";
156+
String mention = "<@" + userId + ">";
157+
String repo = "Job";
158+
String description = "This is a test description.";
159+
160+
String userLink = MarkdownUtil.maskedLink(username, "https://github.com/" + username);
161+
String repoLink = MarkdownUtil.maskedLink(repo, "https://github.com/" + username + "/" + repo);
162+
String submitter = String.format("`%s` (%s)", username, mention);
163+
164+
MessageEmbed embed = CommandUtil.requestEmbed(userLink, repoLink, submitter, description);
165+
166+
Request request = ApplicationHandler.fromEmbed(messageId, embed);
167+
assertNotNull(request);
168+
assertEquals(messageId, request.getMessageId());
169+
assertEquals(userId, request.getUserId());
170+
assertEquals(username, request.getGithubName());
171+
assertEquals(repo, request.getRepoName());
172+
}
173+
148174
}

0 commit comments

Comments
 (0)