2121
2222package 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
3330import 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 ;
3932import java .util .LinkedHashMap ;
4033import java .util .Map .Entry ;
4134
42- @ Loggable (Loggable .DEBUG )
4335class 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}
0 commit comments