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);
}
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);
}
}
}
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);
}
}
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();
}
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());
}
Aggregations