private async Task<List<ChildTicket>> GetSubtickets(List<ChildTicket> stories)
        {
            var ticketList = new List<ChildTicket>();
            if (Jira != null)
            {

                //Set up enumerable of tasks. Each returns a list of issues. 
                IEnumerable<Task<IEnumerable<Issue>>> getStoriesQuery = from story in stories
                                                                        select Jira.GetIssuesFromJqlAsync(BuildSubTicketJql(story), 100, 0, System.Threading.CancellationToken.None);

                //ToList starts asynchronous calls for all tasks in enumerable. 
                var getSubTasksTasks = getStoriesQuery.ToList();

                //Wait for all to finish. Process each as they finish. 
                while (getSubTasksTasks.Count > 0)
                {
                    //Get Completed task and remove from list. 
                    Task<IEnumerable<Issue>> finishedTask = await Task.WhenAny(getSubTasksTasks);
                    getSubTasksTasks.Remove(finishedTask);

                    try
                    {
                        var subtickets = await finishedTask;
                        
                        var parentKey = subtickets.Select(x => x.ParentIssueKey).FirstOrDefault();
                        var storyToAddTo = stories.Where(x => x.Key == parentKey).FirstOrDefault();
                        if (storyToAddTo != null)
                            foreach (var subticket in subtickets)
                            {
                                var subticketChild = new ChildTicket(subticket, true);//JQL Limits tickets to only those with work logs. 
                                storyToAddTo.SubTickets.Add(subticketChild);
                                storyToAddTo.WorkLogs.AddRange(subticketChild.WorkLogs);
                            }

                    }
                    catch (Exception e)
                    {
                        FileWriter.Log("[ERROR] ---- " + e.Message);
                    }
                }
            }
            return ticketList;
        }
 private string BuildSubTicketJql(ChildTicket childTicket)
 {
     return SubtaskJQLQueryBase + childTicket.Key;
 }
        private void GetAndApplyUpdates()
        {
            var lastUpdateTime = FileWriter.GetLastUpdateTime();
            if (lastUpdateTime == null)
                lastUpdateTime = "2016-01-01 00:00:00";

            if(Jira != null)
            {
                try
                {
                    
                    var jqlQuery = string.Format(UpdatedTicketsQuery, lastUpdateTime, lastUpdateTime.Substring(0, 10));
                    var updatedIssues = Jira.GetIssuesFromJql(jqlQuery).ToList();
                    FileWriter.Log("Got " + updatedIssues.Count + " tickets updated since " + lastUpdateTime);
                    //Update Epics within FinalBuilds.
                    foreach (var issue in updatedIssues.Where(x => x.Type.Name == "Epic"))
                    {
                        var newEpic = new ParentTicket(issue);
                        bool update = false;
                        for (int i = 0; i < FinalBuilds.Count; i++)
                        {
                            if(FinalBuilds[i].ParentTicket.Key == newEpic.Key)
                            {
                                newEpic.Stories = FinalBuilds[i].ParentTicket.Stories;
                                FinalBuilds[i].ParentTicket = newEpic;
                                update = true;
                            }
                        }

                        if (!update) //Insert new CBI
                        {
                            FinalBuilds.Add(new ConnectorBuildItem(newEpic));
                        }
                    }

                    //Update Stories
                    foreach (var issue in updatedIssues.Where(x => x.Type.Name == "Story"))
                    {
                        var newStory = new ChildTicket(issue, true);
                        bool update = false;

                        for (int i = 0; i < FinalBuilds.Count; i ++)
                            if(FinalBuilds[i].ParentTicket.Key == newStory.EpicLink)
                            {
                                for( int j = 0; j < FinalBuilds[i].ParentTicket.Stories.Count; j++)
                                    if(FinalBuilds[i].ParentTicket.Stories[j].Key == newStory.Key)
                                    {
                                        FinalBuilds[i].ParentTicket.Stories[j] = newStory;
                                        update = true;
                                    }

                                if(!update)
                                    FinalBuilds[i].ParentTicket.Stories.Add(newStory);
                            }
                    }

                    //Update story sub-tickets
                    foreach (var issue in updatedIssues.Where(x => x.Type.IsSubTask))
                    {
                        var child = new ChildTicket(issue, true);
                        var parent = AllChildren.Where(x => x.Key == issue.ParentIssueKey).FirstOrDefault();
                        bool update = false;
                        for(int i = 0; i < FinalBuilds.Count; i++)
                            if(FinalBuilds[i].ParentTicket.Key == parent.EpicLink)
                                for(int j = 0; j < FinalBuilds[i].ParentTicket.Stories.Count; j++)
                                    if(FinalBuilds[i].ParentTicket.Stories[j].Key == parent.Key)
                                    {
                                        for (int k = 0; k < FinalBuilds[i].ParentTicket.Stories[j].SubTickets.Count; k++)
                                            if(FinalBuilds[i].ParentTicket.Stories[j].SubTickets[k].Key == child.Key)
                                            {
                                                FinalBuilds[i].ParentTicket.Stories[j].SubTickets[k] = child;
                                                update = true;
                                            }

                                        if(!update)
                                            FinalBuilds[i].ParentTicket.Stories[j].SubTickets.Add(child);
                                    }
                    }

                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
            }
        }