Skip to content

Instantly share code, notes, and snippets.

@karb0f0s
Created September 3, 2022 07:16
Show Gist options
  • Save karb0f0s/776f62ae57c260932a17f0f2f72cf0ae to your computer and use it in GitHub Desktop.
Save karb0f0s/776f62ae57c260932a17f0f2f72cf0ae to your computer and use it in GitHub Desktop.
// https://twitter.com/EgorBo/status/1565328134043344896
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
var sw = Stopwatch.StartNew();
string file = await new HttpClient().GetStringAsync(
"https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/");
MatchCollection pullRequestUrls =
Regex.Matches(file, @"https:\/\/github.com\/(?<owner>[a-zA-Z-]+)\/(?<repo>[a-zA-Z-]+)\/pull\/(?<pull>[0-9]+)");
int total = pullRequestUrls.Count;
int counter = 0;
ConcurrentBag<string> authors = new();
var PullRequests = pullRequestUrls
.Select(m => new PullRequest(m.Value, m.Groups["owner"].Value, m.Groups["repo"].Value, m.Groups["pull"].Value))
.Distinct();
HttpClient githubClient = GetClient();
await Parallel.ForEachAsync(PullRequests.AsParallel(),
new ParallelOptions { MaxDegreeOfParallelism = 8 },
async (pullRequest, cancellationToken) =>
{
try
{
Pull? pull = await githubClient.GetFromJsonAsync<Pull>(
requestUri: $"/repos/{pullRequest.Owner}/{pullRequest.Repo}/pulls/{pullRequest.PullNumber}",
cancellationToken: cancellationToken);
if (pull is null) return;
Interlocked.Increment(ref counter);
Console.WriteLine($"\x1b[36m{counter}/{total}\u001b[0m: {pull.Title} by \u001b[32m{pull.User.Login}\u001b[0m");
authors.Add(pull.User.Login);
}
catch (HttpRequestException)
{
Console.WriteLine($"Failed to load: {pullRequest.ToString()}");
}
});
var pad = authors.Select(a => a.Length).Max() + 1;
foreach (var author in authors.GroupBy(g => g).OrderByDescending(g => g.Count()))
{
Console.WriteLine($"\u001b[32m{author.Key.PadRight(pad)}\u001b[0m -- {author.Count()}");
}
sw.Stop();
Console.WriteLine($"Elapsed time \u001b[33m{sw.Elapsed.TotalSeconds}\u001b[0m seconds.");
static HttpClient GetClient()
{
HttpClient githubClient = new HttpClient()
{
BaseAddress = new Uri(@"https://api.github.com"),
};
githubClient.DefaultRequestHeaders.Accept.Add(new("application/vnd.github.v3+json"));
githubClient.DefaultRequestHeaders.Authorization = new("token", Environment.GetEnvironmentVariable("GITHUB_TOKEN"));
githubClient.DefaultRequestHeaders.UserAgent.ParseAdd("testapp");
return githubClient;
}
record PullRequest(
string Uri,
string Owner,
string Repo,
string PullNumber);
record Pull(
[property: JsonPropertyName("title")] string Title,
[property: JsonPropertyName("user")] User User);
record User(
[property: JsonPropertyName("login")] string Login);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment