In Rally we have the following story structure:
Parent Story 1
|__ Sub Story 1
| |__ Child Story 1
| |__ Child Story
The easiest solution I have found is to do the following:
var epicLevelStories = {
key: 'epics',
type: 'hierarchicalrequirement',
fetch: 'FormattedID,Name,ObjectID,Release'
query: epicQuery,
order: 'FormattedID'
};
var epicLevel2Stories = {
key: 'epiclevel2',
placeholder: '${epics.children?fetch=Name,FormattedID,Parent,Release}'
};
var epicLevel3Stories = {
key: 'epiclevel3',
placeholder: '${epiclevel2.children?fetch=Name,FormattedID,Parent,Release}'
};
var queryArray = [epicLevelStories, epicLevel2Stories, epicLevel3Stories];
rallyDataSource.findAll(queryArray, doStuffWithResults);
Once you get a result set (epiclevel#) that has no entries you know you have reached the bottom of the tree.
I'm guessing if epiclevel3 still has stories then you could build a new query array for the next 3 levels and recursively call the same "doStuffWithResults" method. Just a thought. I haven't tested that.