How to auto comment Pull Requests?

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

2 votes

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:


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>() {
        Object perform() throws Throwable {
            // comment is added here
            pullRequestService.addComment(,, 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.


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.

Any solution for cloud/hosted Bitbucket version?

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.



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?


Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

631 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot