Hi,
I have written a script handler to disable ability to delete pull requests by normal user and allow only repo/project admins to do that.
For this I have created a PullRequestDeletionRequestedEvent event handler.
Below is the code for this
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.user.UserService
import com.atlassian.bitbucket.event.pull.PullRequestDeletionRequestedEvent
def userService = ComponentLocator.getComponent(UserService.class)
def event = event as PullRequestDeletionRequestedEvent
def projects = ["list of projects this event will be triggered "]
if(event)
{
if( project.key in projects && ! user is project admin)
event.cancel("Only Admins can delete pull request ")
}
For above code I need project_key for current event and user role ( REPO_ADMIN, PROJECT_ADMIN) for current event.
I am facing problem while getting project key and user role.
Any suggestions for achieving above will help me.
Hi,
I have written below code for above problem and it works fine for one repository ( which is hard-coded in code).
The only problem I am facing now is getting repo-name OR repo-id at run-time so that this event handler can be triggered only for a particular set of repositories and not for all.
Please suggest how to achieve this.
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.user.UserService
import com.atlassian.bitbucket.event.pull.PullRequestDeletionRequestedEvent
import com.atlassian.bitbucket.permission.PermissionService
import com.atlassian.bitbucket.permission.Permission
import com.atlassian.bitbucket.project.ProjectType
import com.atlassian.bitbucket.project.ProjectService
import com.atlassian.bitbucket.repository.RepositoryService
import com.atlassian.bitbucket.repository.Repository
def userService = ComponentLocator.getComponent(UserService.class)
def permissionService = ComponentLocator.getComponent(PermissionService.class)
def projectservice = ComponentLocator.getComponent(ProjectService.class)
def repositoryservice = ComponentLocator.getComponent(RepositoryService.class)
def event = event as PullRequestDeletionRequestedEvent
def project = projectservice.getByKey("project_key") // how to avoid this hard coding
def repository = repositoryservice.getBySlug("project_key","repo slug")
//get repo id at run time
//ids = [list of repos for which this event handler should run]
//if (repo id ! in ids)
return
if(event)
{
//log.warn("permission" + permissionService.hasProjectPermission(event.getUser(),project, Permission.PROJECT_ADMIN) )
if ( !permissionService.hasRepositoryPermission(event.getUser(),repository, Permission.REPO_ADMIN) )
{
event.cancel("Only Repo Admins can delete this request ")
}
}
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.