Skip to content

Commit fa6481b

Browse files
committed
Cleaned code
1 parent d10e71e commit fa6481b

5 files changed

Lines changed: 63 additions & 133 deletions

File tree

Mathematica-IntelliJ-Plugin.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
</orderEntry>
2222
<orderEntry type="library" exported="" name="com.google.guava:guava:r09" level="project" />
2323
<orderEntry type="library" name="com.jcabi:jcabi-github:0.9.4" level="project" />
24+
<orderEntry type="library" name="org.eclipse.mylyn.github:org.eclipse.egit.github.core:2.1.5" level="project" />
2425
</component>
2526
</module>

src/de/halirutan/mathematica/errorreporting/AnonymousFeedback.java

Lines changed: 54 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -21,113 +21,93 @@
2121

2222
package de.halirutan.mathematica.errorreporting;
2323

24-
import com.google.gson.Gson;
25-
import com.intellij.ide.plugins.IdeaPluginDescriptorImpl;
26-
import com.intellij.ide.plugins.PluginManager;
27-
import com.intellij.openapi.extensions.PluginId;
28-
import com.jcabi.aspects.Loggable;
29-
import com.jcabi.github.*;
30-
import com.jcabi.http.wire.RetryWire;
31-
import org.jetbrains.annotations.NotNull;
24+
import org.eclipse.egit.github.core.Issue;
25+
import org.eclipse.egit.github.core.Label;
26+
import org.eclipse.egit.github.core.RepositoryId;
27+
import org.eclipse.egit.github.core.client.GitHubClient;
28+
import org.eclipse.egit.github.core.service.IssueService;
3229

3330
import java.io.IOException;
34-
import java.io.OutputStream;
35-
import java.net.HttpURLConnection;
36-
import java.net.URL;
37-
import java.nio.charset.Charset;
38-
import java.util.HashMap;
31+
import java.util.Collections;
3932
import java.util.LinkedHashMap;
4033
import java.util.Map.Entry;
4134

42-
@Loggable(Loggable.DEBUG)
4335
class AnonymousFeedback {
4436

45-
private final static String gitUser = "Mathematica-IntelliJ-Plugin";
46-
private final static String gitAccessToken = "4a78410ba07788116772376360a55e25263e7cec";
37+
private final static String gitAccessToken = "097a2a4e4a94ff65a73508083da690d4565fd038";
38+
private final static String gitRepoUser = "halirutan";
4739
private final static String gitRepo = "Mathematica-IntelliJ-Plugin";
4840

49-
private Github myGitHub;
50-
private Repo myRepository;
51-
private Issues myIssues;
41+
private GitHubClient myGitHub;
42+
private RepositoryId myRepoID;
5243

53-
public AnonymousFeedback() {
54-
myGitHub = new RtGithub(new RtGithub(gitAccessToken).entry().through(RetryWire.class));
55-
myRepository = myGitHub.repos().get(new Coordinates.Simple(gitUser, gitRepo));
56-
myIssues = myRepository.issues();
44+
AnonymousFeedback() {
45+
myGitHub = new GitHubClient();
46+
myGitHub.setOAuth2Token(gitAccessToken);
47+
myRepoID = new RepositoryId(gitRepoUser, gitRepo);
5748
}
58-
59-
public int findDuplicate(@NotNull final String titel) {
60-
final HashMap<String, String> filter = new HashMap<>();
61-
filter.put("filter", "all");
62-
for (Issue issue : myIssues.iterate(filter)) {
63-
System.out.println(issue);
64-
}
65-
return 0;
66-
}
67-
68-
public String sendFeedback(LinkedHashMap<String, String> environmentDetails) {
49+
//
50+
// public int findDuplicate(@NotNull final String titel) {
51+
// final HashMap<String, String> filter = new HashMap<>();
52+
// filter.put("filter", "all");
53+
// for (Issue issue : myIssues.iterate(filter)) {
54+
// System.out.println(issue);
55+
// }
56+
// return 0;
57+
// }
58+
59+
String sendFeedback(LinkedHashMap<String, String> environmentDetails) throws IOException {
6960
String errorMessage = environmentDetails.get("error.message");
7061
if (errorMessage == null || errorMessage.isEmpty()) {
7162
errorMessage = "Unspecified error";
7263
}
64+
environmentDetails.remove("error.message");
7365

74-
final String body = generateGithubIssueBody(environmentDetails);
66+
final String body = generateGitHubIssueBody(environmentDetails);
7567
try {
76-
final Issue newIssue = myIssues.create(ErrorReportBundle.message("issue.title", errorMessage), body);
77-
final int issueNumber = newIssue.number();
78-
final Repo issueRepo = newIssue.repo();
79-
return "Created issue #" + issueNumber + " on " + issueRepo;
68+
final Issue newIssue = new Issue();
69+
newIssue.setTitle(ErrorReportBundle.message("issue.title", errorMessage));
70+
newIssue.setBody(body);
71+
Label label = new Label();
72+
label.setName("auto-generated");
73+
newIssue.setLabels(Collections.singletonList(label));
74+
IssueService issueService = new IssueService(myGitHub);
75+
final Issue issue = issueService.createIssue(myRepoID, newIssue);
76+
final long id = issue.getNumber();
77+
return "<a href=\"" + issue.getHtmlUrl() + "\">Created issue #" + id + "</a>";
8078
} catch (IOException e) {
81-
return "Failed to create issue on GitHub";
79+
throw new IOException("Failed to create issue on GitHub");
8280
}
8381
}
8482

85-
static String sendFeedback(
86-
HttpConnectionFactory httpConnectFactory,
87-
LinkedHashMap<String, String> environmentDetails) throws IOException {
88-
89-
sendFeedback(httpConnectFactory, convertToGitHubIssueFormat(environmentDetails));
90-
91-
return Long.toString(System.currentTimeMillis());
92-
}
93-
94-
private static byte[] convertToGitHubIssueFormat(LinkedHashMap<String, String> environmentDetails) {
95-
LinkedHashMap<String, String> result = new LinkedHashMap<>(5);
96-
97-
String errorMessage = environmentDetails.get("error.message");
98-
if (errorMessage == null || errorMessage.isEmpty()) {
99-
errorMessage = "Unspecified error";
100-
}
101-
environmentDetails.remove("error.message");
102-
103-
result.put("title", ErrorReportBundle.message("issue.title", errorMessage));
104-
result.put("label", ErrorReportBundle.message("issue.label"));
105-
result.put("body", generateGithubIssueBody(environmentDetails));
106-
107-
return ((new Gson()).toJson(result)).getBytes(Charset.forName("UTF-8"));
108-
}
109-
110-
private static String generateGithubIssueBody(LinkedHashMap<String, String> body) {
111-
String errorDescription = body.get("error.description");
83+
private static String generateGitHubIssueBody(LinkedHashMap<String, String> details) {
84+
String errorDescription = details.get("error.description");
11285
if (errorDescription == null) {
11386
errorDescription = "";
11487
}
115-
body.remove("error.description");
88+
details.remove("error.description");
11689

117-
String stackTrace = body.get("error.stacktrace");
90+
String errorHash = details.get("error.hash");
91+
if (errorHash == null) {
92+
errorHash = "";
93+
}
94+
details.remove("error.hash");
95+
96+
String stackTrace = details.get("error.stacktrace");
11897
if (stackTrace == null || stackTrace.isEmpty()) {
11998
stackTrace = "invalid stacktrace";
12099
}
121-
body.remove("error.stacktrace");
100+
details.remove("error.stacktrace");
122101

123102
StringBuilder result = new StringBuilder();
124103

125104
if (!errorDescription.isEmpty()) {
126105
result.append(errorDescription);
127-
result.append("\n\n");
106+
result.append("\n\n----------------------\n\n");
128107
}
129108

130-
for (Entry<String, String> entry : body.entrySet()) {
109+
for (Entry<String, String> entry : details.entrySet()) {
110+
result.append("- ");
131111
result.append(entry.getKey());
132112
result.append(": ");
133113
result.append(entry.getValue());
@@ -138,49 +118,8 @@ private static String generateGithubIssueBody(LinkedHashMap<String, String> body
138118
result.append(stackTrace);
139119
result.append("\n```\n");
140120

141-
return result.toString();
142-
}
143-
144-
private static void sendFeedback(HttpConnectionFactory httpConnectFactory, byte[] payload) throws IOException {
145-
String url = "https://api.github.com/repos/halirutan/Mathematica-IntelliJ-Plugin/issues?access_token=3d23871d02daa221d2270b0df18196ba821628f5";
146-
String userAgent = "Mathematica IntelliJ IDEA plugin";
147-
148-
IdeaPluginDescriptorImpl pluginDescriptor = (IdeaPluginDescriptorImpl) PluginManager.getPlugin(PluginId.getId("de.halirutan.mathematica"));
149-
if (pluginDescriptor != null) {
150-
String name = pluginDescriptor.getName();
151-
String version = pluginDescriptor.getVersion();
152-
userAgent = name + " (" + version + ")";
153-
}
154-
155-
HttpURLConnection httpURLConnection = connect(httpConnectFactory, url);
156-
httpURLConnection.setDoOutput(true);
157-
httpURLConnection.setRequestMethod("POST");
158-
httpURLConnection.setRequestProperty("User-Agent", userAgent);
159-
httpURLConnection.setRequestProperty("Content-Type", "application/json");
160-
161-
try (OutputStream outputStream = httpURLConnection.getOutputStream()) {
162-
outputStream.write(payload);
163-
}
121+
result.append("Hash Code:\n").append(errorHash);
164122

165-
int responseCode = httpURLConnection.getResponseCode();
166-
if (responseCode != 201) {
167-
throw new RuntimeException("Expected HTTP_CREATED (201), obtained " + responseCode);
168-
}
169-
}
170-
171-
private static HttpURLConnection connect(HttpConnectionFactory httpConnectFactory, String url) throws IOException {
172-
HttpURLConnection httpURLConnection = httpConnectFactory.openHttpConnection(url);
173-
httpURLConnection.setConnectTimeout(5000);
174-
httpURLConnection.setReadTimeout(5000);
175-
return httpURLConnection;
176-
}
177-
178-
public static class HttpConnectionFactory {
179-
HttpConnectionFactory() {
180-
}
181-
182-
protected HttpURLConnection openHttpConnection(String url) throws IOException {
183-
return (HttpURLConnection) ((new URL(url)).openConnection());
184-
}
123+
return result.toString();
185124
}
186125
}

src/de/halirutan/mathematica/errorreporting/AnonymousFeedbackTask.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@
2525
import com.intellij.openapi.progress.Task.Backgroundable;
2626
import com.intellij.openapi.project.Project;
2727
import com.intellij.util.Consumer;
28-
import com.intellij.util.net.HttpConfigurable;
29-
import de.halirutan.mathematica.errorreporting.AnonymousFeedback.HttpConnectionFactory;
3028
import org.jetbrains.annotations.NotNull;
3129
import org.jetbrains.annotations.Nullable;
3230

33-
import java.io.IOException;
34-
import java.net.HttpURLConnection;
3531
import java.util.LinkedHashMap;
3632

3733

@@ -69,11 +65,4 @@ public void run(@NotNull ProgressIndicator indicator) {
6965
myErrorCallback.consume(e);
7066
}
7167
}
72-
73-
private static class ProxyHttpConnectionFactory extends HttpConnectionFactory {
74-
@Override
75-
protected HttpURLConnection openHttpConnection(String url) throws IOException {
76-
return HttpConfigurable.getInstance().openHttpConnection(url);
77-
}
78-
}
7968
}

src/de/halirutan/mathematica/errorreporting/ErrorReporter.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,22 @@ private static boolean doSubmit(final IdeaLoggingEvent event,
106106

107107
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
108108

109-
Consumer<String> successCallback = token -> {
109+
Consumer<String> successCallback = successMessage -> {
110110
final SubmittedReportInfo reportInfo = new SubmittedReportInfo(
111-
null, "Issue " + token, SubmissionStatus.NEW_ISSUE);
111+
null, successMessage, SubmissionStatus.NEW_ISSUE);
112112
callback.consume(reportInfo);
113113

114-
ReportMessages.GROUP.createNotification(ReportMessages.ERROR_REPORT,
115-
"Submitted",
114+
ReportMessages.GROUP.createNotification(
115+
ReportMessages.ERROR_REPORT,
116+
successMessage,
116117
NotificationType.INFORMATION,
117118
null).setImportant(false).notify(project);
118119
};
119120

120121
Consumer<Exception> errorCallback = e -> {
121122
String message = e.getMessage();
122-
ReportMessages.GROUP.createNotification(ReportMessages.ERROR_REPORT,
123+
ReportMessages.GROUP.createNotification(
124+
ReportMessages.ERROR_REPORT,
123125
message,
124126
NotificationType.ERROR,
125127
NotificationListener.URL_OPENING_LISTENER).setImportant(false).notify(project);

src/de/halirutan/mathematica/errorreporting/IdeaITNProxy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ static LinkedHashMap<String, String> getKeyValuePairs(ErrorBean error,
6060

6161
params.put("Last Action", error.getLastAction());
6262

63-
//params.put("previous.exception", error.getPreviousException() == null ? null : Integer.toString(error.getPreviousException()));
64-
6563
params.put("error.message", error.getMessage());
6664
params.put("error.stacktrace", error.getStackTrace());
65+
params.put("error.hash", String.valueOf(error.getStackTrace().hashCode()));
6766

6867
for (Attachment attachment : error.getAttachments()) {
6968
params.put("attachment.name", attachment.getName());

0 commit comments

Comments
 (0)