Crafting a service with JIRA Metadata Plugin

Tanner Wortham
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.
December 25, 2012

Objective

Craft a service to refresh a certain set of project-level data in JIRA. (Version dates to be specific.) Fun facts about this service:

  • Will be crafted with Groovy Runner (cuz it's awesome)
  • Will run once a day
  • The source data is pulled from a database
  • This source data knows which project data to refresh through a a custom project field set by the JIRA Metadata Plugin.

And it's this plugin that's currently the crux of my question. I found this code snippet that explains the proper API call for the plugin, but it assumes you're calling it from a listener or post-function and not a service:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.comments.CommentManager
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.plugin.osgi.container.OsgiContainerManager
import org.apache.log4j.Category

OsgiContainerManager osgiManager = ComponentManager.getInstance().getComponentInstanceOfType(OsgiContainerManager.class)
issue.setDescription(osgiManager.getServiceTracker("com.osoboo.jira.metadata.MetadataService").getService().getMetadataValue(issue.getProjectObject(), "accounting.code"))

What I'm really looking for is some API call with this plugin that returns an object of type Project when given a custom field name and custom field value.

Questions

  1. Does anyone know if I can use JIRA Metadata Plugin to accomplish my goal?
  2. If not, can anyone think of any creative solutions to accomplish my goal?

1 answer

1 accepted

1 vote
Answer accepted
Andreas Spall (evolu.software)
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
December 26, 2012

Hi,

1. your script-snipplet should work with Groovy Runner.

2. you should ask the ProjectManager to get the Project (Project Object)

e.g. ComponentManager.getInstance().getProjectManager().getProjectObjByKey("ProjectKey") (see: http://docs.atlassian.com/jira/latest/com/atlassian/jira/project/ProjectManager.html and http://docs.atlassian.com/jira/latest/com/atlassian/jira/ComponentManager.html)

3. you can change the medatadata value (custom project field) via api call

e.g.

osgiManager.getServiceTracker("com.osoboo.jira.metadata.MetadataService").getService().save(issue.getProjectObject(), "accounting.code", "the new value"); (see: https://studio.plugins.atlassian.com/source/browse/JMETA/trunk/jira.metadata.plugin/src/main/java/com/osoboo/jira/metadata/MetadataService.java?hb=true)
</span<>>

hope this will help you out

Andreas

Tanner Wortham
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.
December 26, 2012

Thanks, Andreas. We're getting there, but it's not quite what I'm looking for. My source data contains a whole list of projects. This data is tied to the data in JIRA by a metadata field called "Theme Key." Ideally, I would make an API call via JIRA Metadata to a method looking something like this:

Project getProject (String customFieldName, String customFieldValue)

My code would never need to modify the metadata only use it to find which project to update. In short, this is what I want my code to do:

  1. Service starts.
  2. Service grabs first row of data from source database.
  3. Service figures out which project to update by finding the proper theme key.
    1. (This step right here is where I need that getProject API call I mentioned above.)
  4. Service updates some project-level information using JIRA's API.
  5. Service grabs next row of data. Go to Step 3.
  6. Service ends when all source data has been consumed.
Tanner Wortham
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.
December 26, 2012

Also, Andreas, by the looks of it, this guy might do the trick:

Collection<JiraMetadata> getMetadata(ObjectenrichedObject);
But that's only if:
  1. That collection can be unwrapped to return me the proper project.
  2. I can figure out how to properly feed it that enriched object.
Andreas Spall (evolu.software)
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
December 26, 2012

The enriched object must be a project or version object. The collection will be all metadata values for this project/version object

Andreas Spall (evolu.software)
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
December 26, 2012

You can iterate over all projects.something like:

for (Project p : ProjectManager.getProjectObjects()) {
if ("the value ".equals(metadataService.getMetadataValue(p, "theme"))) {
gogo
}
}

Tanner Wortham
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.
December 26, 2012

In that case, getMetadata won't do the trick. Is there anything in JIRA Metadata that will, Andreas?

Tanner Wortham
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.
December 26, 2012

brilliant. i hadn't thought to tackle the problem from the other direction. That should do nicely. Thank you!! :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events