Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
feat: ログイン時、不具合報告・フィードバックがクローズされていたら通知する (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
book000 authored Sep 21, 2022
1 parent facb978 commit b62bbc4
Show file tree
Hide file tree
Showing 4 changed files with 422 additions and 143 deletions.
93 changes: 20 additions & 73 deletions src/main/java/com/jaoafa/mymaid4/command/Cmd_Bug.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,26 @@
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.meta.CommandMeta;
import com.jaoafa.mymaid4.Main;
import com.jaoafa.mymaid4.lib.CommandPremise;
import com.jaoafa.mymaid4.lib.MyMaidCommand;
import com.jaoafa.mymaid4.lib.MyMaidLibrary;
import com.jaoafa.mymaid4.lib.NMSManager;
import com.jaoafa.mymaid4.lib.*;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import okhttp3.*;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class Cmd_Bug extends MyMaidLibrary implements CommandPremise {
static long sendTime = -1L;
static IssueManager.Repository repo = IssueManager.Repository.MyMaid4;

@Override
public MyMaidCommand.Detail details() {
Expand Down Expand Up @@ -177,71 +167,28 @@ void createIssueBook(CommandContext<CommandSender> context) {
return;
}

String title = meta.getTitle();
String author = meta.getAuthor();
List<Component> pages = meta.pages();
String body = pages.stream()
.skip(1)
.map(o -> ChatColor.stripColor(PlainTextComponentSerializer.plainText().serialize(o)))
.collect(Collectors.joining("\n")) +
MessageFormat.format("""
## システム追加情報
- 作成者: [{0} (`{1}`)]({2})
- 執筆者: {3}""",
player.getName(),
player.getUniqueId().toString(),
String.format("https://users.jaoafa.com/%s", player.getUniqueId()),
author);

new BukkitRunnable() {
public void run() {
String accessToken = Main.getMyMaidConfig().getGitHubAccessToken();
if (accessToken == null) {
SendMessage(player, details(), "不具合報告に必要な設定情報が見つからなかったため、不具合報告ができませんでした。");
return;
IssueManager.CreateIssueResponse response = IssueManager.addIssue(player, repo, meta, List.of(IssueManager.Label.BUG));
if (response.getErrorMessage() != null) {
// Exception
SendMessage(player, details(), "不具合報告処理に失敗しました: %s".formatted(response.getErrorMessage()));
} else if (!response.getStatus()) {
// Error
SendMessage(player, details(), String.format("不具合報告に失敗しました: %d", response.getStatusCode()));
}

String repo = "jaoafa/MyMaid4";
String url = String.format("https://api.github.com/repos/%s/issues", repo);
JSONObject json = new JSONObject()
.put("title", title)
.put("body", body)
.put("labels", new JSONArray()
.put("\uD83D\uDC1Bbug"));

try {
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(json.toString(), MediaType.parse("application/json; charset=UTF-8"));
Request request = new Request.Builder()
.url(url)
.header("Authorization", String.format("token %s", accessToken))
.post(requestBody)
.build();
JSONObject obj;
try (Response response = client.newCall(request).execute()) {
if (response.code() != 201) {
SendMessage(player, details(), String.format("不具合報告に失敗しました: %d", response.code()));
return;
}
obj = new JSONObject(Objects.requireNonNull(response.body()).string());
}

int issueNum = obj.getInt("number");
SendMessage(player, details(), Component.text().append(
Component.text("不具合報告に成功しました。", NamedTextColor.GREEN),
Component.text("こちら", NamedTextColor.AQUA, TextDecoration.UNDERLINED)
.hoverEvent(HoverEvent.showText(Component.text()))
.clickEvent(ClickEvent.openUrl(MessageFormat.format("https://github.com/{0}/issues/{1}", repo, issueNum))),
Component.text("から確認できます。", NamedTextColor.GREEN)
).build());
SendMessage(player, details(), "不具合の改善報告等は致しかねますので、上記リンク先を定期的にご確認ください。ご報告いただきありがとうございました!");
inv.setItemInMainHand(null);
sendTime = System.currentTimeMillis();
} catch (IOException e) {
SendMessage(player, details(), String.format("不具合報告に失敗しました: %s", e.getMessage()));
MyMaidLibrary.reportError(getClass(), e);
}
// Success
SendMessage(player, details(), Component.text().append(
Component.text("不具合報告に成功しました。", NamedTextColor.GREEN),
Component.text("こちら", NamedTextColor.AQUA, TextDecoration.UNDERLINED)
.hoverEvent(HoverEvent.showText(Component.text()))
.clickEvent(ClickEvent.openUrl(MessageFormat.format("https://github.com/{0}/issues/{1}", repo.getRepo(), response.getIssueNumber()))),
Component.text("から確認できます。", NamedTextColor.GREEN)
).build());
SendMessage(player, details(), "不具合の改善連絡等は致しかねますので、上記リンク先を定期的にご確認ください。ご報告いただきありがとうございました!");
inv.setItemInMainHand(null);
sendTime = System.currentTimeMillis();
}
}.runTaskAsynchronously(Main.getJavaPlugin());
}
Expand Down
90 changes: 20 additions & 70 deletions src/main/java/com/jaoafa/mymaid4/command/Cmd_Feedback.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,26 @@
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.meta.CommandMeta;
import com.jaoafa.mymaid4.Main;
import com.jaoafa.mymaid4.lib.CommandPremise;
import com.jaoafa.mymaid4.lib.MyMaidCommand;
import com.jaoafa.mymaid4.lib.MyMaidLibrary;
import com.jaoafa.mymaid4.lib.NMSManager;
import com.jaoafa.mymaid4.lib.*;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import okhttp3.*;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.JSONObject;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class Cmd_Feedback extends MyMaidLibrary implements CommandPremise {
static long sendTime = -1L;
static IssueManager.Repository repo = IssueManager.Repository.jaoMinecraftServer;

@Override
public MyMaidCommand.Detail details() {
Expand Down Expand Up @@ -153,69 +144,28 @@ void createIssueBook(CommandContext<CommandSender> context) {
return;
}

String title = meta.getTitle();
String author = meta.getAuthor();
List<Component> pages = meta.pages();
String body = pages.stream()
.skip(1)
.map(o -> ChatColor.stripColor(PlainTextComponentSerializer.plainText().serialize(o)))
.collect(Collectors.joining("\n")) +
MessageFormat.format("""
## システム追加情報
- 作成者: [{0} (`{1}`)]({2})
- 執筆者: {3}""",
player.getName(),
player.getUniqueId().toString(),
String.format("https://users.jaoafa.com/%s", player.getUniqueId()),
author);

new BukkitRunnable() {
public void run() {
String accessToken = Main.getMyMaidConfig().getGitHubAccessToken();
if (accessToken == null) {
SendMessage(player, details(), "不具合報告に必要な設定情報が見つからなかったため、不具合報告ができませんでした。");
return;
IssueManager.CreateIssueResponse response = IssueManager.addIssue(player, repo, meta, null);
if (response.getErrorMessage() != null) {
// Exception
SendMessage(player, details(), "フィードバック処理に失敗しました: %s".formatted(response.getErrorMessage()));
} else if (!response.getStatus()) {
// Error
SendMessage(player, details(), String.format("フィードバックの送信に失敗しました: %d", response.getStatusCode()));
}

String repo = "jaoafa/jao-Minecraft-Server";
String url = String.format("https://api.github.com/repos/%s/issues", repo);
JSONObject json = new JSONObject()
.put("title", title)
.put("body", body);

try {
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(json.toString(), MediaType.parse("application/json; charset=UTF-8"));
Request request = new Request.Builder()
.url(url)
.header("Authorization", String.format("token %s", accessToken))
.post(requestBody)
.build();
JSONObject obj;
try (Response response = client.newCall(request).execute()) {
if (response.code() != 201) {
SendMessage(player, details(), String.format("不具合報告に失敗しました: %d", response.code()));
return;
}
obj = new JSONObject(Objects.requireNonNull(response.body()).string());
}

int issueNum = obj.getInt("number");
SendMessage(player, details(), Component.text().append(
Component.text("フィードバックの送信に成功しました。", NamedTextColor.GREEN),
Component.text("こちら", NamedTextColor.AQUA, TextDecoration.UNDERLINED)
.hoverEvent(HoverEvent.showText(Component.text()))
.clickEvent(ClickEvent.openUrl(MessageFormat.format("https://github.com/{0}/issues/{1}", repo, issueNum))),
Component.text("から確認できます。", NamedTextColor.GREEN)
).build());
SendMessage(player, details(), "フィードバックに対する直接の返信は致しかねますので、上記リンク先を定期的にご確認ください。ありがとうございました!");
inv.setItemInMainHand(null);
sendTime = System.currentTimeMillis();
} catch (IOException e) {
SendMessage(player, details(), String.format("フィードバック処理に失敗しました: %s", e.getMessage()));
MyMaidLibrary.reportError(getClass(), e);
}
// Success
SendMessage(player, details(), Component.text().append(
Component.text("フィードバックの送信に成功しました。", NamedTextColor.GREEN),
Component.text("こちら", NamedTextColor.AQUA, TextDecoration.UNDERLINED)
.hoverEvent(HoverEvent.showText(Component.text()))
.clickEvent(ClickEvent.openUrl(MessageFormat.format("https://github.com/{0}/issues/{1}", repo.getRepo(), response.getIssueNumber()))),
Component.text("から確認できます。", NamedTextColor.GREEN)
).build());
SendMessage(player, details(), "フィードバックに対する直接の返信は致しかねますので、上記リンク先を定期的にご確認ください。ありがとうございました!");
inv.setItemInMainHand(null);
sendTime = System.currentTimeMillis();
}
}.runTaskAsynchronously(Main.getJavaPlugin());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* jaoLicense
*
* Copyright (c) 2022 jao Minecraft Server
*
* The following license applies to this project: jaoLicense
*
* Japanese: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE.md
* English: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE-en.md
*/

package com.jaoafa.mymaid4.event;

import com.jaoafa.mymaid4.Main;
import com.jaoafa.mymaid4.lib.EventPremise;
import com.jaoafa.mymaid4.lib.IssueManager;
import com.jaoafa.mymaid4.lib.MyMaidLibrary;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Date;
import java.util.List;

public class Event_ClosedIssueNotifier extends MyMaidLibrary implements Listener, EventPremise {
@Override
public String description() {
return "ログイン時、bugコマンドやfeedbackコマンドによって作成されたissueがクローズされていたら通知します。";
}

@EventHandler
public void onJoinClearCache(PlayerJoinEvent event) {
Player player = event.getPlayer();
new BukkitRunnable() {
public void run() {
List<IssueManager.Issue> issues = IssueManager.getPlayerIssues(player);
if (issues.isEmpty()) {
return;
}
List<IssueManager.Issue> closedIssues = issues.stream()
.filter(issue -> IssueManager.isClosedIssue(issue.repository(), issue.issueNumber()))
.toList();
if (closedIssues.isEmpty()) {
return;
}
player.sendMessage(Component.text().append(
Component.text("["),
Component.text("BugFeedback", NamedTextColor.YELLOW),
Component.text("]"),
Component.space(),
Component.text("あなたが送信した不具合報告・フィードバックが完了済みとしてマークされました。", NamedTextColor.GREEN)
));

for (IssueManager.Issue issue : closedIssues) {
String datetime = sdfFormat(new Date(issue.time()));
player.sendMessage(Component.text().append(
Component.text("["),
Component.text("BugFeedback", NamedTextColor.YELLOW),
Component.text("]"),
Component.space(),
Component.text(datetime + " に送信された", NamedTextColor.GREEN),
Component.text(IssueManager.getRepoIssueType(issue.repository()), NamedTextColor.GREEN),
Component.text(":", NamedTextColor.GREEN),
Component.space(),
Component.text("%s#%d".formatted(issue.repository().getRepo(), issue.issueNumber()), NamedTextColor.AQUA)
.hoverEvent(HoverEvent.showText(Component.text("クリックで不具合報告・フィードバックのページを開きます", NamedTextColor.GREEN)))
.clickEvent(ClickEvent.openUrl("https://github.com/%s/issues/%d".formatted(issue.repository().getRepo(), issue.issueNumber())))
));
IssueManager.removePlayerIssue(player, issue.issueNumber());
}

}
}.runTaskAsynchronously(Main.getJavaPlugin());
}
}
Loading

0 comments on commit b62bbc4

Please sign in to comment.