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
Accepted answer

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
Published Thursday in Agile

How Scrum works? It starts with training and education

To answer “How scrum works,” most of the teams I've worked with first addressed the question: “where to start?”  That question applies to both implementation and improvements on the Scrum framew...

209 views 3 6
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