public void createComments() { if (commentsProvider.shouldCreateCommentWithAllSingleFileComments()) { createCommentWithAllSingleFileComments(); } if (commentsProvider.shouldCreateSingleFileComment()) { createSingleFileComments(); } if (!commentsProvider.shouldCreateCommentWithAllSingleFileComments() && !commentsProvider.shouldCreateSingleFileComment()) { violationsLogger.log( INFO, "Will not comment because both 'CreateCommentWithAllSingleFileComments' and 'CreateSingleFileComment' is false."); } }
public static void createComments( final ViolationsLogger violationsLogger, final List<Violation> violations, final Integer maxCommentSize, final CommentsProvider commentsProvider) { final CommentsCreator commentsCreator = new CommentsCreator(violationsLogger, commentsProvider, violations, maxCommentSize); commentsCreator.createComments(); }
private void createCommentWithAllSingleFileComments() { final List<String> accumulatedComments = getAccumulatedComments( violations, files, commentsProvider.findCommentTemplate().orElse(null), maxCommentSize); for (final String accumulatedComment : accumulatedComments) { violationsLogger.log( INFO, "Asking " + commentsProvider.getClass().getSimpleName() + " to create comment with all single file comments."); List<Comment> oldComments = commentsProvider.getComments(); oldComments = filterCommentsWithContent(oldComments, FINGERPRINT_ACC); final List<Comment> alreadyMadeComments = filterCommentsWithContent(oldComments, accumulatedComment); removeOldCommentsThatAreNotStillReported(oldComments, alreadyMadeComments); if (violations.isEmpty()) { return; } final boolean commentHasNotBeenMade = alreadyMadeComments.isEmpty(); if (commentHasNotBeenMade) { commentsProvider.createCommentWithAllSingleFileComments(accumulatedComment); } } }
private void createSingleFileComments() { List<Comment> oldComments = commentsProvider.getComments(); oldComments = filterCommentsWithContent(oldComments, FINGERPRINT); oldComments = filterCommentsWithoutContent(oldComments, FINGERPRINT_ACC); violationsLogger.log( INFO, "Asking " + commentsProvider.getClass().getSimpleName() + " to comment:"); final ViolationComments alreadyMadeComments = getViolationComments(oldComments, violations); removeOldCommentsThatAreNotStillReported(oldComments, alreadyMadeComments.getComments()); alreadyMadeComments.getViolations().contains(violation); if (violationCommentExistsSinceBefore) { continue; final Optional<ChangedFile> changedFile = findChangedFile(files, violation); if (changedFile.isPresent()) { final String commentTemplate = commentsProvider.findCommentTemplate().orElse(null); final String singleFileCommentContent = createSingleFileCommentContent(changedFile.get(), violation, commentTemplate); violationsLogger.log( INFO, violation.getReporter() + violation.getRule() + " " + changedFile.get().getFilename() + " " + violation.getStartLine() + " "
@Override public void createSingleFileComment( final ChangedFile file, final Integer line, final String comment) { final String patchString = file.getSpecifics().get(0); Optional<Integer> lineToCommentOpt = new PatchParser(patchString).findLineInDiff(line); final Integer lineToComment = lineToCommentOpt.orElse(1); try { final CommitComment commitComment = new CommitComment(); commitComment.setBody(comment); commitComment.setPath(file.getFilename()); commitComment.setCommitId(pullRequestCommit); commitComment.setPosition(lineToComment); repository, violationCommentsToGitHubApi.getPullRequestId(), commitComment); } catch (final IOException e) { violationsLogger.log( SEVERE, "File: \""
@Override public boolean shouldComment(final ChangedFile changedFile, final Integer line) { final String patchString = changedFile.getSpecifics().get(0); final boolean lineChanged = new PatchParser(patchString).isLineInDiff(line); final boolean commentOnlyChangedContent = violationCommentsToGitHubApi.getCommentOnlyChangedContent(); if (commentOnlyChangedContent && !lineChanged) { return false; } return true; }
.map((f) -> f.getFilename()) // violationsLogger.log(INFO, "Files changed:\n " + changedFiles); violationsLogger.log(INFO, "Files with violations:\n " + violationFiles); Set<String> notIncludedNotChanged = new TreeSet<>(); for (final Violation violation : mixedViolations) { final Optional<ChangedFile> file = findChangedFile(files, violation); String violationFile = violation.getFile() + " " + violation.getStartLine(); if (file.isPresent()) { final boolean shouldComment = commentsProvider.shouldComment(file.get(), violation.getStartLine()); if (shouldComment) { isChanged.add(violation); violationsLogger.log( INFO, "Will include violations on:\n " + included.stream().collect(joining("\n "))); violationsLogger.log( INFO, "Will not include violations on changed files because violation reported on untouched lines:\n " violationsLogger.log( INFO, "Will not include violations on unchanged files:\n "
@Override public void removeComments(final List<Comment> comments) { for (final Comment comment : comments) { try { final Long commentId = Long.valueOf(comment.getIdentifier()); if (comment.getType().equals(TYPE_DIFF)) { pullRequestService.deleteComment(repository, commentId); } else { issueSerivce.deleteComment(repository, commentId); } } catch (final Throwable e) { violationsLogger.log(SEVERE, e.getMessage(), e); } } }
@Override public void removeComments(final List<Comment> comments) { for (final Comment comment : comments) { try { final GitlabNote noteToDelete = new GitlabNote(); noteToDelete.setId(Integer.parseInt(comment.getIdentifier())); gitlabApi.deleteNote(mergeRequest, noteToDelete); } catch (final Throwable e) { violationsLogger.log( INFO, "Exception thrown when delete note " + comment.getIdentifier() + ". This is probably because of " + "https://github.com/timols/java-gitlab-api/issues/321"); // violationsLogger.log(SEVERE, "Could not delete note " + comment, e); } } }
@Override public List<Comment> getComments() { final List<Comment> found = new ArrayList<>(); try { final List<GitlabNote> notes = gitlabApi.getAllNotes(mergeRequest); for (final GitlabNote note : notes) { final String identifier = note.getId() + ""; final String content = note.getBody(); final String type = "PR"; final List<String> specifics = new ArrayList<>(); final Comment comment = new Comment(identifier, content, type, specifics); found.add(comment); } } catch (final Throwable e) { violationsLogger.log(SEVERE, "Could not get comments", e); } return found; }
@Override public List<ChangedFile> getFiles() { final List<ChangedFile> changedFiles = new ArrayList<>(); try { final List<CommitFile> files = pullRequestService.getFiles(repository, violationCommentsToGitHubApi.getPullRequestId()); for (final CommitFile commitFile : files) { final List<String> list = new ArrayList<>(); list.add(commitFile.getPatch()); changedFiles.add(new ChangedFile(commitFile.getFilename(), list)); } } catch (final IOException e) { violationsLogger.log(SEVERE, e.getMessage(), e); } return changedFiles; }
@Override public void createCommentWithAllSingleFileComments(final String comment) { markMergeRequestAsWIP(); try { gitlabApi.createNote(mergeRequest, comment); } catch (final Throwable e) { violationsLogger.log(SEVERE, "Could create comment " + comment, e); } }
public void toPullRequest() throws Exception { populateFromEnvironmentVariables(); checkState(); final CommentsProvider commentsProvider = new GitHubCommentsProvider(violationsLogger, this); createComments(violationsLogger, violations, MAX_VALUE, commentsProvider); }
static ViolationComments getViolationComments( final List<Comment> comments, final List<Violation> violations) { final List<Violation> madeViolations = new ArrayList<>(); final List<Comment> madeComments = new ArrayList<>(); for (final Violation violation : violations) { for (final Comment candidate : comments) { final boolean containsIdentifier = candidate.getContent().contains(identifier(violation) + ""); if (containsIdentifier) { madeViolations.add(violation); madeComments.add(candidate); } } } return new ViolationComments(madeComments, madeViolations); } }
private void removeOldCommentsThatAreNotStillReported( final List<Comment> oldComments, final List<Comment> comments) { if (!commentsProvider.shouldKeepOldComments()) { final List<Comment> existingWithoutViolation = new ArrayList<>(); existingWithoutViolation.addAll(oldComments); existingWithoutViolation.removeAll(comments); commentsProvider.removeComments(existingWithoutViolation); } }
CommentsCreator( ViolationsLogger violationsLogger, final CommentsProvider commentsProvider, final List<Violation> violations, final Integer maxCommentSize) { checkNotNull(violations, "violations"); checkNotNull(commentsProvider, "commentsProvider"); this.violationsLogger = checkNotNull(violationsLogger, "violationsLogger"); this.commentsProvider = commentsProvider; files = commentsProvider.getFiles(); this.violations = filterChanged(violations); this.maxCommentSize = maxCommentSize; }
static List<String> getAccumulatedComments( final List<Violation> violations, final List<ChangedFile> files, final String commentTemplate, final Integer maxCommentSize) { final List<String> partitions = new ArrayList<>(); StringBuilder sb = new StringBuilder(); sb.append("Found " + violations.size() + " violations:\n\n"); for (final Violation violation : violations) { final Optional<ChangedFile> changedFile = findChangedFile(files, violation); final String singleFileCommentContent = createSingleFileCommentContent(changedFile.get(), violation, commentTemplate); if (sb.length() + singleFileCommentContent.length() >= maxCommentSize) { sb.append(" *" + FINGERPRINT_ACC + "*"); partitions.add(sb.toString()); sb = new StringBuilder(); } sb.append(singleFileCommentContent + "\n"); } sb.append(" *" + FINGERPRINT_ACC + "*"); partitions.add(sb.toString()); return partitions; }
@Override public boolean shouldComment(final ChangedFile changedFile, final Integer line) { final String patchString = changedFile.getSpecifics().get(0); if (!violationCommentsToGitLabApi.getCommentOnlyChangedContent()) return true; return new PatchParser(patchString) // .isLineInDiff(line); }
@Override public void createCommentWithAllSingleFileComments(final String comment) { try { issueSerivce.createComment( repository, violationCommentsToGitHubApi.getPullRequestId(), comment); } catch (final IOException e) { violationsLogger.log(SEVERE, e.getMessage(), e); } }
public void toPullRequest() throws Exception { if (Utils.isNullOrEmpty(commentTemplate)) { commentTemplate = getDefaultTemplate(); } final CommentsProvider commentsProvider = new GitLabCommentsProvider(violationsLogger, this); createComments(violationsLogger, violations, MAX_VALUE, commentsProvider); }