How to get sprint names of a defined issue?

Rosy Salameh July 24, 2014

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

0 votes
Rosy Salameh July 29, 2014

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

0 votes
Rosy Salameh July 29, 2014

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

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.
July 29, 2014

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

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.
July 29, 2014

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;
	}
}

Suggest an answer

Log in or Sign up to answer