It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to get Sprint name and ID from Issue object when writing plugin?

I'm trying to get the Sprint Name and ID from the sprint that an issue is assigned to in my plugin that i am writing. I first tested some of this functionality in the Script Runner plugin using the script console in a Groovy script, but apparently that is a little more flexible than Java.

Here's what i had working in a Groovy script:

CustomField sprint = customFieldManager.getCustomFieldObjectByName("Sprint")
sprint.getValue(issue)[0].name

So, i had to access it like a list because that's apparently what is returned. This doesn't work very well in a plugin, and my plugin won't compile. Has anyone accessed this information like this and if so, can you please share how that is accessed? Thanks.

2 answers

1 accepted

0 votes
Answer accepted

Thank you very much for the answer. Here is the complete picture:

In pom.xml:

<dependency>
            <groupId>com.atlassian.jira.plugins</groupId>
            <artifactId>jira-greenhopper-plugin</artifactId>
            <version>${greenhopper.version}</version>
            <scope>provided</scope>
        </dependency>

<properties>
        <jira.version>6.2.2</jira.version>
        <amps.version>4.2.10</amps.version>
        <greenhopper.version>6.3.11</greenhopper.version>
        <plugin.testrunner.version>1.1.2</plugin.testrunner.version>
        <!-- TestKit version 5.x for JIRA 5.x, 6.x for JIRA 6.x -->
        <testkit.version>5.2.26</testkit.version>
    </properties>

Then in my java class:

import com.atlassian.greenhopper.service.sprint.Sprint;

CustomField sprint = customFieldManager.getCustomFieldObjectByName("Sprint");
ArrayList<Sprint> sprints = (ArrayList<Sprint>)sprint.getValue(issue);

Obviously this is shortened a bit, but at least it's the whole picture in one place

The way to do all this is actually a bit tricky. Atlassian has a Manager class called SprintManager, which will provide you all kind of functions around Agile's sprints.

However, it is not an easy to find interface, so you have to dig a little to get it:

def greenhopperContext = GetApplicationContext("com.pyxis.greenhopper.jira");
def sprintManager = greenhopperContext.getBean("sprintManagerImpl");

It doesn't look like that is going to get me what I want. According to the SprintManager API, it only gives me ways of dealing with sprints directly (creating, deleting, updating, etc.). So, I would need to already know the sprint ID or name prior to looking up the sprint with the manager. I want to somehow get that information from the issue. So i start with an issue, and then get the sprint information. Do you know of a way to do this?

Here is a link to the SprintManager API documentation from JIRA Agile 6.3.9.2: https://docs.atlassian.com/greenhopper/6.3.9.2/com/atlassian/greenhopper/service/sprint/SprintManager.html

Ok, seems like I misread your question a little.Hopefully I get it right this time:

public Long getSprintId(){
	ArrayList<Sprint> list = (ArrayList<Sprint>) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sprint").getValue(getIssue());
	if(list!=null){
		Iterator<Sprint> it = list.iterator();
		Sprint current = null;
		while(it.hasNext()){
		        Sprint next = it.next();
			if(current .getId()<next.getId()){
				current=next;
			}
		}
		return current;
	}else{
		//do something else
}
}

Unfortunatly its just some Java code, but it should not be to hard to translate it into Groovy.

After you run this code you will get the last valid Sprint your issue was in.

Ok, i see that you are using Sprint as an object, i'm not sure how to use that as I haven't been able to import any of the Greenhopper(Agile) stuff. Can you explain how to got the ability to use the Sprint object type?

You should get full access to all Agile classes once you added it to the dependencies of your plugin. Just add

<dependency>
	<groupId>com.atlassian.jira.plugins</groupId>
	<artifactId>jira-greenhopper-plugin</artifactId>
	<version>${greenhopper.version}</version>
	<scope>provided</scope>
</dependency>

to your pom file and rebuild your eclipse project using the command atlas-mvn eclipse:eclipse and you should be ready to go

I converted my comment to an answer so that I can mark both as answers so that others can see the whole picture in one place.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

We want to hear from you! How do you use Slack and Atlassian together?

Hi Everyone! My name is Mina and I am on Atlassian’s Ecosystems Marketing team. Our team is focused on our technology partnerships and marketplace apps. One of Atlassian’s partners is Slack, who ...

85 views 1 5
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you