Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to auto comment Pull Requests?

Ryan Currah February 9, 2017

I would like to auto comment on new Pull Requests with the rules for code review. Is there built in functionality for this or is there a plugin? Any insight would be greatly appreciated thanks!

1 answer

1 accepted

3 votes
Answer accepted
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 9, 2017

Hi Ryan,

You can do this quite easily with the ScriptRunner for Bitbucket Server plugin. Once installed you go to Admin -> Script Event Handlers -> Add tasks to new pull requests and enter the rules you would like to add.

These could be the rules for code review that the user needs to tick to indicate they are following those rules. These would be added every time the pull request is created. You can select in bulk what repositories/projects to apply this to. The tasks are shown in the image below with a comment:

add-tasks-result.png

Alternatively if you don't want to add them as tasks you can still add your own comment by going to Admin -> Script Event Handlers -> Custom event handler and add the following code and for the event use PullRequestOpenedEvent:

import com.atlassian.bitbucket.event.pull.PullRequestOpenedEvent
import com.atlassian.bitbucket.pull.PullRequestService
import com.atlassian.bitbucket.user.SecurityService
import com.atlassian.bitbucket.user.UserService
import com.atlassian.bitbucket.util.Operation
import com.atlassian.sal.api.component.ComponentLocator

def securityService = ComponentLocator.getComponent(SecurityService)
def pullRequestService = ComponentLocator.getComponent(PullRequestService)
def userService = ComponentLocator.getComponent(UserService)

def event = event as PullRequestOpenedEvent
def pullRequest = event.getPullRequest()
def repository = pullRequest.toRef.repository

// repositories we want to add the comment for
def allowedRepos = ["rep_1", "rep_2"]

if (repository.slug in allowedRepos) {

    // user you want to add the comment as
    def username = "rulesuser"
    def user = userService.getUserBySlug(username)

    // the rules you want to add as a comment
    def codeReviewRules = "Code review must follow these rules:\n1. Tabs and no spaces\n2. Must include tests."

    securityService.impersonating(user, "auto adding comment").call(new Operation<Object, RuntimeException>() {
        @Override
        Object perform() throws Throwable {
            // comment is added here
            pullRequestService.addComment(repository.id, pullRequest.id, codeReviewRules)
        }
    })
}

I've commented the various lines that you need to update for this to work for you. This will give you full flexibility in the comment you want to add to a pull request.

Our full documentation for all of what I have mentioned can be found here.

Let us know how you get on with that or if you need further help.

Thanks,
Adam 

Ryan Currah February 10, 2017

That's interesting thank you. I took a look at the plugin, it cost a bit of money. If I could find more than one use for it I might be able to make a case to buy it but for now probably not.

Alex Macris July 26, 2017

Any solution for cloud/hosted Bitbucket version?

adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2017

Hi Alex,

Unfortunately we don't offer a Cloud version of ScriptRunner for Bitbucket at the moment. However this is something we will be looking at in the future.

Thanks,
Adam

Former user November 16, 2017

Hi.

I'm trying to run this script on latest BitBucket Server instance. It seems that the method "addComment" has been removed from the API. Are there any ideas in terms of another solution?

Thanks,
Matthias

Infantino Benito December 19, 2023

Any solution for Bitbucket 7.x? This no longer works

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events