|
| 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 | +package de.halirutan.mathematica.errorreporting; |
| 23 | + |
| 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; |
| 32 | + |
| 33 | +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; |
| 39 | +import java.util.LinkedHashMap; |
| 40 | +import java.util.Map.Entry; |
| 41 | + |
| 42 | +@Loggable(Loggable.DEBUG) |
| 43 | +class AnonymousFeedback { |
| 44 | + |
| 45 | + private final static String gitUser = "Mathematica-IntelliJ-Plugin"; |
| 46 | + private final static String gitAccessToken = "4a78410ba07788116772376360a55e25263e7cec"; |
| 47 | + private final static String gitRepo = "Mathematica-IntelliJ-Plugin"; |
| 48 | + |
| 49 | + private Github myGitHub; |
| 50 | + private Repo myRepository; |
| 51 | + private Issues myIssues; |
| 52 | + |
| 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(); |
| 57 | + } |
| 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) { |
| 69 | + String errorMessage = environmentDetails.get("error.message"); |
| 70 | + if (errorMessage == null || errorMessage.isEmpty()) { |
| 71 | + errorMessage = "Unspecified error"; |
| 72 | + } |
| 73 | + |
| 74 | + final String body = generateGithubIssueBody(environmentDetails); |
| 75 | + 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; |
| 80 | + } catch (IOException e) { |
| 81 | + return "Failed to create issue on GitHub"; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 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"); |
| 112 | + if (errorDescription == null) { |
| 113 | + errorDescription = ""; |
| 114 | + } |
| 115 | + body.remove("error.description"); |
| 116 | + |
| 117 | + String stackTrace = body.get("error.stacktrace"); |
| 118 | + if (stackTrace == null || stackTrace.isEmpty()) { |
| 119 | + stackTrace = "invalid stacktrace"; |
| 120 | + } |
| 121 | + body.remove("error.stacktrace"); |
| 122 | + |
| 123 | + StringBuilder result = new StringBuilder(); |
| 124 | + |
| 125 | + if (!errorDescription.isEmpty()) { |
| 126 | + result.append(errorDescription); |
| 127 | + result.append("\n\n"); |
| 128 | + } |
| 129 | + |
| 130 | + for (Entry<String, String> entry : body.entrySet()) { |
| 131 | + result.append(entry.getKey()); |
| 132 | + result.append(": "); |
| 133 | + result.append(entry.getValue()); |
| 134 | + result.append("\n"); |
| 135 | + } |
| 136 | + |
| 137 | + result.append("\n```\n"); |
| 138 | + result.append(stackTrace); |
| 139 | + result.append("\n```\n"); |
| 140 | + |
| 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 | + } |
| 164 | + |
| 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 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments