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
4,365,091
Community Members
 
Community Events
168
Community Groups

How do you get User Context in Bitbucket Server Scriptrunner Script Fragments

David Puchosic Community Leader Jun 18, 2018

I've been working / struggling with Scriptrunner Script Fragments for Bitbucket Server. We have a use case to add restrictions on a set of Projects (regex). We want the users to be Project Admins, but limit the ability to change Permissions or Delete the project itself and repositories.

The Scriptrunner Hide system or plugin UI element seems to have all the Web Items and Panels I need to hide, but I'm having trouble sorting out how current user context to allow Global Admins to still see these options and Project context the Project level permissions.

Is there a simple way to add in the HasGlobalPermissionCondition into existing Web Items? I don't see a jiraHelper which Jira Scriptrunner implements to make things a little smoother.

com.atlassian.bitbucket.server.bitbucket-server-web-fragments:projects-settings-delete-button
com.atlassian.bitbucket.server.bitbucket-server-web-fragments:projects-settings-permissions-tab

 

package com.onresolve.bitbucket.groovy.test.docs.webitems

import com.atlassian.plugin.PluginParseException
import com.atlassian.plugin.web.Condition
import com.atlassian.bitbucket.repository.Repository
import com.atlassian.bitbucket.project.Project
import groovy.util.logging.Log4j
import org.slf4j.Logger
import org.slf4j.LoggerFactory

@Log4j
class OnlySomeProjectsCondition implements Condition {
    @Override
    void init(Map<String, String> params) throws PluginParseException {
        
    }

    @Override
    boolean shouldDisplay(Map<String, Object> context) {
        def repository = context.repository as Repository
        def projKey = repository.project.key.toString()
        return !(projKey.matches("AAA-.*\\d{6}.*"))
    }
}

 

1 answer

1 accepted

0 votes
Answer accepted
David Puchosic Community Leader Jul 18, 2018

I got this sorted out once I got some decent logging output...appears to work well!

package com.onresolve.bitbucket.groovy.test.docs.webitems
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.user.UserService

import com.atlassian.plugin.PluginParseException
import com.atlassian.plugin.web.Condition
import com.atlassian.bitbucket.user.ApplicationUser
import com.atlassian.bitbucket.project.Project

import groovy.util.logging.Log4j

@Log4j
class OnlySomeProjectsCondition implements Condition {
    @Override
    void init(Map<String, String> params) throws PluginParseException {
        
    }

    @Override
    boolean shouldDisplay(Map<String, Object> context) {
        def userService = ComponentLocator.getComponent(UserService.class)        
        def user = context.currentUser as ApplicationUser
        def project = context.project as Project
        def truthy = false
        // if this is NOT a special project let display
        if (!project.key.matches("AAA-.*\\d{6}.*")) {
            truthy = true
        }
        // if this IS a special project check for additional permissions
        else if ( userService.isUserInGroup(user.username, "bitbucket-admin") ) {
            truthy = true
        }
        return truthy
    }
}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events