Using Subcomponent Java API within the ScriptRunner environment in Jira
Introduction
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:
Subcomponents (Data Center) - extra functional hierarchy and granularity extension for Jira Components, Versions, and Packages.
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.
Solution
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:
Navigate to Project and choose Component properties
Assign component property schema having defaultAssignee property defined:
Create a new schema or update the existing one and add the new property; make sure it has the type 'user'Â
Add existing component(s) to the hierarchy
Set component property value to the desired user
Workflow Function with ScriptRunner
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.
Open ScriptRunner's workflow functions: In the upper sidebar, click on Workflows. It will open the workflow functions that ScriptRunner provides.
Create a new workflow function: Click the Create Workflow Function button to create a new function. Select workflow, transition, and workflow function type. Click Create. Select Custom script post-function from the list of ScriptRunner post-functions.
In the text area for the "Script" field, paste the script and hit Create button.
The draft workflow is saved. Publish it to test the changes.
The script
In a nutshell, the script algorithm includes the following steps:
get property schema for the project
find the ID of custom property to set
loop over issue’s components
retrieve a property value for a component property associated with an issue in your Jira instance using Subcomponents Java API
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
Â