public async Task <Changeset> GetChangesetAsync(BuildInformation buildInformation, int sourceVersion)
        {
            string response;

            using (var httpClient = WebClientFactory.CreateHttpClient(buildInformation.DomainName, buildInformation.Login, buildInformation.CryptedPassword))
            {
                HttpResponseMessage responseMessage;
                try
                {
                    responseMessage = await httpClient.GetAsync(string.Format(buildInformation.ChangesetUrl, sourceVersion));
                }
                catch (Exception ex)
                {
                    return(null);
                }

                if (!responseMessage.IsSuccessStatusCode)
                {
                    return(null);
                }

                response = await responseMessage.Content.ReadAsStringAsync();
            }

            var changeset = JsonConvert.DeserializeObject <Changeset>(response);

            return(changeset);
        }
Example #2
0
        private async Task FillInProgressBuild(BuildInformation buildInformation, dynamic jsonItem, BuildResult buildResult)
        {
            string response2;

            using (
                var httpClient = WebClientFactory.CreateHttpClient(buildInformation.DomainName, buildInformation.Login,
                                                                   buildInformation.CryptedPassword))
            {
                var buildIdUri    = string.Format(buildInformation.TestResultUrl, jsonItem.id);
                var responseMsgs2 = await httpClient.GetAsync(buildIdUri);

                response2 = await responseMsgs2.Content.ReadAsStringAsync();
            }

            UpdateProperties(response2, buildResult);
            buildResult.IsRunning = true;
            var buildReason = string.Format("{0}", jsonItem.reason);

            buildResult.IsGatedCheckin = string.Equals(buildReason, "checkInShelveset",
                                                       StringComparison.InvariantCultureIgnoreCase);
            buildResult.RunningTfsUri           = jsonItem.uri;
            buildResult.RunningBuildId          = jsonItem.id;
            buildResult.RunningBuildRequestedBy = jsonItem.requests[0].requestedFor.displayName;
            buildResult.RunningBuildNumber      = jsonItem.buildNumber;
            buildResult.RunningBuildTfsUri      = jsonItem.uri;
            var runningStartTimeString = string.Format("{0}", jsonItem.startTime);

            if (!string.IsNullOrEmpty(runningStartTimeString))
            {
                buildResult.RunningStartTime = DateTime.Parse(runningStartTimeString, CultureInfo.CurrentCulture,
                                                              DateTimeStyles.AssumeUniversal);
            }
        }
        private static async Task GetInProgressBuildDetails(BuildInformation buildInformation, dynamic jsonItem,
                                                            BuildResult buildResult)
        {
            if (jsonItem._links.details == null)
            {
                // details link not provided by current version of api in VSTS
                return;
            }

            string response;

            try
            {
                using (
                    var httpClient = WebClientFactory.CreateHttpClient(buildInformation.DomainName, buildInformation.Login,
                                                                       buildInformation.CryptedPassword))
                {
                    string buildIdUri    = jsonItem._links.details.href;
                    var    responseMsgs2 = await httpClient.GetAsync(buildIdUri);

                    response = await responseMsgs2.Content.ReadAsStringAsync();
                }
            }
            catch
            {
                return;
            }

            dynamic jsonObject = JsonConvert.DeserializeObject(response);

            foreach (var item in jsonObject.value)
            {
                if (item.type == "BuildError")
                {
                    buildResult.TestsFailed = true;
                    string message = item.fields.Message;
                    buildResult.FailingDetails.Add(message);
                }

                if (item.type == "ActivityTracking")
                {
                    string displayText = item.fields.DisplayText;
                    if (displayText.StartsWith("Run MSTest"))
                    {
                        buildResult.TestsRunning = true;
                    }

                    if (displayText == "Copy Files to Drop Location")
                    {
                        buildResult.TestsFinished = true;
                    }
                }
            }
        }
Example #4
0
        /// <summary>Gets the build definitions.</summary>
        /// <param name="buildServer">The build server.</param>
        /// <returns>The <see cref="TfsConnectResult" />.</returns>
        public async Task <TfsConnectResult> GetBuildDefinitions(BuildServer buildServer)
        {
            string response;

            using (var httpClient = WebClientFactory.CreateHttpClient(buildServer.DomainName, buildServer.Login, buildServer.PasswordBytes))
            {
                HttpResponseMessage responseMessage;
                try
                {
                    responseMessage = await httpClient.GetAsync(buildServer.Url);
                }
                catch (HttpRequestException exception)
                {
                    return(new TfsConnectResult {
                        Message = exception.InnerException != null ? exception.InnerException.Message : exception.Message
                    });
                }

                if (!responseMessage.IsSuccessStatusCode)
                {
                    return(new TfsConnectResult
                    {
                        Message = string.Format("Error {0}:{1} - {2}", (int)responseMessage.StatusCode, responseMessage.StatusCode, responseMessage.ReasonPhrase)
                    });
                }

                response = await responseMessage.Content.ReadAsStringAsync();
            }

            dynamic jsonObject = JsonConvert.DeserializeObject(response);
            var     result     = new TfsConnectResult();

            try
            {
                foreach (var item in jsonObject.value)
                {
                    var buildResult = new BuildDefinitionResult {
                        Id = item.id, Name = item.name, Uri = item.uri, Url = item.url
                    };
                    if (buildServer.TfsVersion == TfsVersion.Version2015)
                    {
                        buildResult.ProjectName = item.project.name;
                    }

                    result.BuildDefinitions.Add(buildResult);
                }
            }
            catch (Exception ex)
            {
                result.Message = "Error on getting list of builds - May be wrong TFS type - " + ex.Message;
            }

            return(result);
        }
        /// <summary>Requests the build.</summary>
        /// <param name="buildInformation">The build information.</param>
        /// <returns>The <see cref="BuildResult"/>.</returns>
        public async Task <BuildResult> RequestBuild(BuildInformation buildInformation)
        {
            string response;

            try
            {
                using (var httpClient = WebClientFactory.CreateHttpClient(buildInformation.DomainName, buildInformation.Login, buildInformation.CryptedPassword))
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var json    = string.Format("{{ \"definition\": {{ \"id\": {0} }}, \"reason\": \"Manual\", \"priority\": \"Normal\" }}", buildInformation.BuildDefinitionId);
                    var request = new HttpRequestMessage(HttpMethod.Post, buildInformation.RequestBuildUrl)
                    {
                        Content = new StringContent(json, Encoding.UTF8, "application/json")
                    };

                    var responseMessage = await httpClient.SendAsync(request);

                    if (responseMessage.StatusCode != HttpStatusCode.OK)
                    {
                        return(new BuildResult {
                            Name = responseMessage.StatusCode.ToString("G")
                        });
                    }

                    response = await responseMessage.Content.ReadAsStringAsync();
                }
            }
            catch (Exception ex)
            {
                return(new BuildResult {
                    Name = ex.Message
                });
            }

            dynamic jsonObject        = JsonConvert.DeserializeObject(response);
            var     buildstatusString = string.Format("{0}", jsonObject.status);

            if (buildStatusReplacement.ContainsKey(buildstatusString))
            {
                buildstatusString = buildStatusReplacement[buildstatusString];
            }

            var    buildStatus = (BuildStatus)Enum.Parse(typeof(BuildStatus), buildstatusString, true);
            string buildId     = jsonObject.uri;

            return(new BuildResult
            {
                Name = jsonObject.definition.name,
                Status = buildStatus,
                RequestedBy = jsonObject.requestedBy.displayName,
                TfsUri = buildId
            });
        }
        public async Task GetTestResultAsync(BuildInformation buildInformation, BuildResult result)
        {
            string response;

            using (var httpClient = WebClientFactory.CreateHttpClient(buildInformation.DomainName, buildInformation.Login, buildInformation.CryptedPassword))
            {
                var postString = string.Format("{{ \"query\":\"SELECT * FROM TestRun WHERE buildUri='vstfs:///Build/Build/{0}'\"}}", result.Id);
                HttpResponseMessage responseMessage;
                try
                {
                    responseMessage = await httpClient.PostAsync(buildInformation.TestRunUrl,
                                                                 new StringContent(postString, Encoding.UTF8, "application/json"));
                }
                catch
                {
                    return;
                }

                if (!responseMessage.IsSuccessStatusCode)
                {
                    return;
                }

                response = await responseMessage.Content.ReadAsStringAsync();
            }

            var     testRuns   = new List <TestRun>();
            dynamic jsonObject = JsonConvert.DeserializeObject(response);

            foreach (var item in jsonObject.value)
            {
                var testRun         = new TestRun();
                int incompletTests  = item.incompleteTests;
                int unanalyzedTests = item.unanalyzedTests;
                testRun.Id          = item.id;
                testRun.TotalTests  = item.totalTests;
                testRun.PassedTests = item.passedTests;
                testRun.FailedTests = incompletTests + unanalyzedTests;
                testRuns.Add(testRun);
            }

            result.TotalTests  = testRuns.Sum(x => x.TotalTests);
            result.PassedTests = testRuns.Sum(x => x.PassedTests);
            result.FailedTests = testRuns.Sum(x => x.FailedTests);
        }
Example #7
0
        /// <summary>Stops the build.</summary>
        /// <param name="buildInformation">The build information.</param>
        /// <param name="buildId">The build identifier.</param>
        /// <returns>True if stopping the build succeed; otherwise false.</returns>
        public async Task <BuildResult> StopBuild(BuildInformation buildInformation, string buildId)
        {
            string response;

            try
            {
                using (var httpClient = WebClientFactory.CreateHttpClient(buildInformation.DomainName, buildInformation.Login, buildInformation.CryptedPassword))
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var requestUri = string.Format(buildInformation.StopBuildUrl, buildId);
                    var request    = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri)
                    {
                        Content = new StringContent("{ \"status\": \"Stopped\" }", Encoding.UTF8, "application/json")
                    };

                    var responseMessage = await httpClient.SendAsync(request);

                    if (responseMessage.StatusCode != HttpStatusCode.OK)
                    {
                        return(new BuildResult {
                            Name = responseMessage.StatusCode.ToString("G")
                        });
                    }

                    response = await responseMessage.Content.ReadAsStringAsync();
                }
            }
            catch (Exception ex)
            {
                return(new BuildResult {
                    Name = ex.Message
                });
            }

            dynamic jsonObject        = JsonConvert.DeserializeObject(response);
            var     buildstatusString = string.Format("{0}", jsonObject.status);
            var     buildStatus       = (BuildStatus)Enum.Parse(typeof(BuildStatus), buildstatusString, true);

            return(new BuildResult {
                Name = jsonObject.buildNumber, Status = buildStatus, RequestedBy = jsonObject.lastChangedBy.displayName, TfsUri = jsonObject.uri
            });
        }
        /// <summary>Gets the build result.</summary>
        /// <param name="buildInformation">The build information.</param>
        /// <returns>The <see cref="BuildResult" />.</returns>
        public async Task <BuildResultCollection> GetBuildResultCollection(BuildInformation buildInformation)
        {
            var collectionResult = new BuildResultCollection();

            string response;

            using (var httpClient = WebClientFactory.CreateHttpClient(buildInformation.DomainName, buildInformation.Login, buildInformation.CryptedPassword))
            {
                HttpResponseMessage responseMessage;
                try
                {
                    responseMessage = await httpClient.GetAsync(buildInformation.BuildDefinitionUrl);
                }
                catch (Exception ex)
                {
                    collectionResult.ErrorMessage = ex.InnerException?.Message ?? ex.Message;
                    return(collectionResult);
                }

                if (!responseMessage.IsSuccessStatusCode)
                {
                    collectionResult.ErrorMessage =
                        $"Error {(int)responseMessage.StatusCode}:{responseMessage.StatusCode} - {responseMessage.ReasonPhrase}";
                    return(collectionResult);
                }

                response = await responseMessage.Content.ReadAsStringAsync();
            }

            try
            {
                dynamic jsonObject = JsonConvert.DeserializeObject(response);

                foreach (var jsonItem in jsonObject.value)
                {
                    var buildResult = new BuildResult();
                    collectionResult.BuildResults.Add(buildResult);

                    buildResult.Status = ReadBuildStatus(jsonItem);

                    string webLinkUri = jsonItem._links.web.href;
                    var    uri        = new Uri(webLinkUri);
                    if (string.Equals(uri.Host, "tfs", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var host = new Uri(buildInformation.BuildDefinitionUrl).Host + ":" + uri.Port;
                        webLinkUri = string.Format("{0}://{1}{2}{3}", uri.Scheme, host, string.Join(string.Empty, uri.Segments), uri.Query);
                    }

                    buildResult.Id          = jsonItem.id ?? -1;
                    buildResult.Name        = jsonItem.definition.name;
                    buildResult.Number      = jsonItem.buildNumber;
                    buildResult.TfsUri      = webLinkUri;
                    buildResult.RequestedBy = jsonItem.requestedFor.displayName;

                    string sourceVersion = jsonItem.sourceVersion;
                    int    changeset;
                    if (!string.IsNullOrEmpty(sourceVersion) && int.TryParse(sourceVersion.Replace("C", string.Empty), out changeset))
                    {
                        buildResult.SourceVersion = changeset;
                    }

                    string finishTimeString = jsonItem.finishTime == null ? string.Empty : string.Format("{0}", jsonItem.finishTime);
                    var    finishDate       = DateTime.MinValue;
                    if (!string.IsNullOrEmpty(finishTimeString))
                    {
                        finishDate = DateTime.Parse(finishTimeString, CultureInfo.CurrentCulture,
                                                    DateTimeStyles.AssumeUniversal);
                        buildResult.FinishTime = finishDate.ToString("g", CultureInfo.CurrentCulture);
                    }

                    string startTimeString = jsonItem.startTime == null ? string.Empty : string.Format("{0}", jsonItem.startTime);
                    if (!string.IsNullOrEmpty(startTimeString))
                    {
                        var startTime = DateTime.Parse(startTimeString, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal);
                        if (finishDate != DateTime.MinValue)
                        {
                            var delta = finishDate - startTime;
                            buildResult.Duration = delta.TotalMinutes;
                        }
                    }

                    if (buildResult.Status == BuildStatus.NotStarted)
                    {
                        FillNotStartedProperties(jsonItem, buildResult, webLinkUri);
                        continue;
                    }

                    if (buildResult.Status == BuildStatus.InProgress)
                    {
                        await FillInProgressProperties(buildInformation, jsonItem, buildResult, webLinkUri);
                    }
                }

                return(collectionResult);
            }
            catch (Exception ex)
            {
                collectionResult.ErrorMessage = ex.Message;
                return(collectionResult);
            }
        }
        /// <summary>Gets the build definitions.</summary>
        /// <param name="buildServer">The build server.</param>
        /// <returns>The <see cref="TfsConnectResult"/>.</returns>
        public async Task <TfsConnectResult> GetBuildDefinitions(BuildServer buildServer)
        {
            string response;

            using (var httpClient = WebClientFactory.CreateHttpClient(buildServer.DomainName, buildServer.Login, buildServer.PasswordBytes))
            {
                HttpResponseMessage responseMessage;
                try
                {
                    responseMessage = await httpClient.GetAsync(string.Concat(buildServer.Url, "/_apis/projects"));
                }
                catch (Exception exception)
                {
                    return(new TfsConnectResult {
                        Message = exception.InnerException != null ? exception.InnerException.Message : exception.Message
                    });
                }

                if (!responseMessage.IsSuccessStatusCode || responseMessage.StatusCode == HttpStatusCode.NonAuthoritativeInformation)
                {
                    return(new TfsConnectResult
                    {
                        Message = string.Format("Error {0}:{1} - {2}", (int)responseMessage.StatusCode, responseMessage.StatusCode, responseMessage.ReasonPhrase)
                    });
                }

                response = await responseMessage.Content.ReadAsStringAsync();
            }

            dynamic projectCollectionJson = JsonConvert.DeserializeObject(response);
            var     result = new TfsConnectResult();

            try
            {
                foreach (var project in projectCollectionJson.value)
                {
                    string projectName = project.name;
                    string projectId   = project.id;

                    using (var httpClient = WebClientFactory.CreateHttpClient(buildServer.DomainName, buildServer.Login, buildServer.PasswordBytes))
                    {
                        HttpResponseMessage responseMessage;
                        try
                        {
                            var requestUri = string.Concat(buildServer.Url, "/", projectId, "/_apis/build/definitions?api-version=2.0");
                            responseMessage = await httpClient.GetAsync(requestUri);
                        }
                        catch (HttpRequestException exception)
                        {
                            return(new TfsConnectResult {
                                Message = exception.InnerException != null ? exception.InnerException.Message : exception.Message
                            });
                        }

                        if (!responseMessage.IsSuccessStatusCode)
                        {
                            return(new TfsConnectResult
                            {
                                Message = string.Format("Error {0}:{1} - {2}", (int)responseMessage.StatusCode, responseMessage.StatusCode, responseMessage.ReasonPhrase)
                            });
                        }

                        response = await responseMessage.Content.ReadAsStringAsync();
                    }

                    dynamic definitionCollectionJson = JsonConvert.DeserializeObject(response);

                    foreach (var definition in definitionCollectionJson.value)
                    {
                        var buildResult = new BuildDefinitionResult {
                            Id = definition.id, Name = definition.name, Uri = definition.uri, Url = definition.url, ProjectName = projectName, ProjectId = projectId
                        };

                        result.BuildDefinitions.Add(buildResult);
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = "Error on getting list of builds - May be wrong TFS type - " + ex.Message;
            }

            return(result);
        }
Example #10
0
        /// <summary>Gets the build result.</summary>
        /// <param name="buildInformation">The build information.</param>
        /// <returns>The <see cref="BuildResult" />.</returns>
        public async Task <BuildResultCollection> GetBuildResultCollection(BuildInformation buildInformation)
        {
            var collectionResult = new BuildResultCollection();

            string response;

            using (var httpClient = WebClientFactory.CreateHttpClient(buildInformation.DomainName, buildInformation.Login, buildInformation.CryptedPassword))
            {
                HttpResponseMessage responseMessage;
                try
                {
                    responseMessage = await httpClient.GetAsync(buildInformation.BuildDefinitionUrl);
                }
                catch (HttpRequestException ex)
                {
                    collectionResult.ErrorMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    return(collectionResult);
                }

                if (!responseMessage.IsSuccessStatusCode)
                {
                    collectionResult.ErrorMessage = string.Format("Error {0}:{1} - {2}", (int)responseMessage.StatusCode,
                                                                  responseMessage.StatusCode, responseMessage.ReasonPhrase);
                    return(collectionResult);
                }

                response = await responseMessage.Content.ReadAsStringAsync();
            }

            try
            {
                dynamic jsonObject = JsonConvert.DeserializeObject(response);

                foreach (var jsonItem in jsonObject.value)
                {
                    var buildResult = new BuildResult();
                    collectionResult.BuildResults.Add(buildResult);

                    buildResult.Name        = jsonItem.definition.name;
                    buildResult.Number      = jsonItem.buildNumber;
                    buildResult.TfsUri      = jsonItem.uri;
                    buildResult.RequestedBy = jsonItem.requests[0].requestedFor.displayName;

                    string finishTimeString = jsonItem.finishTime == null ? string.Empty : string.Format("{0}", jsonItem.finishTime);
                    var    finishDate       = DateTime.MinValue;
                    if (!string.IsNullOrEmpty(finishTimeString))
                    {
                        finishDate = DateTime.Parse(finishTimeString, CultureInfo.CurrentCulture,
                                                    DateTimeStyles.AssumeUniversal);
                        buildResult.FinishTime = finishDate.ToString("g", CultureInfo.CurrentCulture);
                    }

                    string startTimeString = jsonItem.startTime == null ? string.Empty : string.Format("{0}", jsonItem.startTime);
                    if (!string.IsNullOrEmpty(startTimeString))
                    {
                        var startTime = DateTime.Parse(startTimeString, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal);
                        if (finishDate != DateTime.MinValue)
                        {
                            var delta = finishDate - startTime;
                            buildResult.Duration = delta.TotalMinutes;
                        }
                    }

                    var buildstatusString = string.Format("{0}", jsonItem.status);
                    buildResult.Status = (BuildStatus)Enum.Parse(typeof(BuildStatus), buildstatusString, true);
                    if (buildResult.Status == BuildStatus.InProgress)
                    {
                        await FillInProgressBuild(buildInformation, jsonItem, buildResult);
                    }
                }

                return(collectionResult);
            }
            catch (Exception ex)
            {
                collectionResult.ErrorMessage = ex.Message;
                return(collectionResult);
            }
        }