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!
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Any solution for cloud/hosted Bitbucket version?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.