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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Bitbucket Server 7.14 OnPullRequestOpen PullRequestOpenedEvent actions: add reviewers, watchers, com

I've seen others ask similar questions about how to do some of these actions.

The code below triggers when a pull request is opened and will:

  • Check if larry has read access to the repository then adds him as a watcher.
  • Add a comment to the pull request stating larry was added as a watcher.
  • Check if curly has read access to the repository then adds him as a reviewer.
  • Add a comment to the pull request stating curly was added as a reviewer.

If you try to add a user that does not exist or does not have permission, it will throw an exception. In my plugin, I collect users that do not have permission and add a comment listing them so I can add them later if needed.

In my plugin, I need to know which files changed. GetChangedFiles does that but an additional class is needed.

Change the package to your package.

Event listener (

package ...;

import com.atlassian.bitbucket.comment.Comment;
import com.atlassian.bitbucket.comment.CommentService;
import com.atlassian.bitbucket.event.pull.PullRequestOpenedEvent;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermissionService;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.pull.PullRequestChangesRequest;
import com.atlassian.bitbucket.pull.PullRequestService;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.user.UserService;
import com.atlassian.bitbucket.watcher.WatchRequest;
import com.atlassian.bitbucket.watcher.WatcherService;
import com.atlassian.event.api.EventListener;
import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;

public class TestListener {

private final PullRequestService pullRequestService;

private final WatcherService watcherService;

private final CommentService commentService;

private final PermissionService permissionService;

private final UserService userService;

private PullRequest pullRequest;
private Repository repository;

public TestListener(final PullRequestService pullRequestService,
final WatcherService watcherService,
final CommentService commentService,
final PermissionService permissionService,
final UserService userService)
this.pullRequestService = pullRequestService;
this.watcherService = watcherService;
this.commentService = commentService;
this.permissionService = permissionService;
this.userService = userService;

public void OnPullRequestOpen(PullRequestOpenedEvent pullRequestOpenedEvent)
this.pullRequest = pullRequestOpenedEvent.getPullRequest();
this.repository = pullRequest.getToRef().getRepository();



ArrayList<String> changedPaths = this.GetChangedFiles();

private void AddReviewer(String username)
if (this.CheckUserPermission(username))
pullRequestService.addReviewer(repository.getId(), pullRequest.getId(), username);

this.AddComment("Added reviewer: " + username);

private void AddWatcher(String username)
if (this.CheckUserPermission(username))
WatchRequest.Builder watchRequestBuilder = new WatchRequest.Builder(this.pullRequest);
WatchRequest watchRequest =;;

this.AddComment("Added watcher: " + username);

private void AddComment(String comment)
AddCommentRequest addCommentRequest = new AddCommentRequest.Builder(pullRequest, comment).build();
Comment newComment = commentService.addComment(addCommentRequest);

private boolean CheckUserPermission(String username)
ApplicationUser testUser = this.userService.getUserByName(username);
if (testUser == null || ! this.permissionService.hasRepositoryPermission(testUser, this.repository, Permission.REPO_READ))
return false;

return true;

private ArrayList<String> GetChangedFiles()
ArrayList<String> changedPaths = new ArrayList<String>();
pullRequestService.streamChanges(new PullRequestChangesRequest.Builder(pullRequest).build(), new PullRequestChangesCallback(changedPaths));

return changedPaths;


PullRequestChangesCallback needed to get a list of changed files (

package ...;

import com.atlassian.bitbucket.content.AbstractChangeCallback;
import com.atlassian.bitbucket.content.Change;
import com.atlassian.bitbucket.content.ChangeContext;
import com.atlassian.bitbucket.content.ChangeSummary;

import javax.annotation.Nonnull;
import java.util.ArrayList;

public class PullRequestChangesCallback extends AbstractChangeCallback
private ArrayList<String> changedPaths;

public PullRequestChangesCallback(ArrayList<String> changedPaths)

this.changedPaths = changedPaths;

public boolean onChange(@Nonnull Change change) throws IOException

return true;

public void onEnd(@Nonnull ChangeSummary summary) throws IOException

public void onStart(@Nonnull ChangeContext context) throws IOException


After creating the pull request, it takes a few seconds for the reviewers to appear in the top right and you need to refresh the page for the comments to show.

I hope this helps some people.


1 comment

Ulrich Kuhnhardt _IzymesCo_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
Aug 03, 2021

Thanks for sharing, Ryan!


Log in or Sign up to comment
AUG Leaders

Atlassian Community Events