Exemplo n.º 1
0
        private async Task ExecuteAsync()
        {
            using (var client = new CodecovClient())
            {
                if (Slug == null && Remote != null)
                {
                    var remoteUri = new Uri(Remote);
                    if (remoteUri.Host == "github.com")
                    {
                        // LocalPath will be in format `/:owner/:repo.git` (e.g. `/leonard-thieu/toofz-build.git`).
                        // Slug needs to be in format `:owner/:repo` so the leading `/` and `.git` is stripped off.
                        Slug = remoteUri.LocalPath.Substring("/".Length, remoteUri.LocalPath.Length - 1 - ".git".Length);
                    }
                }
                var @params = new AddNewReportParams
                {
                    RepositoryUploadToken = Token,
                    Branch            = Branch,
                    Build             = Build,
                    Job               = Job,
                    BuildUrl          = BuildUrl,
                    Name              = Name,
                    Slug              = Slug,
                    ConfigurationPath = ConfigurationPath,
                    Service           = Service,
                    PullRequest       = PullRequest,
                };
                if (Flags != null)
                {
                    @params.Flags.AddRange(Flags);
                }

                var urls = await client.AddNewReportAsync(Commit, @params).ConfigureAwait(false);

                Log.LogMessage(MessageImportance.High, $"View report at {urls.ReportUri}");
                Log.LogMessage($"Uploading report to {urls.S3Uri}");

                var codecovReport = WriteCodecovReport();

                var uploadResponse = await client.UploadReportAsync(urls.S3Uri, codecovReport).ConfigureAwait(false);

                if (!uploadResponse.IsSuccessStatusCode)
                {
                    Log.LogError($"Failed to upload report: HTTP {(int)uploadResponse.StatusCode} {uploadResponse.ReasonPhrase}.");
                    var uploadContent = await uploadResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    Log.LogError(uploadContent);
                }
                else
                {
                    var builder = new UriBuilder(urls.S3Uri);
                    builder.Query = null;
                    Log.LogMessage(MessageImportance.High, $"Uploaded report to {builder.Uri}");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="commit"></param>
        /// <param name="params"></param>
        /// <returns></returns>
        public async Task <AddNewReportResponse> AddNewReportAsync(
            string commit,
            AddNewReportParams @params = null)
        {
            @params = @params ?? new AddNewReportParams();

            var requestUri  = "https://codecov.io/upload/v4";
            var queryParams = new Dictionary <string, string>
            {
                ["commit"]    = commit,
                ["token"]     = @params.RepositoryUploadToken,
                ["branch"]    = @params.Branch,
                ["build"]     = @params.Build,
                ["job"]       = @params.Job,
                ["build_url"] = @params.BuildUrl,
                ["name"]      = @params.Name,
                ["slug"]      = @params.Slug,
                ["yaml"]      = @params.ConfigurationPath,
                ["service"]   = @params.Service,
                ["pr"]        = @params.PullRequest,
            };

            if (@params.Flags.Count > 0)
            {
                queryParams.Add("flags", string.Join(",", @params.Flags));
            }
            var query = ToQueryString(queryParams);

            requestUri += query;

            var request = new HttpRequestMessage(HttpMethod.Post, requestUri);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
            var response = await http.SendAsync(request).ConfigureAwait(false);

            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException(content);
            }
            var urls = content.Split('\n');

            return(new AddNewReportResponse(urls[0], urls[1]));
        }