1
+ pub mod contributors;
1
2
pub mod events;
2
3
pub mod extensions;
3
4
pub mod ips_file;
4
5
pub mod slack;
5
6
6
7
use crate :: {
7
8
auth,
8
- db:: { ContributorSelector , User , UserId } ,
9
+ db:: { User , UserId } ,
9
10
rpc, AppState , Error , Result ,
10
11
} ;
11
12
use anyhow:: anyhow;
12
13
use axum:: {
13
14
body:: Body ,
14
- extract:: { self , Path , Query } ,
15
+ extract:: { Path , Query } ,
15
16
http:: { self , Request , StatusCode } ,
16
17
middleware:: { self , Next } ,
17
18
response:: IntoResponse ,
18
19
routing:: { get, post} ,
19
20
Extension , Json , Router ,
20
21
} ;
21
22
use axum_extra:: response:: ErasedJson ;
22
- use chrono:: { NaiveDateTime , SecondsFormat } ;
23
23
use serde:: { Deserialize , Serialize } ;
24
- use std:: sync:: { Arc , OnceLock } ;
24
+ use std:: sync:: Arc ;
25
25
use tower:: ServiceBuilder ;
26
26
27
27
pub use extensions:: fetch_extensions_from_blob_store_periodically;
@@ -31,8 +31,7 @@ pub fn routes(rpc_server: Option<Arc<rpc::Server>>, state: Arc<AppState>) -> Rou
31
31
. route ( "/user" , get ( get_authenticated_user) )
32
32
. route ( "/users/:id/access_tokens" , post ( create_access_token) )
33
33
. route ( "/rpc_server_snapshot" , get ( get_rpc_server_snapshot) )
34
- . route ( "/contributors" , get ( get_contributors) . post ( add_contributor) )
35
- . route ( "/contributor" , get ( check_is_contributor) )
34
+ . merge ( contributors:: router ( ) )
36
35
. layer (
37
36
ServiceBuilder :: new ( )
38
37
. layer ( Extension ( state) )
@@ -126,107 +125,6 @@ async fn get_rpc_server_snapshot(
126
125
Ok ( ErasedJson :: pretty ( rpc_server. snapshot ( ) . await ) )
127
126
}
128
127
129
- async fn get_contributors ( Extension ( app) : Extension < Arc < AppState > > ) -> Result < Json < Vec < String > > > {
130
- Ok ( Json ( app. db . get_contributors ( ) . await ?) )
131
- }
132
-
133
- #[ derive( Debug , Deserialize ) ]
134
- struct CheckIsContributorParams {
135
- github_user_id : Option < i32 > ,
136
- github_login : Option < String > ,
137
- }
138
-
139
- impl CheckIsContributorParams {
140
- fn as_contributor_selector ( self ) -> Result < ContributorSelector > {
141
- if let Some ( github_user_id) = self . github_user_id {
142
- return Ok ( ContributorSelector :: GitHubUserId { github_user_id } ) ;
143
- }
144
-
145
- if let Some ( github_login) = self . github_login {
146
- return Ok ( ContributorSelector :: GitHubLogin { github_login } ) ;
147
- }
148
-
149
- Err ( anyhow ! (
150
- "must be one of `github_user_id` or `github_login`."
151
- ) ) ?
152
- }
153
- }
154
-
155
- #[ derive( Debug , Serialize ) ]
156
- struct CheckIsContributorResponse {
157
- signed_at : Option < String > ,
158
- }
159
-
160
- async fn check_is_contributor (
161
- Extension ( app) : Extension < Arc < AppState > > ,
162
- Query ( params) : Query < CheckIsContributorParams > ,
163
- ) -> Result < Json < CheckIsContributorResponse > > {
164
- let params = params. as_contributor_selector ( ) ?;
165
-
166
- if RenovateBot :: is_renovate_bot ( & params) {
167
- return Ok ( Json ( CheckIsContributorResponse {
168
- signed_at : Some (
169
- RenovateBot :: created_at ( )
170
- . and_utc ( )
171
- . to_rfc3339_opts ( SecondsFormat :: Millis , true ) ,
172
- ) ,
173
- } ) ) ;
174
- }
175
-
176
- Ok ( Json ( CheckIsContributorResponse {
177
- signed_at : app
178
- . db
179
- . get_contributor_sign_timestamp ( & params)
180
- . await ?
181
- . map ( |ts| ts. and_utc ( ) . to_rfc3339_opts ( SecondsFormat :: Millis , true ) ) ,
182
- } ) )
183
- }
184
-
185
- /// The Renovate bot GitHub user (`renovate[bot]`).
186
- ///
187
- /// https://api.github.com/users/renovate[bot]
188
- struct RenovateBot ;
189
-
190
- impl RenovateBot {
191
- const LOGIN : & ' static str = "renovate[bot]" ;
192
- const USER_ID : i32 = 29139614 ;
193
-
194
- /// Returns the `created_at` timestamp for the Renovate bot user.
195
- fn created_at ( ) -> & ' static NaiveDateTime {
196
- static CREATED_AT : OnceLock < NaiveDateTime > = OnceLock :: new ( ) ;
197
- CREATED_AT . get_or_init ( || {
198
- chrono:: DateTime :: parse_from_rfc3339 ( "2017-06-02T07:04:12Z" )
199
- . expect ( "failed to parse 'created_at' for 'renovate[bot]'" )
200
- . naive_utc ( )
201
- } )
202
- }
203
-
204
- /// Returns whether the given contributor selector corresponds to the Renovate bot user.
205
- fn is_renovate_bot ( contributor : & ContributorSelector ) -> bool {
206
- match contributor {
207
- ContributorSelector :: GitHubLogin { github_login } => github_login == Self :: LOGIN ,
208
- ContributorSelector :: GitHubUserId { github_user_id } => {
209
- github_user_id == & Self :: USER_ID
210
- }
211
- }
212
- }
213
- }
214
-
215
- async fn add_contributor (
216
- Extension ( app) : Extension < Arc < AppState > > ,
217
- extract:: Json ( params) : extract:: Json < AuthenticatedUserParams > ,
218
- ) -> Result < ( ) > {
219
- let initial_channel_id = app. config . auto_join_channel_id ;
220
- app. db
221
- . add_contributor (
222
- & params. github_login ,
223
- params. github_user_id ,
224
- params. github_email . as_deref ( ) ,
225
- initial_channel_id,
226
- )
227
- . await
228
- }
229
-
230
128
#[ derive( Deserialize ) ]
231
129
struct CreateAccessTokenQueryParams {
232
130
public_key : String ,
0 commit comments