private IEnumerable <string> getPlanJobs(string planKey)
        {
            string endpoint = server.Url + string.Format(PLAN_JOBS, planKey);

            using (Stream stream = getQueryResultStream(endpoint + getBasicAuthParameter(endpoint), true)) {
                XPathDocument doc = XPathUtils.getXmlDocument(stream);

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

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

                XPathNavigator    nav  = doc.CreateNavigator();
                XPathExpression   expr = nav.Compile("/plan/stages/stage/plans/plan");
                XPathNodeIterator it   = nav.Select(expr);
                while (it.MoveNext())
                {
                    string jobKey = XPathUtils.getAttributeSafely(it.Current, "key", null);
                    if (jobKey != null)
                    {
                        result.Add(jobKey);
                    }
                }
                return(result);
            }
        }
        public void runBuild(string planKey)
        {
            if (serverBuildNumber >= BUILD_NUMBER_4_0)
            {
                string endpoint = server.Url + RUN_BUILD_ACTION_NEW + "/" + planKey;

                Stream stream = postWithNullBody(endpoint + getBasicAuthParameter(endpoint), true);

                XPathDocument doc = XPathUtils.getXmlDocument(stream);

                string code = getRestErrorStatusCode(doc);
                if (code != null)
                {
                    throw new Exception(code);
                }
            }
            else
            {
                string endpoint = server.Url + RUN_BUILD_ACTION_OLD
                                  + "?buildKey=" + planKey
                                  + "&auth=" + HttpUtility.UrlEncode(authToken, Encoding.UTF8);

                using (Stream stream = getQueryResultStream(endpoint, false)) {
                    XPathDocument doc = XPathUtils.getXmlDocument(stream);

                    string code = getRemoteExceptionMessages(doc);
                    if (code != null)
                    {
                        throw new Exception(code);
                    }
                }
            }
        }
        public ICollection <BambooTest> getTestResultsOlderThan30(string buildKey)
        {
            string buildUrl = string.Format(ALL_TESTS, buildKey);
            string endpoint = server.Url + buildUrl;

            using (Stream stream = getQueryResultStream(endpoint + getBasicAuthParameter(endpoint), 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("/build/testResults/all/testResult");
                XPathNodeIterator it   = nav.Select(expr);

                if (it.Count == 0)
                {
                    expr = nav.Compile("/result/stages/stage/results/result/testResults/all/testResult");
                    it   = nav.Select(expr);
                }

                return(retrieveTestsFromXPath(it));
            }
        }
        // achtung - gobble all exceptions
        public int getServerBuildNumber()
        {
            string endpoint = server.Url + INFO;

            try {
                using (Stream stream = getQueryResultStream(endpoint + getBasicAuthParameter(endpoint), true)) {
                    XPathDocument doc = XPathUtils.getXmlDocument(stream);

                    string code = getRestErrorStatusCode(doc);
                    if (code != null)
                    {
                        return(0);
                    }

                    XPathNavigator nav = doc.CreateNavigator();

                    XPathExpression   expr = nav.Compile("/info/buildNumber");
                    XPathNodeIterator it   = nav.Select(expr);

                    if (it.MoveNext())
                    {
                        serverBuildNumber = it.Current.ValueAsInt;
                    }
                }
            } catch (Exception e) {
                Debug.WriteLine("RestSession.getServerBuildNumber() - exception (ignored): " + e.Message);
            }
            return(serverBuildNumber);
        }
Пример #5
0
        private List <JiraIssue> createIssueList(Stream s)
        {
            XPathDocument doc = XPathUtils.getXmlDocument(s);

            XPathNavigator nav = doc.CreateNavigator();

            XPathExpression   localeExpr = nav.Compile("/rss/channel/language");
            XPathNodeIterator localeIt   = nav.Select(localeExpr);
            string            locale     = null;

            if (localeIt.MoveNext())
            {
                locale = localeIt.Current.Value;
            }

            XPathExpression   expr = nav.Compile("/rss/channel/item");
            XPathNodeIterator it   = nav.Select(expr);

            List <JiraIssue> list = new List <JiraIssue>();

            while (it.MoveNext())
            {
                list.Add(new JiraIssue(server, locale, it.Current));
            }

            return(list);
        }
        public ICollection <BuildArtifact> getArtifacts(string buildKey)
        {
            string buildUrl = string.Format(getCorrectEnpointOrXPath(ALL_ARTIFACTS, ALL_ARTIFACTS_3_0), buildKey);
            string endpoint = server.Url + buildUrl;

            using (Stream stream = getQueryResultStream(endpoint + getBasicAuthParameter(endpoint), true)) {
                XPathDocument doc = XPathUtils.getXmlDocument(stream);

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

                XPathNavigator nav = doc.CreateNavigator();

                List <BuildArtifact> artifacts = new List <BuildArtifact>();

                XPathExpression   expr = nav.Compile("/build/artifacts/artifact");
                XPathNodeIterator it   = nav.Select(expr);

                if (it.MoveNext())
                {
                    artifacts.Add(getArtifact(null, it.Current));
                }

                expr = nav.Compile("/result/artifacts/artifact");
                it   = nav.Select(expr);

                if (it.MoveNext())
                {
                    artifacts.Add(getArtifact(null, it.Current));
                }

                expr = nav.Compile("/result/stages/stage/results/result");
                it   = nav.Select(expr);

                while (it.MoveNext())
                {
                    string            resultKey = XPathUtils.getAttributeSafely(it.Current, "key", null);
                    XPathNodeIterator it2       = it.Clone();
                    XPathExpression   expr3     = it2.Current.Compile("artifacts/artifact");

                    XPathNodeIterator it3 = it2.Current.Select(expr3);

                    while (it3.MoveNext())
                    {
                        artifacts.Add(getArtifact(resultKey, it3.Current));
                    }
                }

                return(artifacts);
            }
        }
        public RestSession login(string username, string pwd)
        {
#if OLDSKOOL_AUTH
            string endpoint = server.Url + LOGIN_ACTION
                              + "?username="******"&password="******"&os_username="******"&os_password="******"/response/auth");
                XPathNodeIterator it   = nav.Select(expr);
                if (it.Count == 0)
                {
                    throw new Exception("Server did not return any authentication token");
                }
                if (it.Count != 1)
                {
                    throw new Exception("Server returned unexpected number of authentication tokens (" + it.Count + ")");
                }
                it.MoveNext();
                authToken = it.Current.Value;

                getServerBuildNumber();

                LoggedIn = true;
                return(this);
            }
        }
        private BambooBuild.PlanState getPlanStateForBuild(string buildKey)
        {
            BambooBuild.PlanState state;

            string endpoint = server.Url + string.Format(PLAN_DETAILS, BambooBuildUtils.getPlanKey(buildKey));

            using (Stream stream = getQueryResultStream(endpoint + getBasicAuthParameter(endpoint), 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("/plan/isInBuildQueue");
                XPathNodeIterator it         = nav.Select(expr);
                string            inQueue    = "false";
                string            isBuilding = "false";
                if (it.Count > 0)
                {
                    it.MoveNext();
                    inQueue = it.Current.Value;
                }
                expr = nav.Compile("/plan/isBuilding");
                it   = nav.Select(expr);
                if (it.Count > 0)
                {
                    it.MoveNext();
                    isBuilding = it.Current.Value;
                }
                if (inQueue.Equals("true"))
                {
                    state = BambooBuild.PlanState.IN_QUEUE;
                }
                else if (isBuilding.Equals("true"))
                {
                    state = BambooBuild.PlanState.BUILDING;
                }
                else
                {
                    state = BambooBuild.PlanState.IDLE;
                }
            }
            return(state);
        }
        public void addLabel(string planKey, int buildNumber, string label)
        {
            string endpoint = server.Url + ADD_LABEL_ACTION + "?auth=" + HttpUtility.UrlEncode(authToken, Encoding.UTF8)
                              + "&buildKey=" + HttpUtility.UrlEncode(planKey) + "&buildNumber=" + buildNumber + "&label="
                              + HttpUtility.UrlEncode(label);

            using (Stream stream = getQueryResultStream(endpoint, false)) {
                XPathDocument doc = XPathUtils.getXmlDocument(stream);

                string code = getRemoteExceptionMessages(doc);
                if (code != null)
                {
                    throw new Exception(code);
                }
            }
        }
        private IEnumerable <BambooTest> getTestsForJob(string job, string number)
        {
            string endpoint = server.Url + string.Format(ALL_TESTS_3_0, job, number);

            using (Stream stream = getQueryResultStream(endpoint + getBasicAuthParameter(endpoint), 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("/result/testResults/allTests/testResult");
                XPathNodeIterator it   = nav.Select(expr);

                return(retrieveTestsFromXPath(it));
            }
        }
        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);
            }
        }