|
| 1 | +package org.jetbrains.research.groups.ml_methods.error_reporting; |
| 2 | + |
| 3 | +import com.intellij.openapi.diagnostic.SubmittedReportInfo; |
| 4 | +import com.intellij.openapi.diagnostic.SubmittedReportInfo.SubmissionStatus; |
| 5 | +import org.eclipse.egit.github.core.Issue; |
| 6 | +import org.eclipse.egit.github.core.Label; |
| 7 | +import org.eclipse.egit.github.core.RepositoryId; |
| 8 | +import org.eclipse.egit.github.core.client.GitHubClient; |
| 9 | +import org.eclipse.egit.github.core.client.PageIterator; |
| 10 | +import org.eclipse.egit.github.core.service.IssueService; |
| 11 | +import org.jetbrains.research.groups.ml_methods.error_reporting.ErrorReportInformation.InformationType; |
| 12 | +import org.jetbrains.research.groups.ml_methods.utils.ArchitectureReloadedBundle; |
| 13 | + |
| 14 | +import javax.annotation.Nullable; |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +import static org.jetbrains.research.groups.ml_methods.error_reporting.ErrorReportInformation.InformationType.*; |
| 18 | + |
| 19 | +/** |
| 20 | + * Provides functionality to create and send GitHub issues when an exception is thrown by a plugin. |
| 21 | + */ |
| 22 | +class AnonymousFeedback { |
| 23 | + private final static String TOKEN_FILE = "errorReporterToken"; |
| 24 | + private final static String GIT_REPO_USER = "ml-in-programming"; |
| 25 | + private final static String GIT_REPO = "ArchitectureReloaded"; |
| 26 | + private final static String ISSUE_LABEL_BUG = "bug"; |
| 27 | + private final static String ISSUE_LABEL_AUTO_GENERATED = "auto-generated"; |
| 28 | + private final static String GIT_ISSUE_TITLE = "[auto-generated:%s] %s"; |
| 29 | + private final static String HTML_URL_TO_CREATE_NEW_ISSUE = "https://github.com/ml-in-programming/ArchitectureReloaded/issues/new"; |
| 30 | + private final static EnumMap<InformationType, String> usersInformationToPresentableForm |
| 31 | + = new EnumMap<>(InformationType.class); |
| 32 | + |
| 33 | + static { |
| 34 | + usersInformationToPresentableForm.put(PLUGIN_NAME, "Plugin Name"); |
| 35 | + usersInformationToPresentableForm.put(PLUGIN_VERSION, "Plugin Version"); |
| 36 | + usersInformationToPresentableForm.put(OS_NAME, "OS Name"); |
| 37 | + usersInformationToPresentableForm.put(JAVA_VERSION, "Java Version"); |
| 38 | + usersInformationToPresentableForm.put(JAVA_VM_VENDOR, "Java VM Vendor"); |
| 39 | + usersInformationToPresentableForm.put(APP_NAME, "App Name"); |
| 40 | + usersInformationToPresentableForm.put(APP_FULL_NAME, "App Full Name"); |
| 41 | + usersInformationToPresentableForm.put(APP_VERSION_NAME, "App Version Name"); |
| 42 | + usersInformationToPresentableForm.put(IS_EAP, "Is EAP"); |
| 43 | + usersInformationToPresentableForm.put(APP_BUILD, "App Build"); |
| 44 | + usersInformationToPresentableForm.put(APP_VERSION, "App Version"); |
| 45 | + usersInformationToPresentableForm.put(LAST_ACTION, "Last Action"); |
| 46 | + usersInformationToPresentableForm.put(PERMANENT_INSTALLATION_ID, "User's Permanent Installation ID"); |
| 47 | + } |
| 48 | + |
| 49 | + private AnonymousFeedback() { |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Makes a connection to GitHub. Checks if there is an issue that is a duplicate and based on this, creates either a |
| 54 | + * new issue or comments on the duplicate (if the user provided additional information). |
| 55 | + * |
| 56 | + * @param errorReportInformation Information collected by {@link ErrorReportInformation} |
| 57 | + * @return The report info that is then used in {@link GitHubErrorReporter} to show the user a balloon with the link |
| 58 | + * of the created issue. |
| 59 | + */ |
| 60 | + static SubmittedReportInfo sendFeedback(ErrorReportInformation errorReportInformation) { |
| 61 | + |
| 62 | + final SubmittedReportInfo result; |
| 63 | + try { |
| 64 | + final String gitAccessToken = GitHubAccessTokenScrambler.decrypt(AnonymousFeedback.class.getResourceAsStream(TOKEN_FILE)); |
| 65 | + |
| 66 | + GitHubClient client = new GitHubClient(); |
| 67 | + client.setOAuth2Token(gitAccessToken); |
| 68 | + RepositoryId repoID = new RepositoryId(GIT_REPO_USER, GIT_REPO); |
| 69 | + IssueService issueService = new IssueService(client); |
| 70 | + |
| 71 | + Issue newGibHubIssue = createNewGibHubIssue(errorReportInformation); |
| 72 | + Issue duplicate = findFirstDuplicate(newGibHubIssue.getTitle(), issueService, repoID); |
| 73 | + boolean isNewIssue = true; |
| 74 | + if (duplicate != null) { |
| 75 | + String newErrorComment = generateGitHubIssueBody(errorReportInformation, false); |
| 76 | + issueService.createComment(repoID, duplicate.getNumber(), newErrorComment); |
| 77 | + newGibHubIssue = duplicate; |
| 78 | + isNewIssue = false; |
| 79 | + } else { |
| 80 | + newGibHubIssue = issueService.createIssue(repoID, newGibHubIssue); |
| 81 | + } |
| 82 | + |
| 83 | + final long id = newGibHubIssue.getNumber(); |
| 84 | + final String htmlUrl = newGibHubIssue.getHtmlUrl(); |
| 85 | + final String message = ArchitectureReloadedBundle.message(isNewIssue ? "git.issue.text" : "git.issue.duplicate.text", htmlUrl, id); |
| 86 | + result = new SubmittedReportInfo(htmlUrl, message, isNewIssue ? SubmissionStatus.NEW_ISSUE : SubmissionStatus.DUPLICATE); |
| 87 | + return result; |
| 88 | + } catch (Exception e) { |
| 89 | + return new SubmittedReportInfo(HTML_URL_TO_CREATE_NEW_ISSUE, |
| 90 | + ArchitectureReloadedBundle.message("report.error.connection.failure", |
| 91 | + HTML_URL_TO_CREATE_NEW_ISSUE), |
| 92 | + SubmissionStatus.FAILED); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Collects all issues on the repo and finds the first duplicate that has the same title. For this to work, the title |
| 98 | + * contains the hash of the stack trace. |
| 99 | + * |
| 100 | + * @param uniqueTitle Title of the newly created issue. Since for auto-reported issues the title is always the same, |
| 101 | + * it includes the hash of the stack trace. The title is used so that I don't have to match |
| 102 | + * something in the whole body of the issue. |
| 103 | + * @param service Issue-service of the GitHub lib that lets you access all issues |
| 104 | + * @param repo The repository that should be used |
| 105 | + * @return The duplicate if one is found or null |
| 106 | + */ |
| 107 | + @Nullable |
| 108 | + private static Issue findFirstDuplicate(String uniqueTitle, final IssueService service, RepositoryId repo) { |
| 109 | + Map<String, String> searchParameters = new HashMap<>(2); |
| 110 | + searchParameters.put(IssueService.FILTER_STATE, IssueService.STATE_OPEN); |
| 111 | + final PageIterator<Issue> pages = service.pageIssues(repo, searchParameters); |
| 112 | + for (Collection<Issue> page : pages) { |
| 113 | + for (Issue issue : page) { |
| 114 | + if (issue.getTitle().equals(uniqueTitle)) { |
| 115 | + return issue; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Turns collected information of an error into a new (offline) GitHub issue |
| 124 | + * |
| 125 | + * @param errorReportInformation A map of the information. Note that I remove items from there when they should not go in the issue |
| 126 | + * body as well. When creating the body, all remaining items are iterated. |
| 127 | + * @return The new issue |
| 128 | + */ |
| 129 | + private static Issue createNewGibHubIssue(ErrorReportInformation errorReportInformation) { |
| 130 | + String errorMessage = errorReportInformation.get(ERROR_MESSAGE); |
| 131 | + if (errorMessage == null || errorMessage.isEmpty()) { |
| 132 | + errorMessage = "Unspecified error"; |
| 133 | + } |
| 134 | + String errorHash = errorReportInformation.get(ERROR_HASH); |
| 135 | + if (errorHash == null) { |
| 136 | + errorHash = ""; |
| 137 | + } |
| 138 | + |
| 139 | + final Issue gitHubIssue = new Issue(); |
| 140 | + final String body = generateGitHubIssueBody(errorReportInformation, true); |
| 141 | + gitHubIssue.setTitle(String.format(GIT_ISSUE_TITLE, errorHash, errorMessage)); |
| 142 | + gitHubIssue.setBody(body); |
| 143 | + Label bugLabel = new Label(); |
| 144 | + bugLabel.setName(ISSUE_LABEL_BUG); |
| 145 | + Label autoGeneratedLabel = new Label(); |
| 146 | + autoGeneratedLabel.setName(ISSUE_LABEL_AUTO_GENERATED); |
| 147 | + gitHubIssue.setLabels(Arrays.asList(autoGeneratedLabel, bugLabel)); |
| 148 | + return gitHubIssue; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Creates the body of the GitHub issue. It will contain information about the system, error report information |
| 153 | + * provided by the user and the full stack trace. Everything is formatted using markdown. |
| 154 | + * |
| 155 | + * @param errorReportInformation Details provided by {@link ErrorReportInformation} |
| 156 | + * @return A markdown string representing the GitHub issue body. |
| 157 | + */ |
| 158 | + private static String generateGitHubIssueBody(ErrorReportInformation errorReportInformation, boolean addStacktrace) { |
| 159 | + String errorDescription = errorReportInformation.get(ERROR_DESCRIPTION); |
| 160 | + if (errorDescription == null) { |
| 161 | + errorDescription = ""; |
| 162 | + } |
| 163 | + String stackTrace = errorReportInformation.get(ERROR_STACKTRACE); |
| 164 | + if (stackTrace == null || stackTrace.isEmpty()) { |
| 165 | + stackTrace = "invalid stacktrace"; |
| 166 | + } |
| 167 | + |
| 168 | + StringBuilder result = new StringBuilder(); |
| 169 | + if (!errorDescription.isEmpty()) { |
| 170 | + result.append(errorDescription); |
| 171 | + result.append("\n\n----------------------\n\n"); |
| 172 | + } |
| 173 | + for (Map.Entry<InformationType, String> usersInformationEntry : usersInformationToPresentableForm.entrySet()) { |
| 174 | + result.append("- "); |
| 175 | + result.append(usersInformationEntry.getValue()); |
| 176 | + result.append(": "); |
| 177 | + result.append(errorReportInformation.get(usersInformationEntry.getKey())); |
| 178 | + result.append("\n"); |
| 179 | + } |
| 180 | + |
| 181 | + if (addStacktrace) { |
| 182 | + result.append("\n```\n"); |
| 183 | + result.append(stackTrace); |
| 184 | + result.append("\n```\n"); |
| 185 | + } |
| 186 | + |
| 187 | + return result.toString(); |
| 188 | + } |
| 189 | +} |
0 commit comments