Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Answer accepted

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 Sign up to answer
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

4,379 views 12 15
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you