How to store custom project properties in jira using scriptrunner

Mazin April 19, 2022

I'm trying to store custom project properties in Jira server via scriptrunner, but had no success. I found this link that shows how to retrieve a project's properties https://community.atlassian.com/t5/Marketplace-Apps-Integrations/How-to-retrieve-custom-project-properties-in-jira-workflow/qaq-p/1368086

However, I couldn't find any post to store. Any help will be appreciated.

2 answers

1 accepted

0 votes
Answer accepted
Mazin May 1, 2022

Found a solution after tinkering and going through the java docs. Here is a simple solution that got it working. Needs proper error handling and whatnot.

NOTE: the value must be some sort of a map type because it needs to be converted into a JSON format, or else it will not be a valid format after trying to troubleshoot the problem.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.entity.property.EntityProperty
import com.atlassian.jira.entity.property.EntityPropertyOptions
import com.atlassian.jira.entity.property.EntityPropertyService
import com.atlassian.jira.bc.project.property.ProjectPropertyService
import io.atlassian.fugue.Option
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper


class ProjectEntityProperty {
def projectManager
def issueManager
String projectKey
def project
def reporter
ProjectPropertyService pps

ProjectEntityProperty(String projectKey, String userKey) {
this.projectKey = projectKey
projectManager = ComponentAccessor.projectManager
issueManager = ComponentAccessor.issueManager
project = projectManager.getProjectByCurrentKey(this.projectKey)
reporter = ComponentAccessor.userManager.getUserByKey(userKey)
pps = ComponentAccessor.getComponent(ProjectPropertyService.class)
}

def setProperty(String key, Map value) {
def json = new JsonBuilder(value)
def entityPropertyInput = new EntityPropertyService.EntityPropertyInput(json.toString(), key, project.id, project.key)


EntityPropertyOptions entityPropertyOptions = new EntityPropertyOptions(true)
def validatePropertyInput = pps.validatePropertyInput(entityPropertyInput)
def setPropertyValidationResult = pps.validateSetProperty(reporter, project.id, entityPropertyInput, entityPropertyOptions)

if(!setPropertyValidationResult.valid) {
// TODO: throw an exception
// and show something like
// log.warn(setPropertyValidationResult.getProperties())
}

EntityPropertyService.PropertyResult propertyResult = pps.setProperty(reporter, setPropertyValidationResult)
}

def getProperty(String key) {
EntityPropertyService.PropertyResult propertyResult = pps.getProperty(reporter, project.key, key)
Option<EntityProperty> jsonValues = propertyResult.getEntityProperty()
def entityProperty = jsonValues.getOrNull()
JsonSlurper parser = new JsonSlurper()
return parser.parseText(entityProperty.value)
}
}

def ep = new ProjectEntityProperty('SOME_PROJECT_KEY', 'SOME_JIRA_USER_KEY')
String key = 'com.something.example'
ep.setProperty(key, [myData: 'My data'])
return ep.getProperty(key)

 Another solution is to use Active Objects and the solution can be found here https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Active-Objects-in-a-ScriptRunner-plugin/qaq-p/624518

1 vote
Jorden Van Bogaert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 19, 2022

Hi @Mazin 

I'm not sure how to store them via code, however, we use Enhanced Project Properties on our instance and it's working perfectly. And the best thing, it's free :D

We read the properties via code, so that works fine too. And storing goes via the UI which is much more user friendly for non-scripting admins.

It also comes with some additional features such as JQL search.

Hope this helps
Kind regards
Jorden

Mazin April 19, 2022

Hi @Jorden Van Bogaert Thanks for your reply. Just learned I can't use any plugins, sadly.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events