How to get the custom project properties in jira plugin?

Simon Huang November 9, 2017

I am making a jira plugin where the client side executes a jira api:

PUT - ${BASEURL}/rest/api/2/project/SAMPLE/properties/HT

 

On the backend I have a rest module trying to access the project properties data I created. This is the code I have so far...

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ProjectPropertyService projectPeopertyService = ComponentAccessor.getComponent(ProjectPropertyService.class);
EntityPropertyService.PropertyResult propertyValues = projectPeopertyService.getProperty(user, "SAMPLE", "HT"); //THIS SHOULD CONTAIN MY PROPERTIES
Option<EntityProperty> jsonValues = propertyValues.getEntityProperty();
jsonValues.get();

How do I parse the json data that is returned to me. I am not sure how to access it? I am trying to return jsonValues.get() but it's giving me a no serializer found exception.

Is this the correct way to extract the project properties data through component access?

2 answers

1 accepted

1 vote
Answer accepted
Alexey Matveev
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.
November 10, 2017

Hello,

That is how you can access it:

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.property.ProjectPropertyService
import com.atlassian.jira.entity.property.EntityPropertyService
import com.atlassian.jira.entity.property.EntityProperty
import com.atlassian.fugue.Option

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ProjectPropertyService projectPeopertyService = ComponentAccessor.getComponent(ProjectPropertyService.class);
EntityPropertyService.PropertyResult propertyValues = projectPeopertyService.getProperty(user, "SAMPLE", "HT"); //THIS SHOULD CONTAIN MY PROPERTIES
Option<EntityProperty> jsonValues = propertyValues.getEntityProperty();
def value1 = jsonValues.getOrNull()
log.error(value1.getValue());
Simon Huang November 10, 2017

Hi Alexey,

 

This was what I was looking for. Thanks!

Immo Hüneke November 8, 2018

Hi Alexey,

Thanks for this very informative reply. I've followed your recipe, and my unit tests work fine (where I have mocked the EntityProperty interface).

However, in the real world my plugin is not so good. I cannot retrieve the value of the project lead from a project. It could be because I am passing the wrong strings for key and/or property name, or it could be that the ProjectPropertyService is not connecting to the project in the H2 database properly.

My wired test creates a project using the testkit backdoor (I know this works, because after the test has run I can view this project in the Jira Web UI):

  • Project Key: SAM
  • Project Name: SampleProject
  • Project Lead: admin

Here's the code that always returns Optional.empty() when the wired test case invokes it with projectKey = "SAM" and propertyName = "lead":

@Override
public Optional<String> getProjectProperty(String projectKey, String propertyName) {
final JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();
final ApplicationUser user = jiraAuthenticationContext.getLoggedInUser();
if(user == null) {
return Optional.empty();
}
final ProjectPropertyService projectPropertyService = ComponentAccessor.getComponent(ProjectPropertyService.class);
final EntityPropertyService.PropertyResult propertyValues = projectPropertyService.getProperty(user, projectKey, propertyName);
if(propertyValues == null) {
return Optional.empty();
}
final Option<EntityProperty> entityProperty = propertyValues.getEntityProperty();
final EntityProperty propertyValue = entityProperty.getOrNull();
return (propertyValue == null) ? Optional.empty() : Optional.of(propertyValue.getValue());
}

I have single-stepped in the debugger to verify that the ApplicationUser, ProjectPropertyService and PropertyResult objects are valid objects and that the user is named "admin". The entityProperty, however, just contains "none()".

Any advice would be gratefully received - e.g. any commands or methods I can use to verify that the ProjectPropertyService is properly initialised and connected to the right database.

Immo Hüneke November 9, 2018

I've resolved my own problem by reading around the subject - but it was jolly difficult to find a lot of this information.

Essentially, the ProjectPropertyService does NOT support what I assumed it did, i.e. properties of a project. Instead, retrieve the ProjectManager from the ComponentAccessor, retrieve the Project from the ProjectManager by its key and then enquire of the Project - e.g. getProjectLead() returning an ApplicationUser object.

For a more fully worked example, see https://community.atlassian.com/t5/Agile-articles/Three-ways-to-update-an-issue-in-Jira-Java-Api/ba-p/736585

1 vote
Lilit Khechoyan November 9, 2020

Did you have a chance to get project properties with Jira Server recent version (8.x)?

Using getEntityProperty brings to the following error:

java.lang.NoSuchMethodError: com.atlassian.jira.entity.property.EntityPropertyService$PropertyServiceResult.getEntityProperty()Lcom/atlassian/fugue/Option;

 

Details are in:

https://community.atlassian.com/t5/Jira-Software-questions/How-to-get-jira-project-propeties-from-jira-plugin/qaq-p/1526621

Suggest an answer

Log in or Sign up to answer