Skip to content

Commit d957936

Browse files
committed
update code with new language and api features
1 parent 0d99046 commit d957936

File tree

99 files changed

+716
-765
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+716
-765
lines changed

Clients/CirrusCiClient/CirrusCi.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public static class CirrusCi
2323
static CirrusCi()
2424
{
2525
var collection = new ServiceCollection();
26-
collection.AddClient(ExecutionStrategy.CacheAndNetwork)
27-
.ConfigureHttpClient(c => c.BaseAddress = new("https://api.cirrus-ci.com/graphql"));
26+
collection.AddClient(ExecutionStrategy.CacheAndNetwork).ConfigureHttpClient(c => c.BaseAddress = new("https://api.cirrus-ci.com/graphql"));
2827
ServiceProvider = collection.BuildServiceProvider();
2928
}
3029

Clients/CompatApiClient/ApiConfig.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ static ApiConfig()
9090
catch (Exception e)
9191
{
9292
Log.Fatal(e);
93-
ReverseDirections = new Dictionary<string, char>();
94-
ReverseReleaseTypes = new Dictionary<string, char>();
93+
ReverseDirections = new();
94+
ReverseReleaseTypes = new();
9595
}
9696
}
9797
}

Clients/CompatApiClient/Compression/CompressionMessageHandler.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public CompressionMessageHandler(bool isServer = false)
3131
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
3232
{
3333
if (isServer
34-
&& request.Content?.Headers.ContentEncoding != null
35-
&& request.Content.Headers.ContentEncoding.FirstOrDefault() is string serverEncoding
34+
&& request.Content?.Headers.ContentEncoding.FirstOrDefault() is string serverEncoding
3635
&& Compressors.FirstOrDefault(c => c.EncodingType.Equals(serverEncoding, StringComparison.OrdinalIgnoreCase)) is ICompressor serverDecompressor)
3736
{
3837
request.Content = new DecompressedContent(request.Content, serverDecompressor);

Clients/CompatApiClient/Formatters/CompatApiCommitHashConverter.cs

+5-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@ public sealed class CompatApiCommitHashConverter : JsonConverter<string>
88
{
99
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1010
{
11-
if (reader.TokenType == JsonTokenType.Number
12-
&& !reader.HasValueSequence
13-
&& reader.ValueSpan.Length == 1
14-
&& reader.ValueSpan[0] == (byte)'0')
15-
{
16-
_ = reader.GetInt32();
17-
return null;
18-
}
19-
20-
return reader.GetString();
11+
if (reader is not { TokenType: JsonTokenType.Number, HasValueSequence: false, ValueSpan: [(byte)'0'] })
12+
return reader.GetString();
13+
14+
_ = reader.GetInt32();
15+
return null;
2116
}
2217

2318
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)

Clients/CompatApiClient/Formatters/NamingStyles.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,8 @@ public static string CamelCase(string value)
1818
return value;
1919
}
2020

21-
public static string Dashed(string value)
22-
{
23-
return Delimitied(value, '-');
24-
}
25-
26-
public static string Underscore(string value)
27-
{
28-
return Delimitied(value, '_');
29-
}
21+
public static string Dashed(string value) => Delimitied(value, '-');
22+
public static string Underscore(string value) => Delimitied(value, '_');
3023

3124
private static string Delimitied(string value, char separator)
3225
{

Clients/CompatApiClient/Utils/Statistics.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Numerics;
34

45
namespace CompatApiClient.Utils;
56

67
public static class Statistics
78
{
89
public static long Mean(this IEnumerable<long> data)
910
{
10-
System.Numerics.BigInteger sum = 0;
11+
BigInteger sum = 0;
1112
var itemCount = 0;
1213
foreach (var value in data)
1314
{
@@ -22,12 +23,12 @@ public static long Mean(this IEnumerable<long> data)
2223

2324
public static double StdDev(this IEnumerable<long> data)
2425
{
25-
System.Numerics.BigInteger σx = 0, σx2 = 0;
26+
BigInteger σx = 0, σx2 = 0;
2627
var n = 0;
2728
foreach (var value in data)
2829
{
2930
σx += value;
30-
σx2 += (System.Numerics.BigInteger)value * value;
31+
σx2 += (BigInteger)value * value;
3132
n++;
3233
}
3334
if (n == 0)

Clients/CompatApiClient/Utils/UriExtensions.cs

+6-12
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,15 @@ public static class UriExtensions
1414
public static NameValueCollection ParseQueryString(Uri uri)
1515
{
1616
if (!uri.IsAbsoluteUri)
17-
uri = new Uri(FakeHost, uri);
17+
uri = new(FakeHost, uri);
1818
return uri.ParseQueryString();
1919
}
2020

2121
public static string? GetQueryParameter(this Uri uri, string name)
22-
{
23-
var parameters = ParseQueryString(uri);
24-
return parameters[name];
25-
}
22+
=> ParseQueryString(uri)[name];
2623

2724
public static Uri AddQueryParameter(this Uri uri, string name, string value)
28-
{
29-
var queryValue = WebUtility.UrlEncode(name) + "=" + WebUtility.UrlEncode(value);
30-
return AddQueryValue(uri, queryValue);
31-
}
25+
=> AddQueryValue(uri, WebUtility.UrlEncode(name) + "=" + WebUtility.UrlEncode(value));
3226

3327
public static Uri AddQueryParameters(Uri uri, IEnumerable<KeyValuePair<string, string>> parameters)
3428
{
@@ -105,16 +99,16 @@ private static Uri SetQueryValue(Uri uri, string value)
10599
if (isAbsolute)
106100
{
107101
var builder = new UriBuilder(uri) { Query = value };
108-
return new Uri(builder.ToString());
102+
return new(builder.ToString());
109103
}
110104
else
111105
{
112106
var startWithSlash = uri.OriginalString.StartsWith("/");
113-
uri = new Uri(FakeHost, uri);
107+
uri = new(FakeHost, uri);
114108
var builder = new UriBuilder(uri) { Query = value };
115109
var additionalStrip = startWithSlash ? 0 : 1;
116110
var newUri = builder.ToString()[(FakeHost.OriginalString.Length + additionalStrip)..];
117-
return new Uri(newUri, UriKind.Relative);
111+
return new(newUri, UriKind.Relative);
118112
}
119113
}
120114
}

Clients/CompatApiClient/Utils/Utils.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ public static string AsStorageUnit(this int bytes)
5050
=> AsStorageUnit((long)bytes);
5151

5252
public static string AsStorageUnit(this long bytes)
53-
{
54-
if (bytes < UnderKB)
55-
return $"{bytes} byte{(bytes == 1 ? "" : "s")}";
56-
if (bytes < UnderMB)
57-
return $"{bytes / 1024.0:0.##} KB";
58-
if (bytes < UnderGB)
59-
return $"{bytes / 1024.0 / 1024:0.##} MB";
60-
return $"{bytes / 1024.0 / 1024 / 1024:0.##} GB";
61-
}
53+
=> bytes switch
54+
{
55+
< UnderKB => $"{bytes} byte{(bytes == 1 ? "" : "s")}",
56+
< UnderMB => $"{bytes / 1024.0:0.##} KB",
57+
< UnderGB => $"{bytes / (1024.0 * 1024):0.##} MB",
58+
_ => $"{bytes / (1024.0 * 1024 * 1024):0.##} GB"
59+
};
6260
}

Clients/GithubClient/Client.cs

+34-55
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
using System.Threading.Tasks;
55
using CompatApiClient;
66
using Microsoft.Extensions.Caching.Memory;
7+
using Octokit;
78

89
namespace GithubClient;
910

1011
public class Client
1112
{
12-
private readonly Octokit.GitHubClient client;
13+
private readonly GitHubClient client;
1314

1415
private static readonly TimeSpan PrStatusCacheTime = TimeSpan.FromMinutes(3);
1516
private static readonly TimeSpan IssueStatusCacheTime = TimeSpan.FromMinutes(30);
@@ -22,26 +23,22 @@ public class Client
2223

2324
public Client(string? githubToken)
2425
{
25-
client = new Octokit.GitHubClient(new Octokit.ProductHeaderValue(ApiConfig.ProductName, ApiConfig.ProductVersion));
26-
if (!string.IsNullOrEmpty(githubToken))
27-
{
28-
client.Credentials = new Octokit.Credentials(githubToken);
29-
}
26+
client = new(new ProductHeaderValue(ApiConfig.ProductName, ApiConfig.ProductVersion));
27+
if (githubToken is {Length: >0})
28+
client.Credentials = new(githubToken);
3029
}
3130

32-
public async Task<Octokit.PullRequest?> GetPrInfoAsync(int pr, CancellationToken cancellationToken)
31+
public async Task<PullRequest?> GetPrInfoAsync(int pr, CancellationToken cancellationToken)
3332
{
34-
if (StatusesCache.TryGetValue(pr, out Octokit.PullRequest? result))
33+
if (StatusesCache.TryGetValue(pr, out PullRequest? result))
3534
{
36-
ApiConfig.Log.Debug($"Returned {nameof(Octokit.PullRequest)} for {pr} from cache");
35+
ApiConfig.Log.Debug($"Returned {nameof(PullRequest)} for {pr} from cache");
3736
return result;
3837
}
3938

4039
try
4140
{
42-
var request = client.PullRequest.Get("RPCS3", "rpcs3", pr);
43-
request.Wait(cancellationToken);
44-
result = (await request.ConfigureAwait(false));
41+
result = await client.PullRequest.Get("RPCS3", "rpcs3", pr).WaitAsync(cancellationToken).ConfigureAwait(false);
4542
UpdateRateLimitStats();
4643
}
4744
catch (Exception e)
@@ -50,102 +47,84 @@ public Client(string? githubToken)
5047
}
5148
if (result == null)
5249
{
53-
ApiConfig.Log.Debug($"Failed to get {nameof(Octokit.PullRequest)}, returning empty result");
50+
ApiConfig.Log.Debug($"Failed to get {nameof(PullRequest)}, returning empty result");
5451
return new(pr);
5552
}
5653

5754
StatusesCache.Set(pr, result, PrStatusCacheTime);
58-
ApiConfig.Log.Debug($"Cached {nameof(Octokit.PullRequest)} for {pr} for {PrStatusCacheTime}");
55+
ApiConfig.Log.Debug($"Cached {nameof(PullRequest)} for {pr} for {PrStatusCacheTime}");
5956
return result;
6057
}
6158

62-
public async Task<Octokit.Issue?> GetIssueInfoAsync(int issue, CancellationToken cancellationToken)
59+
public async Task<Issue?> GetIssueInfoAsync(int issue, CancellationToken cancellationToken)
6360
{
64-
if (IssuesCache.TryGetValue(issue, out Octokit.Issue? result))
61+
if (IssuesCache.TryGetValue(issue, out Issue? result))
6562
{
66-
ApiConfig.Log.Debug($"Returned {nameof(Octokit.Issue)} for {issue} from cache");
63+
ApiConfig.Log.Debug($"Returned {nameof(Issue)} for {issue} from cache");
6764
return result;
6865
}
6966

7067
try
7168
{
72-
var request = client.Issue.Get("RPCS3", "rpcs3", issue);
73-
request.Wait(cancellationToken);
74-
result = (await request.ConfigureAwait(false));
69+
result = await client.Issue.Get("RPCS3", "rpcs3", issue).WaitAsync(cancellationToken).ConfigureAwait(false);
7570
UpdateRateLimitStats();
71+
IssuesCache.Set(issue, result, IssueStatusCacheTime);
72+
ApiConfig.Log.Debug($"Cached {nameof(Issue)} for {issue} for {IssueStatusCacheTime}");
73+
return result;
7674
}
7775
catch (Exception e)
7876
{
7977
ApiConfig.Log.Error(e);
8078
}
81-
if (result == null)
82-
{
83-
ApiConfig.Log.Debug($"Failed to get {nameof(Octokit.Issue)}, returning empty result");
84-
return new() { };
85-
}
86-
87-
IssuesCache.Set(issue, result, IssueStatusCacheTime);
88-
ApiConfig.Log.Debug($"Cached {nameof(Octokit.Issue)} for {issue} for {IssueStatusCacheTime}");
89-
return result;
79+
ApiConfig.Log.Debug($"Failed to get {nameof(Issue)}, returning empty result");
80+
return new();
9081
}
9182

92-
public Task<IReadOnlyList<Octokit.PullRequest>?> GetOpenPrsAsync(CancellationToken cancellationToken) => GetPrsWithStatusAsync(new Octokit.PullRequestRequest
93-
{
94-
State = Octokit.ItemStateFilter.Open
95-
}, cancellationToken);
83+
public Task<IReadOnlyList<PullRequest>?> GetOpenPrsAsync(CancellationToken cancellationToken)
84+
=> GetPrsWithStatusAsync(new() { State = ItemStateFilter.Open }, cancellationToken);
9685

97-
public Task<IReadOnlyList<Octokit.PullRequest>?> GetClosedPrsAsync(CancellationToken cancellationToken) => GetPrsWithStatusAsync(new Octokit.PullRequestRequest
86+
public Task<IReadOnlyList<PullRequest>?> GetClosedPrsAsync(CancellationToken cancellationToken) => GetPrsWithStatusAsync(new()
9887
{
99-
State = Octokit.ItemStateFilter.Closed,
100-
SortProperty = Octokit.PullRequestSort.Updated,
101-
SortDirection = Octokit.SortDirection.Descending
88+
State = ItemStateFilter.Closed,
89+
SortProperty = PullRequestSort.Updated,
90+
SortDirection = SortDirection.Descending
10291
}, cancellationToken);
10392

104-
private async Task<IReadOnlyList<Octokit.PullRequest>?> GetPrsWithStatusAsync(Octokit.PullRequestRequest filter, CancellationToken cancellationToken)
93+
private async Task<IReadOnlyList<PullRequest>?> GetPrsWithStatusAsync(PullRequestRequest filter, CancellationToken cancellationToken)
10594
{
106-
var statusURI = "https://api.github.com/repos/RPCS3/rpcs3/pulls?state=" + filter.ToString();
107-
if (StatusesCache.TryGetValue(statusURI, out IReadOnlyList<Octokit.PullRequest>? result))
95+
var statusUri = "https://api.github.com/repos/RPCS3/rpcs3/pulls?state=" + filter;
96+
if (StatusesCache.TryGetValue(statusUri, out IReadOnlyList<PullRequest>? result))
10897
{
10998
ApiConfig.Log.Debug("Returned list of opened PRs from cache");
11099
return result;
111100
}
112101

113102
try
114103
{
115-
var request = client.PullRequest.GetAllForRepository("RPCS3", "rpcs3", filter);
116-
request.Wait(cancellationToken);
117-
118-
result = (await request.ConfigureAwait(false));
104+
result = await client.PullRequest.GetAllForRepository("RPCS3", "rpcs3", filter).WaitAsync(cancellationToken).ConfigureAwait(false);
119105
UpdateRateLimitStats();
106+
StatusesCache.Set(statusUri, result, PrStatusCacheTime);
107+
foreach (var prInfo in result)
108+
StatusesCache.Set(prInfo.Number, prInfo, PrStatusCacheTime);
109+
ApiConfig.Log.Debug($"Cached list of open PRs for {PrStatusCacheTime}");
120110
}
121111
catch (Exception e)
122112
{
123113
ApiConfig.Log.Error(e);
124114
}
125-
if (result != null)
126-
{
127-
StatusesCache.Set(statusURI, result, PrStatusCacheTime);
128-
foreach (var prInfo in result)
129-
StatusesCache.Set(prInfo.Number, prInfo, PrStatusCacheTime);
130-
ApiConfig.Log.Debug($"Cached list of open PRs for {PrStatusCacheTime}");
131-
}
132115
return result;
133116
}
134117

135118
private void UpdateRateLimitStats()
136119
{
137120
var apiInfo = client.GetLastApiInfo();
138121
if (apiInfo == null)
139-
{
140122
return;
141-
}
142123

143124
RateLimit = apiInfo.RateLimit.Limit;
144125
RateLimitRemaining = apiInfo.RateLimit.Remaining;
145126
RateLimitResetTime = DateTimeOffset.FromUnixTimeSeconds(apiInfo.RateLimit.ResetAsUtcEpochSeconds).UtcDateTime;
146-
147127
if (RateLimitRemaining < 10)
148128
ApiConfig.Log.Warn($"Github rate limit is low: {RateLimitRemaining} out of {RateLimit}, will be reset on {RateLimitResetTime:u}");
149129
}
150-
151130
}

Clients/IrdLibraryClient/IrdClient.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class IrdClient
2929
public IrdClient()
3030
{
3131
client = HttpClientFactory.Create(new CompressionMessageHandler());
32-
jsonOptions = new JsonSerializerOptions
32+
jsonOptions = new()
3333
{
3434
PropertyNamingPolicy = SpecialJsonNamingPolicy.SnakeCase,
3535
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
@@ -209,9 +209,8 @@ public async Task<List<Ird>> DownloadAsync(string productCode, string localCache
209209

210210
var idx = html.LastIndexOf("</span>", StringComparison.Ordinal);
211211
var result = html[(idx + 7)..].Trim();
212-
if (string.IsNullOrEmpty(result))
213-
return null;
214-
215-
return result;
212+
if (result is {Length: >0})
213+
return result;
214+
return null;
216215
}
217216
}

0 commit comments

Comments
 (0)