Replace WebClient with HttpClient to fix WebClient obsolete warning (#17128)

This commit is contained in:
Ella Hathaway 2023-08-10 14:44:53 -07:00 committed by GitHub
commit 4ccce78877
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,6 +15,7 @@ using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
using Task = Microsoft.Build.Utilities.Task;
@ -43,7 +44,14 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport
[Required]
public string OutputFilePath { get; set; }
public override bool Execute()
/// <summary>
/// Sends HTTP requests and receives HTTP responses.
/// </summary>
private readonly HttpClient client = new();
public override bool Execute() => ExecuteAsync().GetAwaiter().GetResult();
private async Task<bool> ExecuteAsync()
{
string baselineRelativeFileName = PrebuiltBaselineFile.Replace(RootDirectory, "");
string gitLogCommand = $"log --first-parent --pretty=format:%H,%f,%ci -- {PrebuiltBaselineFile}";
@ -51,22 +59,26 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport
DateTime startTime = DateTime.Now;
Log.LogMessage(MessageImportance.High, "Generating summary usage burndown data...");
ParallelQuery<string> data = ExecuteGitCommand(RootDirectory, gitLogCommand).AsParallel().Select(commitLine =>
{
var splitLine = commitLine.Split(',');
var commit = new Commit()
IEnumerable<Task<Commit>> getCommitTasks = ExecuteGitCommand(RootDirectory, gitLogCommand)
.Select(async commitLine =>
{
Sha = splitLine[0],
Title = splitLine[1],
CommitDate = DateTime.Parse(splitLine[2])
};
string fileContents = GetFileContents(baselineRelativeFileName, commit.Sha);
Usage[] usages = UsageData.Parse(XElement.Parse(fileContents)).Usages.NullAsEmpty().ToArray();
commit.PackageVersionCount = usages.Count();
commit.PackageCount = usages.GroupBy(i => i.PackageIdentity.Id).Select(grp => grp.First()).Count();
return commit;
})
.Select(c => c.ToString());
var splitLine = commitLine.Split(',');
var commit = new Commit()
{
Sha = splitLine[0],
Title = splitLine[1],
CommitDate = DateTime.Parse(splitLine[2])
};
string fileContents = await GetFileContentsAsync(baselineRelativeFileName, commit.Sha);
Usage[] usages = UsageData.Parse(XElement.Parse(fileContents)).Usages.NullAsEmpty().ToArray();
commit.PackageVersionCount = usages.Count();
commit.PackageCount = usages.GroupBy(i => i.PackageIdentity.Id).Select(grp => grp.First()).Count();
return commit;
});
Commit[] commits = await System.Threading.Tasks.Task.WhenAll(getCommitTasks);
IEnumerable<string> data = commits.Select(c => c.ToString());
Directory.CreateDirectory(Path.GetDirectoryName(OutputFilePath));
@ -85,12 +97,8 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport
/// <param name="relativeFilePath">The relative path (from the git root) to the file.</param>
/// <param name="commitSha">The commit sha for the version of the file to get.</param>
/// <returns>The contents of the specified file.</returns>
private string GetFileContents(string relativeFilePath, string commitSha)
{
WebClient client = new WebClient();
var xmlString = client.DownloadString($"https://raw.githubusercontent.com/dotnet/source-build/{commitSha}/{relativeFilePath.Replace('\\', '/')}");
return xmlString;
}
private Task<string> GetFileContentsAsync(string relativeFilePath, string commitSha) =>
client.GetStringAsync($"https://raw.githubusercontent.com/dotnet/source-build/{commitSha}/{relativeFilePath.Replace('\\', '/')}");
/// <summary>
/// Executes a git command and returns the result.