2121
2222package de .halirutan .mathematica .errorreporting ;
2323
24+ import com .intellij .openapi .diagnostic .SubmittedReportInfo ;
25+ import com .intellij .openapi .diagnostic .SubmittedReportInfo .SubmissionStatus ;
2426import org .eclipse .egit .github .core .Issue ;
2527import org .eclipse .egit .github .core .Label ;
2628import org .eclipse .egit .github .core .RepositoryId ;
2729import org .eclipse .egit .github .core .client .GitHubClient ;
30+ import org .eclipse .egit .github .core .client .PageIterator ;
2831import org .eclipse .egit .github .core .service .IssueService ;
2932
33+ import javax .annotation .Nullable ;
3034import java .io .IOException ;
31- import java .util .Collections ;
32- import java .util .LinkedHashMap ;
35+ import java .util .*;
3336import java .util .Map .Entry ;
3437
3538class AnonymousFeedback {
3639
3740 private final static String gitAccessToken = "097a2a4e4a94ff65a73508083da690d4565fd038" ;
38- private final static String gitRepoUser = "halirutan " ;
39- private final static String gitRepo = "Mathematica-IntelliJ -Plugin" ;
41+ private final static String gitRepoUser = "Mathematica-IntelliJ-Plugin " ;
42+ private final static String gitRepo = "Auto-generated-issues-for-the-Mathematica -Plugin" ;
4043
41- private GitHubClient myGitHub ;
42- private RepositoryId myRepoID ;
44+ private final static String issueLabel = "auto-generated" ;
4345
44- AnonymousFeedback () {
45- myGitHub = new GitHubClient ();
46- myGitHub .setOAuth2Token (gitAccessToken );
47- myRepoID = new RepositoryId (gitRepoUser , gitRepo );
46+ private AnonymousFeedback () { }
47+
48+ static SubmittedReportInfo sendFeedback (LinkedHashMap <String , String > environmentDetails ) {
49+
50+ final SubmittedReportInfo result ;
51+ try {
52+ GitHubClient client = new GitHubClient ();
53+ client .setOAuth2Token (gitAccessToken );
54+ RepositoryId repoID = new RepositoryId (gitRepoUser , gitRepo );
55+ IssueService issueService = new IssueService (client );
56+
57+ String errorDescription = environmentDetails .get ("error.description" );
58+
59+ Issue newGibHubIssue = createNewGibHubIssue (environmentDetails );
60+ Issue duplicate = findFirstDuplicate (newGibHubIssue .getTitle (), issueService , repoID );
61+ boolean isNewIssue = true ;
62+ if (duplicate != null ) {
63+ if (errorDescription != null ) {
64+ issueService .createComment (repoID , duplicate .getNumber (), errorDescription );
65+ }
66+ newGibHubIssue = duplicate ;
67+ isNewIssue = false ;
68+ } else {
69+ newGibHubIssue = issueService .createIssue (repoID , newGibHubIssue );
70+ }
71+
72+ final long id = newGibHubIssue .getNumber ();
73+ final String htmlUrl = newGibHubIssue .getHtmlUrl ();
74+ final String message = ErrorReportBundle .message (isNewIssue ? "git.issue.text" : "git.issue.duplicate.text" , htmlUrl , id );
75+ result = new SubmittedReportInfo (htmlUrl , message , isNewIssue ? SubmissionStatus .NEW_ISSUE : SubmissionStatus .DUPLICATE );
76+ return result ;
77+ } catch (IOException e ) {
78+ return new SubmittedReportInfo (null , ErrorReportBundle .message ("report.error.connection.failure" ), SubmissionStatus .FAILED );
79+ }
4880 }
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 {
60- String errorMessage = environmentDetails .get ("error.message" );
81+
82+ @ Nullable
83+ private static Issue findFirstDuplicate (String uniqueTitle , final IssueService service , RepositoryId repo ) throws IOException {
84+ Map <String , String > searchParameters = new HashMap <>(2 );
85+ searchParameters .put (IssueService .FILTER_STATE , IssueService .STATE_OPEN );
86+ final PageIterator <Issue > pages = service .pageIssues (repo , searchParameters );
87+ for (Collection <Issue > page : pages ) {
88+ for (Issue issue : page ) {
89+ if (issue .getTitle ().equals (uniqueTitle )) {
90+ return issue ;
91+ }
92+ }
93+ }
94+ return null ;
95+ }
96+
97+ private static Issue createNewGibHubIssue (LinkedHashMap <String , String > details ) {
98+ String errorMessage = details .get ("error.message" );
6199 if (errorMessage == null || errorMessage .isEmpty ()) {
62100 errorMessage = "Unspecified error" ;
63101 }
64- environmentDetails .remove ("error.message" );
102+ details .remove ("error.message" );
65103
66- final String body = generateGitHubIssueBody (environmentDetails );
67- try {
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>" ;
78- } catch (IOException e ) {
79- throw new IOException ("Failed to create issue on GitHub" );
104+ String errorHash = details .get ("error.hash" );
105+ if (errorHash == null ) {
106+ errorHash = "" ;
80107 }
108+ details .remove ("error.hash" );
109+
110+ final Issue gitHubIssue = new Issue ();
111+ final String body = generateGitHubIssueBody (details );
112+ gitHubIssue .setTitle (ErrorReportBundle .message ("git.issue.title" , errorHash , errorMessage ));
113+ gitHubIssue .setBody (body );
114+ Label label = new Label ();
115+ label .setName (issueLabel );
116+ gitHubIssue .setLabels (Collections .singletonList (label ));
117+ return gitHubIssue ;
81118 }
82119
83120 private static String generateGitHubIssueBody (LinkedHashMap <String , String > details ) {
@@ -87,11 +124,6 @@ private static String generateGitHubIssueBody(LinkedHashMap<String, String> deta
87124 }
88125 details .remove ("error.description" );
89126
90- String errorHash = details .get ("error.hash" );
91- if (errorHash == null ) {
92- errorHash = "" ;
93- }
94- details .remove ("error.hash" );
95127
96128 String stackTrace = details .get ("error.stacktrace" );
97129 if (stackTrace == null || stackTrace .isEmpty ()) {
@@ -118,8 +150,6 @@ private static String generateGitHubIssueBody(LinkedHashMap<String, String> deta
118150 result .append (stackTrace );
119151 result .append ("\n ```\n " );
120152
121- result .append ("Hash Code:\n " ).append (errorHash );
122-
123153 return result .toString ();
124154 }
125155}
0 commit comments