|
| 1 | +package net.seesharpsoft.intellij.plugins.csv; |
| 2 | + |
| 3 | +import com.intellij.ide.DataManager; |
| 4 | +import com.intellij.openapi.actionSystem.CommonDataKeys; |
| 5 | +import com.intellij.openapi.actionSystem.DataContext; |
| 6 | +import com.intellij.openapi.application.ApplicationInfo; |
| 7 | +import com.intellij.openapi.application.ApplicationManager; |
| 8 | +import com.intellij.openapi.diagnostic.ErrorReportSubmitter; |
| 9 | +import com.intellij.openapi.diagnostic.IdeaLoggingEvent; |
| 10 | +import com.intellij.openapi.diagnostic.SubmittedReportInfo; |
| 11 | +import com.intellij.openapi.progress.*; |
| 12 | +import com.intellij.openapi.project.Project; |
| 13 | +import com.intellij.util.Consumer; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | +import org.jetbrains.annotations.Nullable; |
| 16 | +import org.jetbrains.plugins.github.api.*; |
| 17 | +import org.jetbrains.plugins.github.api.data.GithubIssueState; |
| 18 | +import org.jetbrains.plugins.github.api.data.GithubResponsePage; |
| 19 | +import org.jetbrains.plugins.github.api.data.GithubSearchedIssue; |
| 20 | +import org.jetbrains.plugins.github.api.data.request.GithubRequestPagination; |
| 21 | +import org.jetbrains.plugins.github.authentication.GithubAuthenticationManager; |
| 22 | +import org.jetbrains.plugins.github.authentication.accounts.GithubAccount; |
| 23 | + |
| 24 | +import java.awt.*; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Collections; |
| 27 | + |
| 28 | +public class CsvGithubIssueSubmitter extends ErrorReportSubmitter { |
| 29 | + |
| 30 | + public static final String GIT_USER = "SeeSharpSoft"; |
| 31 | + public static final String GIT_REPO = "intellij-csv-validator"; |
| 32 | + public static final GHRepositoryPath GITHUB_FULL_PATH = new GHRepositoryPath(GIT_USER, GIT_REPO); |
| 33 | + |
| 34 | + private static class CsvGithubSubmitException extends RuntimeException { |
| 35 | + CsvGithubSubmitException(Throwable exception) { |
| 36 | + super(exception); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @NotNull |
| 41 | + @Override |
| 42 | + public String getReportActionText() { |
| 43 | + return "Report to 'CSV Table Editor' (Github)"; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean submit(IdeaLoggingEvent @NotNull [] events, @Nullable String additionalInfo, @NotNull Component parentComponent, @NotNull Consumer<? super SubmittedReportInfo> consumer) { |
| 48 | + final DataContext dataContext = DataManager.getInstance().getDataContext(parentComponent); |
| 49 | + final Project project = CommonDataKeys.PROJECT.getData(dataContext); |
| 50 | + |
| 51 | + for (IdeaLoggingEvent event : events) { |
| 52 | + if (!submit(event, additionalInfo, project, consumer)) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + } |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + protected boolean submit(IdeaLoggingEvent event, String additionalInfo, Project project, Consumer<? super SubmittedReportInfo> consumer) { |
| 60 | + GithubAuthenticationManager githubAuthManager = GithubAuthenticationManager.getInstance(); |
| 61 | + if (!githubAuthManager.ensureHasAccounts(project)) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + GithubAccount githubAccount = githubAuthManager.getSingleOrDefaultAccount(project); |
| 65 | + assert githubAccount != null; |
| 66 | + // the cast shouldn't be needed due to inheritance, but Java complains for some reason in 2022.1 |
| 67 | + GithubApiRequestExecutor githubExecutor = GithubApiRequestExecutor.class.cast(GithubApiRequestExecutorManager.getInstance().getExecutor(githubAccount, project)); |
| 68 | + |
| 69 | + Task submitTask = new Task.Backgroundable(project, getReportActionText()) { |
| 70 | + @Override |
| 71 | + public void run(@NotNull ProgressIndicator indicator) { |
| 72 | + submitToGithub(event, additionalInfo, githubExecutor, consumer, indicator); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + if (ApplicationManager.getApplication().isUnitTestMode()) { |
| 77 | + submitTask.run(new EmptyProgressIndicator()); |
| 78 | + } else { |
| 79 | + ProgressManager.getInstance().run(submitTask); |
| 80 | + } |
| 81 | + |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + private void submitToGithub(IdeaLoggingEvent event, |
| 86 | + String additionalInfo, |
| 87 | + GithubApiRequestExecutor githubExecutor, |
| 88 | + Consumer<? super SubmittedReportInfo> consumer, |
| 89 | + ProgressIndicator progressIndicator) { |
| 90 | + try { |
| 91 | + SubmittedReportInfo.SubmissionStatus status; |
| 92 | + |
| 93 | + String issueTitle = getIssueTitle(event); |
| 94 | + String issueDetails = getIssueDetails(event, additionalInfo); |
| 95 | + String foundIssueId = searchExistingIssues(githubExecutor, issueTitle, progressIndicator); |
| 96 | + |
| 97 | + if (foundIssueId == null) { |
| 98 | + githubExecutor.execute(progressIndicator, createNewIssue(issueTitle, issueDetails)); |
| 99 | + status = SubmittedReportInfo.SubmissionStatus.NEW_ISSUE; |
| 100 | + } else { |
| 101 | + githubExecutor.execute(progressIndicator, updateExistingIssue(foundIssueId, issueDetails)); |
| 102 | + status = SubmittedReportInfo.SubmissionStatus.DUPLICATE; |
| 103 | + } |
| 104 | + consumer.consume(new SubmittedReportInfo(status)); |
| 105 | + } catch (IOException exc) { |
| 106 | + throw new CsvGithubSubmitException(exc); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + protected GithubApiRequest updateExistingIssue(String issueId, String content) throws IOException { |
| 111 | + return GithubApiRequests.Repos.Issues.Comments.create( |
| 112 | + GithubServerPath.DEFAULT_SERVER, |
| 113 | + GIT_USER, |
| 114 | + GIT_REPO, |
| 115 | + issueId, |
| 116 | + content |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + protected GithubApiRequest createNewIssue(String title, String content) throws IOException { |
| 121 | + return GithubApiRequests.Repos.Issues.create( |
| 122 | + GithubServerPath.DEFAULT_SERVER, |
| 123 | + GIT_USER, |
| 124 | + GIT_REPO, |
| 125 | + title, |
| 126 | + content, |
| 127 | + null, |
| 128 | + Collections.emptyList(), |
| 129 | + Collections.emptyList()); |
| 130 | + } |
| 131 | + |
| 132 | + protected String searchExistingIssues(GithubApiRequestExecutor githubExecutor, String needle, ProgressIndicator progressIndicator) throws IOException { |
| 133 | + GithubApiRequest<GithubResponsePage<GithubSearchedIssue>> existingIssueRequest = |
| 134 | + GithubApiRequests.Search.Issues.get( |
| 135 | + GithubServerPath.DEFAULT_SERVER, |
| 136 | + GITHUB_FULL_PATH, |
| 137 | + GithubIssueState.open.name(), |
| 138 | + null, |
| 139 | + needle, |
| 140 | + new GithubRequestPagination(1, 1) |
| 141 | + ); |
| 142 | + |
| 143 | + String issueId = null; |
| 144 | + GithubResponsePage<GithubSearchedIssue> foundIssuesPage = githubExecutor.execute(progressIndicator, existingIssueRequest); |
| 145 | + if (foundIssuesPage != null && !foundIssuesPage.getItems().isEmpty()) { |
| 146 | + GithubSearchedIssue foundIssue = foundIssuesPage.getItems().get(0); |
| 147 | + if (foundIssue.getTitle().equals(needle)) { |
| 148 | + issueId = Long.toString(foundIssue.getNumber()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return issueId; |
| 153 | + } |
| 154 | + |
| 155 | + protected String getIssueTitle(IdeaLoggingEvent event) { |
| 156 | + String throwableText = event.getThrowableText(); |
| 157 | + int index = Math.min(throwableText.indexOf("\r"), throwableText.indexOf("\n")); |
| 158 | + return "[Automated Report] " + event.getThrowableText().substring(0, index); |
| 159 | + } |
| 160 | + |
| 161 | + protected String getIssueDetails(IdeaLoggingEvent event, String additionalInfo) { |
| 162 | + return "Message\n---\n" + |
| 163 | + (additionalInfo != null && !additionalInfo.isEmpty() ? additionalInfo : "<no message>") + |
| 164 | + "\n\n" + |
| 165 | + "Stacktrace\n---\n" + |
| 166 | + event.getThrowableText() + |
| 167 | + "\n\n" + |
| 168 | + "Plugin\n---\n" + |
| 169 | + getPluginDescriptor().getPluginClassLoader() + |
| 170 | + "\n\n" + |
| 171 | + "IDE\n---\n" + |
| 172 | + ApplicationInfo.getInstance().getVersionName() + |
| 173 | + " (" + |
| 174 | + ApplicationInfo.getInstance().getBuild() + |
| 175 | + ")"; |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public String getPrivacyNoticeText() { |
| 180 | + return "An automated issue report contains the provided message, " + |
| 181 | + "the stacktrace, " + |
| 182 | + "the plugin class loader info (" + |
| 183 | + getPluginDescriptor().getPluginClassLoader() + |
| 184 | + "), the used IDE version name (" + |
| 185 | + ApplicationInfo.getInstance().getVersionName() + |
| 186 | + ") and build number (" + |
| 187 | + ApplicationInfo.getInstance().getBuild() + |
| 188 | + "). " + |
| 189 | + "By sending the report I agree to the information be used for resolving this and further related issues. " + |
| 190 | + "All provided information will be publicly available at " + |
| 191 | + String.format("https://%s/%s/%s/issues", GithubServerPath.DEFAULT_SERVER, GIT_USER, GIT_REPO); |
| 192 | + } |
| 193 | +} |
0 commit comments