Skip to content

Commit da693e4

Browse files
committed
Reworked GitHub auto issue creator.
1 parent fa6481b commit da693e4

13 files changed

Lines changed: 204 additions & 745 deletions

Mathematica-IntelliJ-Plugin.iml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
</library>
2121
</orderEntry>
2222
<orderEntry type="library" exported="" name="com.google.guava:guava:r09" level="project" />
23-
<orderEntry type="library" name="com.jcabi:jcabi-github:0.9.4" level="project" />
2423
<orderEntry type="library" name="org.eclipse.mylyn.github:org.eclipse.egit.github.core:2.1.5" level="project" />
2524
</component>
2625
</module>

lib/gson-2.2.2.jar

185 KB
Binary file not shown.
199 KB
Binary file not shown.

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<extensions defaultExtensionNs="com.intellij">
8383
<internalFileTemplate name="Package"/>
8484

85-
<errorHandler implementation="de.halirutan.mathematica.errorreporting.ErrorReporter"/>
85+
<errorHandler implementation="de.halirutan.mathematica.errorreporting.GitHubErrorReporter"/>
8686
<fileTypeFactory implementation="de.halirutan.mathematica.MathematicaFileTypeFactory"/>
8787
<lang.parserDefinition language="Mathematica"
8888
implementationClass="de.halirutan.mathematica.parsing.prattparser.MathematicaParserDefinition"/>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Copyright (c) 2017 Patrick Scheibe
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
#
21+
22+
report.error.to.plugin.vendor=Report error to plugin vendor
23+
report.error.progress.dialog.text=Submitting error report...
24+
report.error.connection.failure=Could not communicate with GitHub
25+
26+
git.issue.title=[auto-generated:{0}] {1}
27+
git.issue.label=auto-generated
28+
git.issue.text=<a href="{0}">Created issue {1}</a>. Thank you for your feedback!
29+
git.issue.duplicate.text = <a href="{0}">A similar issues was already reported (#{1})</a>. Thank you for your feedback!
30+

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

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,63 +21,100 @@
2121

2222
package de.halirutan.mathematica.errorreporting;
2323

24+
import com.intellij.openapi.diagnostic.SubmittedReportInfo;
25+
import com.intellij.openapi.diagnostic.SubmittedReportInfo.SubmissionStatus;
2426
import org.eclipse.egit.github.core.Issue;
2527
import org.eclipse.egit.github.core.Label;
2628
import org.eclipse.egit.github.core.RepositoryId;
2729
import org.eclipse.egit.github.core.client.GitHubClient;
30+
import org.eclipse.egit.github.core.client.PageIterator;
2831
import org.eclipse.egit.github.core.service.IssueService;
2932

33+
import javax.annotation.Nullable;
3034
import java.io.IOException;
31-
import java.util.Collections;
32-
import java.util.LinkedHashMap;
35+
import java.util.*;
3336
import java.util.Map.Entry;
3437

3538
class 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
}

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package de.halirutan.mathematica.errorreporting;
2323

24+
import com.intellij.openapi.diagnostic.SubmittedReportInfo;
2425
import com.intellij.openapi.progress.ProgressIndicator;
2526
import com.intellij.openapi.progress.Task.Backgroundable;
2627
import com.intellij.openapi.project.Project;
@@ -37,32 +38,23 @@
3738
* As per answer from here: http://devnet.jetbrains.com/message/5526206;jsessionid=F5422B4AF1AFD05AAF032636E5455E90#5526206
3839
*/
3940
public class AnonymousFeedbackTask extends Backgroundable {
40-
private final Consumer<String> myCallback;
41-
private final Consumer<Exception> myErrorCallback;
41+
private final Consumer<SubmittedReportInfo> myCallback;
4242
private final LinkedHashMap<String, String> myParams;
4343

4444
AnonymousFeedbackTask(@Nullable Project project,
4545
@NotNull String title,
4646
boolean canBeCancelled,
4747
LinkedHashMap<String, String> params,
48-
final Consumer<String> callback,
49-
final Consumer<Exception> errorCallback) {
48+
final Consumer<SubmittedReportInfo> callback) {
5049
super(project, title, canBeCancelled);
5150

5251
myParams = params;
5352
myCallback = callback;
54-
myErrorCallback = errorCallback;
5553
}
5654

5755
@Override
5856
public void run(@NotNull ProgressIndicator indicator) {
5957
indicator.setIndeterminate(true);
60-
AnonymousFeedback feedback = new AnonymousFeedback();
61-
try {
62-
String token = feedback.sendFeedback(myParams);
63-
myCallback.consume(token);
64-
} catch (Exception e) {
65-
myErrorCallback.consume(e);
66-
}
58+
myCallback.consume(AnonymousFeedback.sendFeedback(myParams));
6759
}
6860
}

0 commit comments

Comments
 (0)