static List<Comment> filterCommentsWithoutContent( final List<Comment> unfilteredComments, final String containing) { final List<Comment> filteredComments = new ArrayList<>(); for (final Comment comment : unfilteredComments) { if (!comment.getContent().trim().contains(containing.trim())) { filteredComments.add(comment); } } return filteredComments; }
/** * When creating comment, the call should use the file as it is specified by the comments * provider. Not the one specified by the {@link Violation}. The one in the {@link Violation} may * not be recognized. <br> * <br> * Here we make a guess on which file in the {@link CommentsProvider} to use. */ static Optional<ChangedFile> findChangedFile( final List<ChangedFile> files, final Violation violation) { for (final ChangedFile providerFile : files) { String violationFile = violation.getFile(); String fileFile = providerFile.getFilename(); if (violationFile.endsWith(fileFile) || fileFile.endsWith(violationFile)) { return ofNullable(providerFile); } } return empty(); } }
@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; }
@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); final CommitComment commitComment = new CommitComment(); commitComment.setBody(comment); commitComment.setPath(file.getFilename()); commitComment.setCommitId(pullRequestCommit); commitComment.setPosition(lineToComment);
@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 List<ChangedFile> getFiles() { final List<ChangedFile> changedFiles = new ArrayList<>(); for (final GitlabCommitDiff change : mergeRequest.getChanges()) { final String filename = change.getNewPath(); final List<String> specifics = new ArrayList<>(); final String patchString = change.getDiff(); specifics.add(patchString); specifics.add(change.getOldPath()); specifics.add(change.getNewPath()); final ChangedFile changedFile = new ChangedFile(filename, specifics); changedFiles.add(changedFile); } return changedFiles; }
@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 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); } } }
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) + ">*"; }
@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); }
.map((f) -> f.getFilename()) //
@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 List<Comment> getComments() { final List<Comment> comments = new ArrayList<>(); try { final List<String> specifics = new ArrayList<>(); for (final CommitComment commitComment : pullRequestService.getComments( repository, violationCommentsToGitHubApi.getPullRequestId())) { comments.add( new Comment( Long.toString(commitComment.getId()), commitComment.getBody(), TYPE_DIFF, specifics)); } for (final org.eclipse.egit.github.core.Comment comment : issueSerivce.getComments(repository, violationCommentsToGitHubApi.getPullRequestId())) { comments.add( new Comment(Long.toString(comment.getId()), comment.getBody(), TYPE_PR, specifics)); } } catch (final Exception e) { violationsLogger.log(SEVERE, e.getMessage(), e); } return comments; }
static List<Comment> filterCommentsWithContent( final List<Comment> unfilteredComments, final String containing) { final List<Comment> filteredComments = new ArrayList<>(); for (final Comment comment : unfilteredComments) { if (comment.getContent().trim().contains(containing.trim())) { filteredComments.add(comment); } } return filteredComments; }
final String startSha = mergeRequest.getStartSha(); final String headSha = mergeRequest.getHeadSha(); final String patchString = file.getSpecifics().get(0); final String oldPath = file.getSpecifics().get(1); final String newPath = file.getSpecifics().get(2); Integer oldLine = new PatchParser(patchString) //
+ violation.getRule() + " " + changedFile.get().getFilename() + " " + violation.getStartLine()
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); } }