0

I am adding merge hook through bitbucket server plugin API to prevent merging of pull request if there is some specific comment is missing on PR

I am successfully able to apply this hook on PR, but DON'T see way to get list of comments on PR here

Here is my code snippet:

public class CodeReviewMergeCheck implements RepositoryMergeCheck {
    @Nonnull
    @Override
    public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext preRepositoryHookContext,
                                          @Nonnull PullRequestMergeHookRequest pullRequestMergeHookRequest) {
        pullRequestMergeHookRequest.getPullRequest().getTitle();
        **// I NEED LIST OF COMMENTS FROM PR HERE**
        return RepositoryHookResult.rejected("Code review pending", "Code review pending");
    }
}

How to get list of comments from PR here

Rakesh Bhalani
  • 668
  • 4
  • 10

1 Answers1

0

I don't have the full context however I don't agree with the implementation you are trying to achieve. You are searching for all comments in a PR and looking for a specific text. This can go wrong in many ways like,

  1. What if there is a misspelling. You will allow the merge when it should not.
  2. What about comment threads? If that text can be part of a thread.
  3. A classic problem of searching a pattern when that pattern could be part of a bigger sentence. For example, "Code" vs "Coder"

There are better merge checks that you can use. For example,

  1. Minimum number of reviewers
  2. Incomplete task check - Create a task and if any of the tasks are not resolved, the merge is not allowed.

However, if you are still want to go through this, you can inject CommentService in your class and have something like following,

Page<Comment> comments = commentService.search(
                        new CommentSearchRequest.Builder(
                          pullRequestMergeHookRequest.getPullRequest()).build(), 
                        new PageRequestImpl(0, 1));

I strongly discourage going in this direction. It has the potential of making merge activity for user very sluggish.

GauravJ
  • 2,162
  • 1
  • 22
  • 28