Skip to content

Commit fe3e8c4

Browse files
committed
make the list of issues compact when more than 2 are referanced
1 parent 3ad601d commit fe3e8c4

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

src/main/java/xyz/htmlcsjs/coffeeFloppa/commands/GithubIssueCommand.java

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,44 +68,50 @@ public GithubIssueCommand() {
6868
@Nullable
6969
@Override
7070
public String execute(Message message) {
71-
Map<String, String> issueMap = new HashMap<>();
71+
List<IssueListItem> issueList = new ArrayList<>();
7272
CommandUtil.ghIssuePattern.matcher(message.getContent())
7373
.results()
74-
.limit(5)
75-
.forEach(matchResult -> issueMap.put(matchResult.group(1), matchResult.group(2)));
74+
.forEach(matchResult -> issueList.add(new IssueListItem(matchResult.group(1), matchResult.group(2))));
7675

7776
CommandUtil.ghIssueSmallPattern.matcher(message.getContent())
7877
.results()
79-
.limit(5 - issueMap.size())
8078
.forEach(matchResult -> {
8179
if (matchResult.group(1) == null) {
8280
MessageChannel channel = message.getChannel().block();
8381
if (channel != null && categoryAliasMap.containsKey(channel.getId().asString())) {
84-
issueMap.put(aliasMap.get(categoryAliasMap.get(channel.getId().asString())), matchResult.group(2));
82+
issueList.add(new IssueListItem(aliasMap.get(categoryAliasMap.get(channel.getId().asString())), matchResult.group(2)));
8583
} else if (channel instanceof ThreadChannel thread) {
8684
Optional<Snowflake> parentId = thread.getParentId();
8785
if (parentId.isPresent() && categoryAliasMap.containsKey(parentId.get().asString())) {
88-
issueMap.put(aliasMap.get(categoryAliasMap.get(parentId.get().asString())), matchResult.group(2));
86+
issueList.add(new IssueListItem(aliasMap.get(categoryAliasMap.get(parentId.get().asString())), matchResult.group(2)));
8987
}
9088
}
9189
} else if (aliasMap.containsKey(matchResult.group(1))){
92-
issueMap.put(aliasMap.get(matchResult.group(1)), matchResult.group(2));
90+
issueList.add(new IssueListItem(aliasMap.get(matchResult.group(1)), matchResult.group(2)));
9391
}
9492
});
9593

96-
HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).followRedirects(HttpClient.Redirect.ALWAYS).connectTimeout(Duration.ofSeconds(5)).build();
94+
HttpClient client = HttpClient.newBuilder()
95+
.version(HttpClient.Version.HTTP_1_1)
96+
.followRedirects(HttpClient.Redirect.ALWAYS)
97+
.connectTimeout(Duration.ofSeconds(5)).build();
9798
List<EmbedCreateSpec> embeds = new ArrayList<>();
99+
StringBuilder msgBody = new StringBuilder();
98100

99-
if (issueMap.isEmpty()) {
101+
if (message.getContent().startsWith(String.format("$%s", getName())) && issueList.isEmpty()) {
100102
return "No valid issue descriptors supplied\nUse the format `owner/repo#issueid` or `alias#issueid`";
103+
} else if (issueList.isEmpty()) {
104+
return null;
101105
}
102106

103-
issueMap.forEach((repo, id) -> {
107+
for (IssueListItem item : issueList) {
108+
String repo = item.repo;
109+
String id = item.id;
104110
if (!repo.matches("[\\w.-]+/[\\w.-]+")) {
105111
if (aliasMap.containsKey(repo)) {
106112
repo = aliasMap.get(repo);
107113
} else {
108-
return;
114+
continue;
109115
}
110116
}
111117

@@ -117,12 +123,8 @@ public String execute(Message message) {
117123
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
118124
JSONObject issueJSON = (JSONObject) new JSONParser().parse(response.body());
119125
if (response.statusCode() != 200) {
120-
embeds.add(EmbedCreateSpec.builder()
121-
.title(String.format("Couldn't find issue %s in repo %s", id, repo))
122-
.addField("status", String.format("`%d`", response.statusCode()), true)
123-
.addField("message", String.format("`%s`", issueJSON.get("message")), true)
124-
.build());
125-
} else {
126+
msgBody.append(String.format("Couldn't find issue %s in repo `%s`. HTTP status: `%d`. Message: `%s`\n", id, repo, response.statusCode(), issueJSON.get("message")));
127+
} else if (issueList.size() <= 2) {
126128
EmbedCreateSpec.Builder embedBuilder = EmbedCreateSpec.builder()
127129
.title(Possible.of((String) issueJSON.get("title")))
128130
.url((String) issueJSON.get("html_url"));
@@ -133,7 +135,7 @@ public String execute(Message message) {
133135
StringBuilder body = new StringBuilder();
134136
int i = 0;
135137
for (String s : rawBody.split("\n")) {
136-
if (i > 10) {
138+
if (i > 5) {
137139
body = new StringBuilder(body.toString().trim() + "...\n");
138140
break;
139141
}
@@ -146,20 +148,32 @@ public String execute(Message message) {
146148
}
147149
embedBuilder.timestamp(Instant.parse((String) issueJSON.get("updated_at")));
148150
embeds.add(embedBuilder.build());
151+
} else {
152+
msgBody.append("[#%s: %s](<https://github.com/%s/issues/%s>\n".formatted(id, issueJSON.get("title"), repo, id));
149153
}
150154
} catch (IOException | InterruptedException | ParseException e) {
151155
CoffeeFloppa.handleException(e);
152156
}
153-
});
157+
}
158+
159+
if (!msgBody.isEmpty()) {
160+
msgBody = new StringBuilder().append("Issues referenced:\n").append(msgBody);
161+
}
162+
163+
StringBuilder finalMsgBody = msgBody;
154164
MessageHandler.sendRegisterMessage(message, message.getChannel().flatMap(channel -> channel.createMessage()
155165
.withEmbeds(embeds)
156166
.withMessageReference(message.getId())
157-
.withAllowedMentions(AllowedMentions.suppressEveryone())));
167+
.withAllowedMentions(AllowedMentions.suppressEveryone())
168+
.withContent(finalMsgBody.toString())));
158169
return null;
159170
}
160171

161172
@Override
162173
public String helpInfo() {
163174
return "Provides links to issues for github repos";
164175
}
176+
private record IssueListItem(String repo, String id) {
177+
178+
}
165179
}

0 commit comments

Comments
 (0)