How to get sprint names of a defined issue?

I need to get sprint names of a defined issue. At first, I thought that this can be done as all custom fields like this:

private static final long SPRINT_CUSTOM_FIELD_ID = 11160; // ID of Sprint CF in Jira 6.1.5;

CustomField customFieldSprint = customFieldManager.getCustomFieldObject(Long.valueOf(SPRINT_CUSTOM_FIELD_ID));

log.debug("Issue: "+ issue.getKey() +"Sprint Value: "+ getCustomFieldStringValue(issue, customFieldSprint));

Where getCustomFieldStringValue is the following:

public String getCustomFieldStringValue(Issue issue, CustomField customField) {

String customFieldStringValue = "";

Object value = issue.getCustomFieldValue(customField);

if(value == null){

customFieldStringValue = "";

}

else if(value instanceof Option) {

customFieldStringValue = ((Option) value).getValue();

}

else if(value instanceof String) {

customFieldStringValue = (String) value;

}

log.debug("Custom Field " + customField.getName() + " has the following string value: " + customFieldStringValue);

return customFieldStringValue;

}

But, I obtained an empty value.

I have noticed that the method getSprintsForIssue of com.atlassian.greenhopper.service.sprint.SprintIssueServiceImpl gets sprints for a defined issue, but I'm not be able to use it (it seems that there'no public services for this as mentioned in https://answers.atlassian.com/questions/92681/how-to-get-sprints-using-greenhopper-api). Can you please advise?

Thanks,

Rosy

3 answers

Hi Rosy,

unfortunatly you are right, the SprintIssueService is not a public service. However, depending on your version of Jira and Agile you might be able to access the SprintManager class, which might be useful to your problem. But this only works for Agile and Jira versions <6.2.

In regards to your problems with the custom field, the content of this field is an ArrayList of the class Sprint. therefore your code will probably just keep and return its inital empty string. To access the content of the field you could try some code similar to this:

private void setSprintData(){
	ArrayList&lt;Sprint&gt; list = (ArrayList&lt;Sprint&gt;) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sprint").getValue(getIssue());
	if(list!=null){
		Iterator&lt;Sprint&gt; it = list.iterator();
		String name = "";
		Long id = Long.MIN_VALUE;
		while(it.hasNext()){
			Sprint next = it.next();
			if(id&lt;next.getId()){
				id=next.getId();
				name = next.getName();
			}
		}
		sprintName = name;
		sprintId = id;
	}else{
		sprintName = "";
		sprintId = -2L;
	}
}

Hey Marten,

I have the version 6.1.5, but SprintManager doesn't work with this version as well.

I tried your code and it works perfectly! :)

Thanks a lot for your help,

Rosy

Hi Rosy,

always happy to help. but as far as I know SprintManager should work in your version. It is just very tricky to get it. Standard import via the pom file won't work, you have to do some more elaborated stuff, like posted in this question

Thanks Marten.

You are right! I only imported green hopper plugin classes in my pom.xml and defined this Class as a Component in my atlassian-plugin.xml. I wasn't aware of this tricky code.

Many Thanks,

Rosy

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

893 views 3 12
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