How to get the custom project properties in jira plugin? Edited

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?

1 answer

1 accepted

1 vote
Accepted answer

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());

Hi Alexey,

 

This was what I was looking for. Thanks!

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.

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

1,100 views 4 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you