private static List <JiraNamedEntity> getLinks(XPathNavigator nav)
        {
            XPathExpression   expr = nav.Compile("issuelink/issuekey");
            XPathNodeIterator it   = nav.Select(expr);

            List <JiraNamedEntity> result = new List <JiraNamedEntity>();

            if (nav.MoveToFirstChild())
            {
                while (it.MoveNext())
                {
                    int    id  = XPathUtils.getAttributeSafely(it.Current, "id", 0);
                    string key = it.Current.Value;
                    result.Add(new JiraNamedEntity(id, key, null));
                }
                nav.MoveToParent();
            }
            return(result);
        }
        private void getComments(XPathNavigator nav)
        {
            XPathExpression   expr = nav.Compile("comment");
            XPathNodeIterator it   = nav.Select(expr);

            if (!nav.MoveToFirstChild())
            {
                return;
            }
            while (it.MoveNext())
            {
                Comment c = new Comment
                {
                    Body    = it.Current.Value,
                    Author  = XPathUtils.getAttributeSafely(it.Current, "author", "Unknown"),
                    Created = XPathUtils.getAttributeSafely(it.Current, "created", "Unknown")
                };
                comments.Add(c);
            }
            nav.MoveToParent();
        }
        private void getAttachments(XPathNavigator nav)
        {
            XPathExpression   expr = nav.Compile("attachment");
            XPathNodeIterator it   = nav.Select(expr);

            if (!nav.MoveToFirstChild())
            {
                return;
            }
            while (it.MoveNext())
            {
                JiraAttachment a = new JiraAttachment(
                    int.Parse(XPathUtils.getAttributeSafely(it.Current, "id", "0")),
                    XPathUtils.getAttributeSafely(it.Current, "name", "none"),
                    JiraIssueUtils.getDateTimeFromJiraTimeString(ServerLanguage, XPathUtils.getAttributeSafely(it.Current, "created", "none")),
                    XPathUtils.getAttributeSafely(it.Current, "author", "none"),
                    int.Parse(XPathUtils.getAttributeSafely(it.Current, "size", "0")));
                attachments.Add(a);
            }
            nav.MoveToParent();
        }
        private void getIssueLinks(XPathNavigator nav)
        {
            XPathExpression   expr = nav.Compile("issuelinktype");
            XPathNodeIterator it   = nav.Select(expr);

            if (!nav.MoveToFirstChild())
            {
                return;
            }
            while (it.MoveNext())
            {
                int id = XPathUtils.getAttributeSafely(it.Current, "id", 0);

                string linkTypeName     = null;
                string outwardLinksName = null;
                string inwardLinksName  = null;
                List <JiraNamedEntity> outwardIdsAndKeys = null;
                List <JiraNamedEntity> inwardIdsAndKeys  = null;

                if (it.Current.MoveToFirstChild())
                {
                    do
                    {
                        switch (it.Current.Name)
                        {
                        case "name":
                            linkTypeName = it.Current.Value;
                            break;

                        case "outwardlinks":
                            outwardLinksName  = XPathUtils.getAttributeSafely(it.Current, "description", null);
                            outwardIdsAndKeys = getLinks(it.Current);
                            break;

                        case "inwardlinks":
                            inwardLinksName  = XPathUtils.getAttributeSafely(it.Current, "description", null);
                            inwardIdsAndKeys = getLinks(it.Current);
                            break;
                        }
                    } while (it.Current.MoveToNext());

                    if (id == 0 || linkTypeName == null)
                    {
                        continue;
                    }

                    IssueLinkType ilt = new IssueLinkType(id, linkTypeName, outwardLinksName, inwardLinksName);
                    if (outwardIdsAndKeys != null)
                    {
                        foreach (JiraNamedEntity entity in outwardIdsAndKeys)
                        {
                            ilt.OutwardLinks.Add(entity.Name);
                        }
                    }
                    if (inwardIdsAndKeys != null)
                    {
                        foreach (JiraNamedEntity entity in inwardIdsAndKeys)
                        {
                            ilt.InwardLinks.Add(entity.Name);
                        }
                    }
                    issueLinks.Add(ilt);
                }
                it.Current.MoveToParent();
            }
            nav.MoveToParent();
        }
        public JiraIssue(JiraServer server, string serverLanguage, XPathNavigator nav)
        {
            Server         = server;
            ServerLanguage = serverLanguage;

            nav.MoveToFirstChild();
            do
            {
                switch (nav.Name)
                {
                case "key":
                    Key        = nav.Value;
                    Id         = XPathUtils.getAttributeSafely(nav, "id", UNKNOWN);
                    ProjectKey = Key.Substring(0, Key.LastIndexOf('-'));
                    break;

                case "parent":
                    ParentKey = nav.Value;
                    break;

                case "subtasks":
                    getSubtasks(nav);
                    break;

                case "summary":
                    Summary = nav.Value;
                    break;

                case "attachments":
                    getAttachments(nav);
                    break;

                case "status":
                    Status        = nav.Value;
                    StatusIconUrl = XPathUtils.getAttributeSafely(nav, "iconUrl", null);
                    StatusId      = XPathUtils.getAttributeSafely(nav, "id", UNKNOWN);
                    break;

                case "priority":
                    Priority        = nav.Value;
                    PriorityIconUrl = XPathUtils.getAttributeSafely(nav, "iconUrl", null);
                    PriorityId      = XPathUtils.getAttributeSafely(nav, "id", UNKNOWN);
                    break;

                case "description":
                    Description = nav.Value;
                    break;

                case "type":
                    IssueType        = nav.Value;
                    IssueTypeIconUrl = XPathUtils.getAttributeSafely(nav, "iconUrl", null);
                    IssueTypeId      = XPathUtils.getAttributeSafely(nav, "id", UNKNOWN);
                    break;

                case "assignee":
                    Assignee = XPathUtils.getAttributeSafely(nav, "username", "Unknown");
                    string assigneName = nav.Value;
                    JiraServerCache.Instance.getUsers(server).putUser(new JiraUser(Assignee, assigneName));
                    break;

                case "reporter":
                    Reporter = XPathUtils.getAttributeSafely(nav, "username", "Unknown");
                    string reporterName = nav.Value;
                    JiraServerCache.Instance.getUsers(server).putUser(new JiraUser(Reporter, reporterName));
                    break;

                case "created":
                    CreationDate = JiraIssueUtils.getDateTimeFromJiraTimeString(serverLanguage, nav.Value);
                    break;

                case "updated":
                    UpdateDate = JiraIssueUtils.getDateTimeFromJiraTimeString(serverLanguage, nav.Value);
                    break;

                case "resolution":
                    Resolution   = nav.Value;
                    ResolutionId = XPathUtils.getAttributeSafely(nav, "id", UNKNOWN);
                    break;

                case "timeestimate":
                    RemainingEstimate          = nav.Value;
                    RemainingEstimateInSeconds = XPathUtils.getAttributeSafely(nav, "seconds", UNKNOWN);
                    break;

                case "timeoriginalestimate":
                    OriginalEstimate          = nav.Value;
                    OriginalEstimateInSeconds = XPathUtils.getAttributeSafely(nav, "seconds", UNKNOWN);
                    break;

                case "timespent":
                    TimeSpent          = nav.Value;
                    TimeSpentInSeconds = XPathUtils.getAttributeSafely(nav, "seconds", UNKNOWN);
                    break;

                case "version":
                    versions.Add(nav.Value);
                    break;

                case "fixVersion":
                    fixVersions.Add(nav.Value);
                    break;

                case "component":
                    components.Add(nav.Value);
                    break;

                case "comments":
                    getComments(nav);
                    break;

                case "environment":
                    Environment = nav.Value;
                    break;

                case "issuelinks":
                    getIssueLinks(nav);
                    break;

                default:
                    break;
                }
            } while (nav.MoveToNext());
            if (Key == null || Summary == null)
            {
                throw new InvalidDataException();
            }
        }
        private ICollection <BambooBuild> getBuildsFromUrlWithStartIndex(string endpoint, int start, bool getPlanState, bool withRecursion, string prefix)
        {
            using (Stream stream = getQueryResultStream(endpoint + getBasicAuthParameter(endpoint) + (withRecursion ? "&start-index=" + start : ""), true)) {
                XPathDocument doc = XPathUtils.getXmlDocument(stream);

                string code = getRestErrorStatusCode(doc);
                if (code != null)
                {
                    throw new Exception(code);
                }

                XPathNavigator nav = doc.CreateNavigator();

                XPathExpression   expr;
                XPathNodeIterator it;

                int totalBuildsCount = 0;
                int maxResult        = 0;
                int startIndex       = 0;

                if (!string.IsNullOrEmpty(prefix))
                {
                    expr = nav.Compile(prefix);
                    it   = nav.Select(expr);
                    if (it.MoveNext())
                    {
                        totalBuildsCount = int.Parse(XPathUtils.getAttributeSafely(it.Current, "size", "0"));
                        maxResult        = int.Parse(XPathUtils.getAttributeSafely(it.Current, "max-result", "0"));
                        startIndex       = int.Parse(XPathUtils.getAttributeSafely(it.Current, "start-index", "0"));
                    }
                }

                expr = nav.Compile(prefix + "/build");
                it   = nav.Select(expr);

                if (it.Count == 0)
                {
                    expr = nav.Compile(prefix + "/result");
                    it   = nav.Select(expr);
                }

                List <BambooBuild> builds = new List <BambooBuild>();

                while (it.MoveNext())
                {
                    int    number = int.Parse(XPathUtils.getAttributeSafely(it.Current, "number", "-1"));
                    string key    = XPathUtils.getAttributeSafely(it.Current, "key", null);
                    string state  = XPathUtils.getAttributeSafely(it.Current, "state", null);
                    it.Current.MoveToFirstChild();
                    string buildRelativeTime        = null;
                    string buildDurationDescription = null;
                    string masterKey           = null;
                    int    successfulTestCount = 0;
                    int    failedTestCount     = 0;
                    string projectName         = null;
                    string buildReason         = null;
                    List <BambooBuild.RelatedIssue> relatedIssues = new List <BambooBuild.RelatedIssue>();

                    do
                    {
                        switch (it.Current.Name)
                        {
                        case "master":
                            masterKey = XPathUtils.getAttributeSafely(it.Current, "key", null);
                            break;

                        case "projectName":
                            projectName = it.Current.Value;
                            break;

                        case "buildRelativeTime":
                            buildRelativeTime = it.Current.Value;
                            break;

                        case "buildDurationDescription":
                            buildDurationDescription = it.Current.Value;
                            break;

                        case "successfulTestCount":
                            successfulTestCount = int.Parse(it.Current.Value);
                            break;

                        case "failedTestCount":
                            failedTestCount = int.Parse(it.Current.Value);
                            break;

                        case "buildReason":
                            buildReason = it.Current.Value;
                            break;

                        case "jiraIssues":
                            if (it.Current.HasChildren)
                            {
                                var chnav = it.Clone().Current;
                                if (chnav != null)
                                {
                                    chnav.MoveToFirstChild();
                                    do
                                    {
                                        var issue = getRelatedIssue(chnav);
                                        if (issue != null)
                                        {
                                            relatedIssues.Add(issue);
                                        }
                                    } while (chnav.MoveToNext());
                                }
                            }
                            break;
                        }
                    } while (it.Current.MoveToNext());
                    if (key == null)
                    {
                        continue;
                    }

                    var planState = getPlanState ? getPlanStateForBuild(key) : BambooBuild.PlanState.IDLE;
                    var build     = new BambooBuild(server,
                                                    key, projectName, masterKey, BambooBuild.stringToResult(state), number, buildRelativeTime,
                                                    buildDurationDescription, successfulTestCount, failedTestCount, buildReason, planState, relatedIssues);
                    builds.Add(build);
                }

                // Yes, recursion here. I hope it works as I think it should. If not, we are all doomed
                if (withRecursion && totalBuildsCount > maxResult + startIndex)
                {
                    builds.AddRange(getBuildsFromUrlWithStartIndex(endpoint, startIndex + maxResult, getPlanState, true, getCorrectEnpointOrXPath(BUILDS_XML_SUBTREE, BUILDS_XML_SUBTREE_3_0)));
                }

                return(builds);
            }
        }
        private ICollection <BambooPlan> getPlansFromUrlWithStartIndex(string endpoint, int start)
        {
            using (Stream stream = getQueryResultStream(endpoint + getBasicAuthParameter(endpoint) + "&start-index=" + start, true)) {
                XPathDocument doc = XPathUtils.getXmlDocument(stream);

                string code = getRestErrorStatusCode(doc);
                if (code != null)
                {
                    throw new Exception(code);
                }

                XPathNavigator nav = doc.CreateNavigator();

                XPathExpression   expr = nav.Compile("/plans/plans");
                XPathNodeIterator it   = nav.Select(expr);
                int totalPlansCount    = 0;
                int maxResult          = 0;
                int startIndex         = 0;
                if (it.MoveNext())
                {
                    totalPlansCount = int.Parse(XPathUtils.getAttributeSafely(it.Current, "size", "0"));
                    maxResult       = int.Parse(XPathUtils.getAttributeSafely(it.Current, "max-result", "0"));
                    startIndex      = int.Parse(XPathUtils.getAttributeSafely(it.Current, "start-index", "0"));
                }

                expr = nav.Compile("/plans/plans/plan");
                it   = nav.Select(expr);

                List <BambooPlan> plans = new List <BambooPlan>();

                while (it.MoveNext())
                {
                    string enabledValue = XPathUtils.getAttributeSafely(it.Current, "enabled", "true");
                    string key          = XPathUtils.getAttributeSafely(it.Current, "key", null);
                    string name         = XPathUtils.getAttributeSafely(it.Current, "name", null);
                    bool   enabled      = true;
                    if (enabledValue != null)
                    {
                        enabled = Boolean.Parse(enabledValue);
                    }
                    it.Current.MoveToFirstChild();
                    bool favourite = false;
                    do
                    {
                        switch (it.Current.Name)
                        {
                        case "isFavourite":
                            favourite = it.Current.Value.Equals("true");
                            break;
                        }
                    } while (it.Current.MoveToNext());
                    if (key == null || name == null)
                    {
                        continue;
                    }
                    BambooPlan plan = new BambooPlan(key, name, enabled, favourite);
                    plans.Add(plan);
                }

                // Yes, recursion here. I hope it works as I think it should. If not, we are all doomed
                if (totalPlansCount > maxResult + startIndex)
                {
                    plans.AddRange(getPlansFromUrlWithStartIndex(endpoint, startIndex + maxResult));
                }

                return(plans);
            }
        }