Search in sources :

Example 1 with ReviewComment

use of com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment in project startup-os by google.

the class GithubSync method getReviewCommentCorrelations.

private ImmutableList<ReviewCommentCorrelation> getReviewCommentCorrelations(List<ReviewComment> reviewerReviewComments, List<ReviewComment> githubReviewComments) {
    List<ReviewCommentCorrelation> result = new ArrayList<>();
    for (ReviewComment reviewerComment : reviewerReviewComments) {
        ReviewCommentCorrelation reviewCommentCorrelation = new ReviewCommentCorrelation();
        reviewCommentCorrelation.reviewerComment = reviewerComment;
        reviewCommentCorrelation.githubComment = getAssociatedComment(githubReviewComments, reviewerComment);
        result.add(reviewCommentCorrelation);
    }
    for (ReviewComment githubComment : githubReviewComments) {
        int existingCommentCorrelationIndex = -1;
        for (ReviewCommentCorrelation correlation : result) {
            if (correlation.reviewerComment.getId() == githubComment.getId()) {
                existingCommentCorrelationIndex = result.indexOf(correlation);
            }
        }
        if (existingCommentCorrelationIndex == -1) {
            ReviewCommentCorrelation reviewCommentCorrelation = new ReviewCommentCorrelation();
            reviewCommentCorrelation.reviewerComment = getAssociatedComment(reviewerReviewComments, githubComment);
            reviewCommentCorrelation.githubComment = githubComment;
            result.add(reviewCommentCorrelation);
        } else {
            result.get(existingCommentCorrelationIndex).githubComment = githubComment;
        }
    }
    return ImmutableList.copyOf(result);
}
Also used : ReviewComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment) ArrayList(java.util.ArrayList)

Example 2 with ReviewComment

use of com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment in project startup-os by google.

the class GithubSync method syncWithGithub.

public void syncWithGithub(long diffNumber, ImmutableMap<String, GitRepo> repoNameToGitRepos) throws IOException {
    Diff diff = reviewerClient.getDiff(diffNumber);
    ImmutableList<PullRequest> reviewerPullRequests = new DiffConverter(reviewerClient).toPullRequests(diff, repoNameToGitRepos);
    for (PullRequest reviewerPullRequest : reviewerPullRequests) {
        boolean isFirstSync = reviewerPullRequest.getNumber() == 0;
        if (isFirstSync) {
            long githubPullRequestNumber = createPullRequest(reviewerPullRequest, diffNumber);
            for (ReviewComment reviewComment : reviewerPullRequest.getReviewCommentList()) {
                createReviewCommentOnGithub(reviewerPullRequest, githubPullRequestNumber, reviewComment, diffNumber);
            }
            for (IssueComment issueComment : reviewerPullRequest.getIssueCommentList()) {
                createIssueCommentOnGithub(reviewerPullRequest, githubPullRequestNumber, issueComment, diffNumber);
            }
        } else {
            PullRequest githubPullRequest = githubReader.getPullRequest(reviewerPullRequest.getOwner(), reviewerPullRequest.getRepo(), reviewerPullRequest.getNumber());
            ImmutableList<ReviewCommentCorrelation> reviewCommentCorrelations = getReviewCommentCorrelations(reviewerPullRequest.getReviewCommentList(), githubPullRequest.getReviewCommentList());
            syncReviewComments(diffNumber, reviewerPullRequest, githubPullRequest, reviewCommentCorrelations);
            ImmutableList<IssueCommentCorrelation> issueCommentCorrelations = getIssueCommentCorrelations(reviewerPullRequest.getIssueCommentList(), githubPullRequest.getIssueCommentList());
            syncIssueComments(diffNumber, reviewerPullRequest, githubPullRequest, issueCommentCorrelations);
        }
    }
}
Also used : Diff(com.google.startupos.tools.reviewer.local_server.service.Protos.Diff) ReviewComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment) PullRequest(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.PullRequest) IssueComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.IssueComment)

Example 3 with ReviewComment

use of com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment in project startup-os by google.

the class GithubSync method syncReviewComments.

private void syncReviewComments(long diffNumber, PullRequest reviewerPullRequest, PullRequest githubPullRequest, ImmutableList<ReviewCommentCorrelation> reviewCommentCorrelations) {
    for (ReviewCommentCorrelation reviewCommentCorrelation : reviewCommentCorrelations) {
        ReviewComment githubComment = reviewCommentCorrelation.githubComment;
        ReviewComment reviewerComment = reviewCommentCorrelation.reviewerComment;
        if (reviewerComment.getCreatedAt().isEmpty() && !githubComment.getCreatedAt().isEmpty()) {
            // The comment was deleted by Reviewer
            if (isCreatedByReviewerBot(githubComment.getBody())) {
                githubWriter.deleteReviewComment(githubPullRequest.getOwner(), githubPullRequest.getRepo(), githubComment.getId());
            }
            // The comment was created by GitHub
            reviewerClient.addCodeComment(diffNumber, githubComment);
            continue;
        }
        if (githubComment.getCreatedAt().isEmpty() && !reviewerComment.getCreatedAt().isEmpty()) {
            // The comment was created by Reviewer
            if (reviewerComment.getId() == 0) {
                createReviewCommentOnGithub(reviewerPullRequest, githubPullRequest.getNumber(), reviewCommentCorrelation.reviewerComment, diffNumber);
            } else {
                // The comment was deleted by GitHub
                reviewerClient.deleteCodeComment(diffNumber, reviewerComment.getReviewerThreadId(), reviewerComment.getReviewerCommentId(), reviewerComment.getId());
            }
            continue;
        }
        // The comment exists on Reviewer and GitHub
        updateExistingReviewComments(githubPullRequest.getOwner(), githubPullRequest.getRepo(), diffNumber, reviewerComment, githubComment);
    }
}
Also used : ReviewComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment)

Example 4 with ReviewComment

use of com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment in project startup-os by google.

the class DiffConverter method getReviewCommentsByRepoName.

private ImmutableList<ReviewComment> getReviewCommentsByRepoName(List<Thread> codeThreads, String repoName, ImmutableMap<String, GitRepo> repoNameToGitRepos, long modifiedTimestamp, long diffId) {
    ImmutableList.Builder<ReviewComment> result = ImmutableList.builder();
    ImmutableList<Thread> codeThreadsByRepo = ImmutableList.copyOf(codeThreads.stream().filter(thread -> thread.getRepoId().equals(repoName)).collect(Collectors.toList()));
    for (Thread thread : codeThreadsByRepo) {
        for (Comment comment : thread.getCommentList()) {
            GitRepo gitRepo = getGitRepo(thread.getFile().getRepoId(), repoNameToGitRepos);
            int githubCommentPosition;
            String commitId;
            boolean isOutsideDiffComment = false;
            // `0` value means the comment isn't synced with GitHub
            if (thread.getGithubCommentPosition() == 0 && thread.getClosestGithubCommentPosition() == 0) {
                String filename = thread.getFile().getFilename();
                String baseBranchCommitId = gitRepo.getMostRecentCommitOfBranch("master");
                String patch;
                if (baseBranchCommitId.equals(thread.getFile().getCommitId())) {
                    // for the left file
                    commitId = gitRepo.getMostRecentCommitOfFile(filename);
                    patch = gitRepo.getPatch(baseBranchCommitId, commitId, filename);
                } else {
                    // for the right file
                    commitId = thread.getFile().getCommitId();
                    patch = gitRepo.getPatch(baseBranchCommitId, commitId, filename);
                }
                LineNumberConverter.LineNumberToGithubPositionCorrelation correlation = getGithubReviewCommentPosition(baseBranchCommitId, thread, patch);
                if (correlation.getExactGithubPosition() != 0) {
                    githubCommentPosition = correlation.getExactGithubPosition();
                } else {
                    githubCommentPosition = correlation.getClosestGithubPosition();
                    isOutsideDiffComment = true;
                }
                reviewerClient.addGithubReviewCommentPosition(diffId, thread.getId(), githubCommentPosition, comment.getId());
            } else {
                if (thread.getGithubCommentPosition() != 0) {
                    githubCommentPosition = thread.getGithubCommentPosition();
                } else {
                    githubCommentPosition = thread.getClosestGithubCommentPosition();
                    isOutsideDiffComment = true;
                }
                commitId = thread.getFile().getCommitId();
            }
            ReviewComment.Builder githubComment = ReviewComment.newBuilder();
            githubComment.setPath(thread.getFile().getFilename()).setId(comment.getGithubCommentId()).setPosition(githubCommentPosition).setCommitId(commitId).setUser(User.newBuilder().setEmail(comment.getCreatedBy()).build()).setBody(comment.getContent()).setCreatedAt(String.valueOf(Instant.ofEpochMilli(comment.getTimestamp()))).setUpdatedAt(String.valueOf(Instant.ofEpochMilli(modifiedTimestamp))).setReviewerThreadId(thread.getId()).setReviewerCommentId(comment.getId()).setIsOutsideDiffComment(isOutsideDiffComment).setReviewerLineNumber(thread.getLineNumber()).build();
            result.add(githubComment.build());
        }
    }
    return result.build();
}
Also used : Comment(com.google.startupos.tools.reviewer.local_server.service.Protos.Comment) ReviewComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment) IssueComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.IssueComment) ReviewComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment) ImmutableList(com.google.common.collect.ImmutableList) Thread(com.google.startupos.tools.reviewer.local_server.service.Protos.Thread) GitRepo(com.google.startupos.common.repo.GitRepo)

Example 5 with ReviewComment

use of com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment in project startup-os by google.

the class GithubSync method createReviewCommentOnGithub.

private void createReviewCommentOnGithub(PullRequest pullRequest, long githubPullRequestNumber, ReviewComment reviewComment, long diffNumber) {
    ReviewComment githubComment = githubWriter.createReviewComment(githubPullRequestNumber, reviewComment, pullRequest);
    reviewerClient.addGithubReviewCommentId(diffNumber, reviewComment.getReviewerThreadId(), githubComment.getId(), reviewComment.getReviewerCommentId());
    reviewerClient.addGithubReviewCommentPosition(diffNumber, reviewComment.getReviewerThreadId(), githubComment.getPosition(), reviewComment.getReviewerCommentId());
}
Also used : ReviewComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment)

Aggregations

ReviewComment (com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment)6 IssueComment (com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.IssueComment)2 ImmutableList (com.google.common.collect.ImmutableList)1 GitRepo (com.google.startupos.common.repo.GitRepo)1 PullRequest (com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.PullRequest)1 Comment (com.google.startupos.tools.reviewer.local_server.service.Protos.Comment)1 Diff (com.google.startupos.tools.reviewer.local_server.service.Protos.Diff)1 Thread (com.google.startupos.tools.reviewer.local_server.service.Protos.Thread)1 ArrayList (java.util.ArrayList)1