1
1
use axum:: async_trait;
2
+ use octocrab:: models:: UserId ;
2
3
use std:: collections:: HashSet ;
3
4
use tokio:: sync:: Mutex ;
4
5
@@ -14,7 +15,7 @@ pub enum PermissionType {
14
15
/// Decides if a GitHub user can perform various actions using the bot.
15
16
#[ async_trait]
16
17
pub trait PermissionResolver {
17
- async fn has_permission ( & self , username : & str , permission : PermissionType ) -> bool ;
18
+ async fn has_permission ( & self , _username : & UserId , _permission : PermissionType ) -> bool ;
18
19
async fn reload ( & self ) ;
19
20
}
20
21
@@ -46,11 +47,11 @@ impl TeamApiPermissionResolver {
46
47
47
48
#[ async_trait]
48
49
impl PermissionResolver for TeamApiPermissionResolver {
49
- async fn has_permission ( & self , username : & str , permission : PermissionType ) -> bool {
50
+ async fn has_permission ( & self , user_id : & UserId , permission : PermissionType ) -> bool {
50
51
self . permissions
51
52
. lock ( )
52
53
. await
53
- . has_permission ( username , permission)
54
+ . has_permission ( user_id , permission)
54
55
}
55
56
56
57
async fn reload ( & self ) {
@@ -59,15 +60,15 @@ impl PermissionResolver for TeamApiPermissionResolver {
59
60
}
60
61
61
62
pub struct UserPermissions {
62
- review_users : HashSet < String > ,
63
- try_users : HashSet < String > ,
63
+ review_users : HashSet < UserId > ,
64
+ try_users : HashSet < UserId > ,
64
65
}
65
66
66
67
impl UserPermissions {
67
- fn has_permission ( & self , username : & str , permission : PermissionType ) -> bool {
68
+ fn has_permission ( & self , user_id : & UserId , permission : PermissionType ) -> bool {
68
69
match permission {
69
- PermissionType :: Review => self . review_users . contains ( username ) ,
70
- PermissionType :: Try => self . try_users . contains ( username ) ,
70
+ PermissionType :: Review => self . review_users . contains ( user_id ) ,
71
+ PermissionType :: Try => self . try_users . contains ( user_id ) ,
71
72
}
72
73
}
73
74
}
@@ -90,14 +91,14 @@ async fn load_permissions(repo: &GithubRepoName) -> anyhow::Result<UserPermissio
90
91
91
92
#[ derive( serde:: Deserialize ) ]
92
93
struct UserPermissionsResponse {
93
- github_users : HashSet < String > ,
94
+ github_ids : HashSet < UserId > ,
94
95
}
95
96
96
97
/// Loads users that are allowed to perform try/review from the Rust Team API.
97
98
async fn load_users_from_team_api (
98
99
repository_name : & str ,
99
100
permission : PermissionType ,
100
- ) -> anyhow:: Result < HashSet < String > > {
101
+ ) -> anyhow:: Result < HashSet < UserId > > {
101
102
let permission = match permission {
102
103
PermissionType :: Review => "review" ,
103
104
PermissionType :: Try => "try" ,
@@ -112,5 +113,5 @@ async fn load_users_from_team_api(
112
113
. json :: < UserPermissionsResponse > ( )
113
114
. await
114
115
. map_err ( |error| anyhow:: anyhow!( "Cannot deserialize users from team API: {error:?}" ) ) ?;
115
- Ok ( users. github_users )
116
+ Ok ( users. github_ids )
116
117
}
0 commit comments