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:

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 

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.

Thanks,
Adam

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jun 12, 2018 in Bitbucket

Do you use any Atlassian products for your personal projects?

After spinning my wheels trying to get organized enough to write a book for National Novel Writing Month (NaNoWriMo) I took my affinity for Atlassian products from my work life and decided to tr...

24,416 views 26 12
Join discussion

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