示例#1
0
 public ActionResult Save(Bam.Net.Automation.ContinuousIntegration.Data.BuildResult[] values)
 {
     try
     {
         BuildResultCollection saver = new BuildResultCollection();
         saver.AddRange(values);
         saver.Save();
         return(Json(new { Success = true, Message = "", Dao = "" }));
     }
     catch (Exception ex)
     {
         return(GetErrorResult(ex));
     }
 }
        /// <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);
            }
        }
示例#3
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);
            }
        }