Skip to content

Commit

Permalink
add repo aliases to $gh and channel specific defaults
Browse files Browse the repository at this point in the history
also bump ver
  • Loading branch information
htmlcsjs committed Aug 1, 2023
1 parent 7c6a947 commit 3ad601d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "xyz.htmlcsjs"
version = "1.0.0"
version = "1.0.1"
var projectBaseName = "CoffeeFloppa"
var mainClass = "xyz.htmlcsjs.coffeeFloppa.asm.FloppaLauncher"

Expand All @@ -34,8 +34,8 @@ application {
}

dependencies {
// implementation("com.discord4j:discord4j-core:3.2.3")
implementation("com.github.htmlcsjs:Discord4Floppa:34ef8d4")
// implementation("com.discord4j:discord4j-core:3.2.4")
implementation("com.github.htmlcsjs:Discord4Floppa:c9e32fe")
implementation("ch.qos.logback:logback-classic:1.4.6")
implementation("com.googlecode.json-simple:json-simple:1.1.1")
implementation("me.xdrop:fuzzywuzzy:1.4.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package xyz.htmlcsjs.coffeeFloppa.commands;

import discord4j.common.util.Snowflake;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.channel.MessageChannel;
import discord4j.core.object.entity.channel.ThreadChannel;
import discord4j.core.spec.EmbedCreateSpec;
import discord4j.discordjson.possible.Possible;
import discord4j.rest.util.AllowedMentions;
Expand All @@ -21,14 +24,41 @@
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class GithubIssueCommand implements ICommand {

private static final String apiRoot = "https://api.github.com/";
private static final Map<String, String> aliasMap = new HashMap<>();
private static final Map<String, String> categoryAliasMap = new HashMap<>();


public GithubIssueCommand() {
aliasMap.clear();
for (String str : FloppaTomlConfig.repoAliases.split(";")) {
List<String> split = new ArrayList<>(List.of(str.split(":")));
String alias = split.remove(0);
String repo;
if (split.size() > 1) {
repo = String.join(":", split);
} else {
repo = split.get(0);
}
aliasMap.put(alias, repo);
}
categoryAliasMap.clear();
for (String str : FloppaTomlConfig.channelAliases.split(";")) {
List<String> split = new ArrayList<>(List.of(str.split(":")));
String alias = split.remove(0);
String repo;
if (split.size() > 1) {
repo = String.join(":", split);
} else {
repo = split.get(0);
}
categoryAliasMap.put(alias, repo);
}
}

@Override
public @NotNull String getName() {
Expand All @@ -44,21 +74,44 @@ public String execute(Message message) {
.limit(5)
.forEach(matchResult -> issueMap.put(matchResult.group(1), matchResult.group(2)));

CommandUtil.ghIssueSmallPattern.matcher(message.getContent())
.results()
.limit(5 - issueMap.size())
.forEach(matchResult -> {
if (matchResult.group(1) == null) {
MessageChannel channel = message.getChannel().block();
if (channel != null && categoryAliasMap.containsKey(channel.getId().asString())) {
issueMap.put(aliasMap.get(categoryAliasMap.get(channel.getId().asString())), matchResult.group(2));
} else if (channel instanceof ThreadChannel thread) {
Optional<Snowflake> parentId = thread.getParentId();
if (parentId.isPresent() && categoryAliasMap.containsKey(parentId.get().asString())) {
issueMap.put(aliasMap.get(categoryAliasMap.get(parentId.get().asString())), matchResult.group(2));
}
}
} else if (aliasMap.containsKey(matchResult.group(1))){
issueMap.put(aliasMap.get(matchResult.group(1)), matchResult.group(2));
}
});

HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).followRedirects(HttpClient.Redirect.ALWAYS).connectTimeout(Duration.ofSeconds(5)).build();
List<EmbedCreateSpec> embeds = new ArrayList<>();

if (issueMap.isEmpty()) {
return "No valid issue descriptors supplied\nUse the format `owner/repo#issueid`";
return "No valid issue descriptors supplied\nUse the format `owner/repo#issueid` or `alias#issueid`";
}

issueMap.forEach((repo, id) -> {
/*if (!repo.matches("[a-z\\d](?:[a-z\\d]|-(?=[a-z\\d])){0,38}/[A-Za-z0-9_.\\-]+")) {
return; //TODO add aliases
}*/
if (!repo.matches("[\\w.-]+/[\\w.-]+")) {
if (aliasMap.containsKey(repo)) {
repo = aliasMap.get(repo);
} else {
return;
}
}

HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create(apiRoot + "repos/" + repo + "/issues/" + id));
if (!FloppaTomlConfig.GitHubToken.isEmpty()) {
builder.header("Authorization", String.format("Bearer %s", FloppaTomlConfig.GitHubToken));
if (!FloppaTomlConfig.githubToken.isEmpty()) {
builder.header("Authorization", String.format("Bearer %s", FloppaTomlConfig.githubToken));
}
try {
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static Mono<Object> executeMessage(Message message, String msgContent) {
Mono<Object> amongVal = Mono.empty();

// If the first char is the prefix
if (msgContent.length() > 0 && msgContent.charAt(0) == FloppaTomlConfig.prefix.charAt(0) && !message.getAuthor().get().isBot() && !message.mentionsEveryone()) {
if (!msgContent.isEmpty() && msgContent.charAt(0) == FloppaTomlConfig.prefix.charAt(0) && !message.getAuthor().get().isBot() && !message.mentionsEveryone()) {
try {
// get the command
String commandCall = msgContent.toLowerCase().split(" ")[0].replace(FloppaTomlConfig.prefix, " ").strip();
Expand All @@ -109,7 +109,7 @@ private static Mono<Object> executeMessage(Message message, String msgContent) {
// we do a little bit of executing
sendMessage(message, command);
} catch (IndexOutOfBoundsException ignored) {}
} else if (CommandUtil.ghIssuePattern.matcher(msgContent).matches() && !message.getAuthor().get().isBot()) {
} else if ((CommandUtil.ghIssuePattern.matcher(msgContent).matches() || CommandUtil.ghIssueSmallPattern.matcher(msgContent).matches())&& !message.getAuthor().get().isBot()) {
sendMessage(message, commands.get("gh"));
} else {
for (String key : searchCommands.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class CommandUtil {
public static final Pattern urlPattern = Pattern.compile("(?:^|[\\W])((ht|f)tp(s?):\\/\\/|www\\.)(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
public static final Pattern ghIssuePattern = Pattern.compile(".*?([a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*\\/[a-zA-Z0-9_\\-\\.]+)#(\\d+).*?", Pattern.MULTILINE);
public static final Pattern ghIssueSmallPattern = Pattern.compile(".*?([\\w.]+)?#(\\d+).*?", Pattern.MULTILINE);

public static boolean getAllowedToRun(Message message) {
Set<Snowflake> userRoleIDs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ public class FloppaTomlConfig {
public static String errorChannel;

@TomlConfig.ConfigElement(location = "gh_token")
public static String GitHubToken = "";
public static String githubToken = "";

@TomlConfig.ConfigElement(location = "repo_aliases")
public static String repoAliases = "";
@TomlConfig.ConfigElement(location = "channel_aliases")
public static String channelAliases = "";
}

0 comments on commit 3ad601d

Please sign in to comment.