-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathCsvGithubIssueSubmitter.java
More file actions
261 lines (225 loc) · 11 KB
/
CsvGithubIssueSubmitter.java
File metadata and controls
261 lines (225 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package net.seesharpsoft.intellij.plugins.csv;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.ErrorReportSubmitter;
import com.intellij.openapi.diagnostic.IdeaLoggingEvent;
import com.intellij.openapi.diagnostic.SubmittedReportInfo;
import com.intellij.openapi.progress.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.Strings;
import com.intellij.util.Consumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.github.api.*;
import org.jetbrains.plugins.github.api.data.GithubIssueState;
import org.jetbrains.plugins.github.api.data.GithubResponsePage;
import org.jetbrains.plugins.github.api.data.GithubSearchedIssue;
import org.jetbrains.plugins.github.api.data.request.GithubRequestPagination;
import org.jetbrains.plugins.github.authentication.GHAccountAuthData;
import org.jetbrains.plugins.github.authentication.GHAccountsUtil;
import org.jetbrains.plugins.github.authentication.accounts.GithubAccount;
import org.jetbrains.plugins.github.util.GHCompatibilityUtil;
import java.awt.*;
import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class CsvGithubIssueSubmitter extends ErrorReportSubmitter {
private static final int FALLBACK_ISSUE_TITLE_LENGTH = 600;
public static final String GIT_USER = "SeeSharpSoft";
public static final String GIT_REPO = "intellij-csv-validator";
public static final GHRepositoryPath GITHUB_FULL_PATH = new GHRepositoryPath(GIT_USER, GIT_REPO);
private static final String REPORT_ACTION_TEXT = "Report to 'CSV Editor' (Github)";
private static ScheduledFuture<?> recentlySentReport = null;
private static final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
private static class CsvGithubSubmitException extends RuntimeException {
CsvGithubSubmitException(Throwable exception) {
super(exception);
}
}
@NotNull
@Override
public String getReportActionText() {
return REPORT_ACTION_TEXT;
}
@Override
public synchronized boolean submit(IdeaLoggingEvent @NotNull [] events, @Nullable String additionalInfo, @NotNull Component parentComponent, @NotNull Consumer<? super SubmittedReportInfo> consumer) {
if (reportWasRecentlySent()) return true;
final DataContext dataContext = DataManager.getInstance().getDataContext(parentComponent);
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
for (IdeaLoggingEvent event : events) {
if (!submit(event, additionalInfo, project, consumer)) {
return false;
}
}
reportWasSent();
return true;
}
protected void reportWasSent() {
recentlySentReport = executorService.schedule(() -> { recentlySentReport = null; }, 15, TimeUnit.MINUTES);
}
protected boolean reportWasRecentlySent() {
return recentlySentReport != null;
}
protected boolean submit(IdeaLoggingEvent event, String additionalInfo, Project project, Consumer<? super SubmittedReportInfo> consumer) {
GithubAccount account = GHAccountsUtil.getSingleOrDefaultAccount(project);
if (account == null) {
GHAccountAuthData accountData = GHAccountsUtil.requestNewAccount(project);
if (accountData == null) {
return false;
}
account = accountData.getAccount();
}
String token = GHCompatibilityUtil.getOrRequestToken(account, project);
if (token == null) return false;
GithubApiRequestExecutor githubExecutor = GithubApiRequestExecutor.Factory.getInstance().create(account.getServer(), token);
Task submitTask = new Task.Backgroundable(project, REPORT_ACTION_TEXT) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
submitToGithub(event, additionalInfo, githubExecutor, consumer, indicator);
}
};
if (ApplicationManager.getApplication().isUnitTestMode()) {
submitTask.run(new EmptyProgressIndicator());
} else {
ProgressManager.getInstance().run(submitTask);
}
return true;
}
private void submitToGithub(IdeaLoggingEvent event,
String additionalInfo,
GithubApiRequestExecutor githubExecutor,
Consumer<? super SubmittedReportInfo> consumer,
ProgressIndicator progressIndicator) {
try {
SubmittedReportInfo.SubmissionStatus status;
String issueTitle = getIssueTitle(event);
String issueDetails = getIssueDetails(event, additionalInfo);
String foundIssueId = searchExistingIssues(githubExecutor, issueTitle, progressIndicator);
if (foundIssueId == null) {
githubExecutor.execute(progressIndicator, createNewIssue(issueTitle, issueDetails));
status = SubmittedReportInfo.SubmissionStatus.NEW_ISSUE;
} else {
if (!Strings.isEmpty(additionalInfo)) {
githubExecutor.execute(progressIndicator, updateExistingIssue(foundIssueId, issueDetails));
}
status = SubmittedReportInfo.SubmissionStatus.DUPLICATE;
}
consumer.consume(new SubmittedReportInfo(status));
} catch (IOException exc) {
throw new CsvGithubSubmitException(exc);
}
}
protected GithubApiRequest<?> updateExistingIssue(String issueId, String content) throws IOException {
return GithubApiRequests.Repos.Issues.Comments.create(
GithubServerPath.DEFAULT_SERVER,
GIT_USER,
GIT_REPO,
issueId,
content
);
}
protected GithubApiRequest<?> createNewIssue(String title, String content) throws IOException {
return GithubApiRequests.Repos.Issues.create(
GithubServerPath.DEFAULT_SERVER,
GIT_USER,
GIT_REPO,
title,
content,
null,
Collections.emptyList(),
Collections.emptyList());
}
protected String searchExistingIssuesNeedle(String title) {
// Create a search needle from the title but ensure it is never null/empty to avoid GitHub 422 (Validation Failed)
String needle = title == null ? "" : title.replaceAll("\\s*(\\[.*?]|\\(.*?\\)|\\{.*?})\\s*", "");
// If the sanitized title becomes empty (e.g., only brackets present), fall back to the raw title
if (Strings.isEmptyOrSpaces(needle)) {
needle = title == null ? "" : title.trim();
}
// Apply length cap with word boundary if possible
if (needle.length() > 250) {
int endIndex = needle.substring(0, 250).lastIndexOf(" ");
if (endIndex == -1) {
endIndex = 250;
}
needle = needle.substring(0, endIndex);
}
// Final safety: if still empty, use a minimal non-empty token to satisfy GitHub API requirements
if (Strings.isEmptyOrSpaces(needle)) {
needle = "crash";
}
return needle;
}
protected String searchExistingIssues(GithubApiRequestExecutor githubExecutor, String title, ProgressIndicator progressIndicator) throws IOException {
String needle = searchExistingIssuesNeedle(title);
GithubApiRequest<GithubResponsePage<GithubSearchedIssue>> existingIssueRequest =
GithubApiRequests.Search.Issues.get(
GithubServerPath.DEFAULT_SERVER,
GITHUB_FULL_PATH,
GithubIssueState.open.name(),
null,
needle,
new GithubRequestPagination(1, 5)
);
GithubResponsePage<GithubSearchedIssue> foundIssuesPage = githubExecutor.execute(progressIndicator, existingIssueRequest);
if (foundIssuesPage != null && !foundIssuesPage.getItems().isEmpty()) {
for (GithubSearchedIssue foundIssue : foundIssuesPage.getItems()) {
if (foundIssue.getTitle().equals(title)) {
return Long.toString(foundIssue.getNumber());
}
}
}
return null;
}
protected int getIssueTitleCutIndex(String throwableTextArg) {
String throwableText = throwableTextArg.replaceAll("\r", "\n");
int index = throwableText.indexOf("\n");
if (index == -1) {
index = throwableText.indexOf(" ", FALLBACK_ISSUE_TITLE_LENGTH);
}
return index == -1 ? Math.min(throwableText.length(), FALLBACK_ISSUE_TITLE_LENGTH) : index;
}
protected String getIssueTitle(IdeaLoggingEvent event) {
String throwableText = event.getThrowableText();
int index = getIssueTitleCutIndex(throwableText);
String issueTitle = throwableText.substring(0, index).replaceAll("@[0-9a-fA-F]+", "");
return "[Automated Report] " + issueTitle;
}
protected String getIssueDetails(IdeaLoggingEvent event, String additionalInfo) {
return "Message\n---\n" +
(!Strings.isEmpty(additionalInfo) ? additionalInfo : "<no message>") +
"\n\n" +
"Stacktrace\n---\n" +
event.getThrowableText() +
"\n\n" +
"Plugin\n---\n" +
getPluginDescriptor().getPluginClassLoader() +
"\n\n" +
"IDE\n---\n" +
ApplicationInfo.getInstance().getVersionName() +
" (" +
ApplicationInfo.getInstance().getBuild() +
")";
}
@Override
public String getPrivacyNoticeText() {
return "An automated issue report contains the provided message, " +
"the stacktrace, " +
"the plugin class loader info (" +
getPluginDescriptor().getPluginClassLoader() +
"), the used IDE version name (" +
ApplicationInfo.getInstance().getVersionName() +
") and build number (" +
ApplicationInfo.getInstance().getBuild() +
"). " +
"By sending the report I agree to the information be used for resolving this and further related issues. " +
"All provided information will be publicly available at " +
String.format("https://%s/%s/%s/issues", GithubServerPath.DEFAULT_SERVER, GIT_USER, GIT_REPO);
}
}