Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Adam Barylak
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.
April 4, 2014

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Adam Barylak
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.
April 7, 2014

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

0 votes
Marten Kreienbrock
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.
April 5, 2014

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

Adam Barylak
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.
April 6, 2014

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

Marten Kreienbrock
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.
April 6, 2014

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.

Adam Barylak
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.
April 6, 2014

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?

Marten Kreienbrock
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.
April 7, 2014

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

Adam Barylak
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.
April 7, 2014

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.

TAGS
AUG Leaders

Atlassian Community Events