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; }
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 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); } } }
static String createSingleFileCommentContent( final ChangedFile changedFile, final Violation violation, final String commentTemplate) { final MustacheFactory mf = new DefaultMustacheFactory(); Reader templateReader = null; final Optional<String> commentTemplateOpt = ofNullable(commentTemplate); if (commentTemplateOpt.isPresent() && !commentTemplateOpt.get().isEmpty()) { templateReader = new StringReader(commentTemplateOpt.get()); } else { templateReader = mf.getReader(DEFAULT_VIOLATION_TEMPLATE_MUSTACH); } final Mustache mustache = mf.compile(templateReader, "Violation Template"); final Writer writer = new StringWriter(); final Map<String, Object> context = new HashMap<>(); context.put("violation", new ViolationData(violation)); context.put("changedFile", changedFile); final List<Object> scopes = new ArrayList<>(); scopes.add(context); mustache.execute(writer, scopes); return writer.toString() + "\n\n*" + FINGERPRINT + "* *<" + identifier(violation) + ">*"; }
final String commentTemplate = commentsProvider.findCommentTemplate().orElse(null); final String singleFileCommentContent = createSingleFileCommentContent(changedFile.get(), violation, commentTemplate); violationsLogger.log( INFO,