4
4
using System . Threading . Tasks ;
5
5
using CompatApiClient ;
6
6
using Microsoft . Extensions . Caching . Memory ;
7
+ using Octokit ;
7
8
8
9
namespace GithubClient ;
9
10
10
11
public class Client
11
12
{
12
- private readonly Octokit . GitHubClient client ;
13
+ private readonly GitHubClient client ;
13
14
14
15
private static readonly TimeSpan PrStatusCacheTime = TimeSpan . FromMinutes ( 3 ) ;
15
16
private static readonly TimeSpan IssueStatusCacheTime = TimeSpan . FromMinutes ( 30 ) ;
@@ -22,26 +23,22 @@ public class Client
22
23
23
24
public Client ( string ? githubToken )
24
25
{
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 ) ;
30
29
}
31
30
32
- public async Task < Octokit . PullRequest ? > GetPrInfoAsync ( int pr , CancellationToken cancellationToken )
31
+ public async Task < PullRequest ? > GetPrInfoAsync ( int pr , CancellationToken cancellationToken )
33
32
{
34
- if ( StatusesCache . TryGetValue ( pr , out Octokit . PullRequest ? result ) )
33
+ if ( StatusesCache . TryGetValue ( pr , out PullRequest ? result ) )
35
34
{
36
- ApiConfig . Log . Debug ( $ "Returned { nameof ( Octokit . PullRequest ) } for { pr } from cache") ;
35
+ ApiConfig . Log . Debug ( $ "Returned { nameof ( PullRequest ) } for { pr } from cache") ;
37
36
return result ;
38
37
}
39
38
40
39
try
41
40
{
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 ) ;
45
42
UpdateRateLimitStats ( ) ;
46
43
}
47
44
catch ( Exception e )
@@ -50,102 +47,84 @@ public Client(string? githubToken)
50
47
}
51
48
if ( result == null )
52
49
{
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") ;
54
51
return new ( pr ) ;
55
52
}
56
53
57
54
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 } ") ;
59
56
return result ;
60
57
}
61
58
62
- public async Task < Octokit . Issue ? > GetIssueInfoAsync ( int issue , CancellationToken cancellationToken )
59
+ public async Task < Issue ? > GetIssueInfoAsync ( int issue , CancellationToken cancellationToken )
63
60
{
64
- if ( IssuesCache . TryGetValue ( issue , out Octokit . Issue ? result ) )
61
+ if ( IssuesCache . TryGetValue ( issue , out Issue ? result ) )
65
62
{
66
- ApiConfig . Log . Debug ( $ "Returned { nameof ( Octokit . Issue ) } for { issue } from cache") ;
63
+ ApiConfig . Log . Debug ( $ "Returned { nameof ( Issue ) } for { issue } from cache") ;
67
64
return result ;
68
65
}
69
66
70
67
try
71
68
{
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 ) ;
75
70
UpdateRateLimitStats ( ) ;
71
+ IssuesCache . Set ( issue , result , IssueStatusCacheTime ) ;
72
+ ApiConfig . Log . Debug ( $ "Cached { nameof ( Issue ) } for { issue } for { IssueStatusCacheTime } ") ;
73
+ return result ;
76
74
}
77
75
catch ( Exception e )
78
76
{
79
77
ApiConfig . Log . Error ( e ) ;
80
78
}
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 ( ) ;
90
81
}
91
82
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 ) ;
96
85
97
- public Task < IReadOnlyList < Octokit . PullRequest > ? > GetClosedPrsAsync ( CancellationToken cancellationToken ) => GetPrsWithStatusAsync ( new Octokit . PullRequestRequest
86
+ public Task < IReadOnlyList < PullRequest > ? > GetClosedPrsAsync ( CancellationToken cancellationToken ) => GetPrsWithStatusAsync ( new ( )
98
87
{
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
102
91
} , cancellationToken ) ;
103
92
104
- private async Task < IReadOnlyList < Octokit . PullRequest > ? > GetPrsWithStatusAsync ( Octokit . PullRequestRequest filter , CancellationToken cancellationToken )
93
+ private async Task < IReadOnlyList < PullRequest > ? > GetPrsWithStatusAsync ( PullRequestRequest filter , CancellationToken cancellationToken )
105
94
{
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 ) )
108
97
{
109
98
ApiConfig . Log . Debug ( "Returned list of opened PRs from cache" ) ;
110
99
return result ;
111
100
}
112
101
113
102
try
114
103
{
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 ) ;
119
105
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 } ") ;
120
110
}
121
111
catch ( Exception e )
122
112
{
123
113
ApiConfig . Log . Error ( e ) ;
124
114
}
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
- }
132
115
return result ;
133
116
}
134
117
135
118
private void UpdateRateLimitStats ( )
136
119
{
137
120
var apiInfo = client . GetLastApiInfo ( ) ;
138
121
if ( apiInfo == null )
139
- {
140
122
return ;
141
- }
142
123
143
124
RateLimit = apiInfo . RateLimit . Limit ;
144
125
RateLimitRemaining = apiInfo . RateLimit . Remaining ;
145
126
RateLimitResetTime = DateTimeOffset . FromUnixTimeSeconds ( apiInfo . RateLimit . ResetAsUtcEpochSeconds ) . UtcDateTime ;
146
-
147
127
if ( RateLimitRemaining < 10 )
148
128
ApiConfig . Log . Warn ( $ "Github rate limit is low: { RateLimitRemaining } out of { RateLimit } , will be reset on { RateLimitResetTime : u} ") ;
149
129
}
150
-
151
130
}
0 commit comments