Using Subcomponent Java API within the ScriptRunner environment in Jira


On this page, we aim to demonstrate how to use Subcomponents API on a simple example. We'll guide you on creating a custom script to fetch specific data using our API within the ScriptRunner. By the end of this guide, you will have a clear understanding of how to harness the capabilities of our API.

Prerequisites: Installing ScriptRunner

Install the following plugins in your Jira instance:

  1. Subcomponents (Data Center) - extra functional hierarchy and granularity extension for Jira Components, Versions, and Packages.

  2. ScriptRunner (Get Started) - a powerful tool that allows you to extend your Jira functionalities via Groovy scripts.

Problem statement

Given: the issue in active sprint associated with a component Infrastructure.

As a Jira user, I want to set up an automated rule that changes the assignee of the issue upon status transition to In Progress to the value derived from the Infrastructure custom component property.


Subcomponents' custom component property

The Subcomponents app has a Component properties feature, which allows the creation of custom component properties. For example, having component Infrastructure, we can create custom property defaultAssignee:

  1. Navigate to Project and choose Component properties

  2. Assign component property schema having defaultAssignee property defined:

    1. Create a new schema or update the existing one and add the new property; make sure it has the type 'user' 

  3. Add existing component(s) to the hierarchy

  4. Set component property value to the desired user

Workflow Function with ScriptRunner

  1. Navigate to ScriptRunner settings: Log into your Jira instance with administrative permissions. Click on the cog icon in the upper right corner of the screen to open the administration menu. Select Script Runner from the dropdown list.

  2. Open ScriptRunner's workflow functions: In the upper sidebar, click on Workflows. It will open the workflow functions that ScriptRunner provides.

  3. Create a new workflow function: Click the Create Workflow Function button to create a new function. Select workflowtransition, and workflow function type. Click Create. Select Custom script post-function from the list of ScriptRunner post-functions.

  4. In the text area for the "Script" field, paste the script and hit Create button.

  5. The draft workflow is saved. Publish it to test the changes.

The script

In a nutshell, the script algorithm includes the following steps:

  1. get property schema for the project

  2. find the ID of custom property to set

  3. loop over issue’s components

  4. retrieve a property value for a component property associated with an issue in your Jira instance using Subcomponents Java API

  5. if component property value exists, update the issue's assignee field

import com.onresolve.scriptrunner.runner.customisers.PluginModule import com.onresolve.scriptrunner.runner.customisers.WithPlugin import net.brokenbuild.subcomponents.api.ComponentPropertyService; import net.brokenbuild.subcomponents.api.PropertySchemaService; import net.brokenbuild.subcomponents.api.dto.propschema.PropSchemaTypeEnum; import net.brokenbuild.subcomponents.api.dto.propschema.PropSchemaDto; import net.brokenbuild.subcomponents.api.dto.propschema.PropSchemaItemDto; import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.user.util.UserManager; import com.atlassian.jira.user.ApplicationUser; @WithPlugin("net.brokenbuild.subcomponents") @PluginModule ComponentPropertyService componentPropertyService @PluginModule PropertySchemaService propertySchemaService UserManager userManager = ComponentAccessor.getUserManager() String projectKey = "SUBDT" // change to your project key String propName = "defaultAssignee" // change to property name in your schema Optional<PropSchemaDto> schema = propertySchemaService.getByProjectKey(projectKey, PropSchemaTypeEnum.COMPONENT) if(schema.isPresent()) { PropSchemaItemDto propSchemaItemDto = schema.get().getProps().find {prop -> propName.equals(prop.getName())} if(propSchemaItemDto != null) { for (component in issue.getComponents()) { Optional<String> defaultAssigneePropValue = componentPropertyService.getValue(component.getId(), propSchemaItemDto.getId()) if(defaultAssigneePropValue.isPresent()) { ApplicationUser user = userManager.getUserByName(defaultAssigneePropValue.get()); issue.setAssignee(user) break } } } else { log.warn("Property not found") } } else { log.warn("Property schema not assigned") }


Check Java Doc for more details