Skip to content

Commit e27ba0f

Browse files
feat: use user id instead of username
1 parent debf50e commit e27ba0f

File tree

7 files changed

+23
-15
lines changed

7 files changed

+23
-15
lines changed

src/bors/handlers/trybuild.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ async fn check_try_permissions<Client: RepositoryClient>(
262262
) -> anyhow::Result<bool> {
263263
let result = if !repo
264264
.permissions_resolver
265-
.has_permission(&author.username, PermissionType::Try)
265+
.has_permission(&author.id, PermissionType::Try)
266266
.await
267267
{
268268
tracing::info!("Permission denied");

src/github/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl Display for GithubRepoName {
4646

4747
#[derive(Debug, PartialEq)]
4848
pub struct GithubUser {
49+
pub id: u64,
4950
pub username: String,
5051
pub html_url: Url,
5152
}

src/github/webhook.rs

+4
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ fn parse_comment_from_pr_review(
282282

283283
fn parse_user(user: Author) -> GithubUser {
284284
GithubUser {
285+
id: user.id.0,
285286
username: user.login,
286287
html_url: user.html_url,
287288
}
@@ -381,6 +382,7 @@ mod tests {
381382
name: "bors-kindergarten",
382383
},
383384
author: GithubUser {
385+
id: 4539057,
384386
username: "Kobzol",
385387
html_url: Url {
386388
scheme: "https",
@@ -424,6 +426,7 @@ mod tests {
424426
name: "bors-kindergarten",
425427
},
426428
author: GithubUser {
429+
id: 4539057,
427430
username: "Kobzol",
428431
html_url: Url {
429432
scheme: "https",
@@ -467,6 +470,7 @@ mod tests {
467470
name: "bors-kindergarten",
468471
},
469472
author: GithubUser {
473+
id: 4539057,
470474
username: "Kobzol",
471475
html_url: Url {
472476
scheme: "https",

src/permissions.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub enum PermissionType {
1414
/// Decides if a GitHub user can perform various actions using the bot.
1515
#[async_trait]
1616
pub trait PermissionResolver {
17-
async fn has_permission(&self, username: &str, permission: PermissionType) -> bool;
17+
async fn has_permission(&self, user_id: &u64, permission: PermissionType) -> bool;
1818
async fn reload(&self);
1919
}
2020

@@ -46,11 +46,11 @@ impl TeamApiPermissionResolver {
4646

4747
#[async_trait]
4848
impl PermissionResolver for TeamApiPermissionResolver {
49-
async fn has_permission(&self, username: &str, permission: PermissionType) -> bool {
49+
async fn has_permission(&self, user_id: &u64, permission: PermissionType) -> bool {
5050
self.permissions
5151
.lock()
5252
.await
53-
.has_permission(username, permission)
53+
.has_permission(user_id, permission)
5454
}
5555

5656
async fn reload(&self) {
@@ -59,15 +59,15 @@ impl PermissionResolver for TeamApiPermissionResolver {
5959
}
6060

6161
pub struct UserPermissions {
62-
review_users: HashSet<String>,
63-
try_users: HashSet<String>,
62+
review_users: HashSet<u64>,
63+
try_users: HashSet<u64>,
6464
}
6565

6666
impl UserPermissions {
67-
fn has_permission(&self, username: &str, permission: PermissionType) -> bool {
67+
fn has_permission(&self, user_id: &u64, permission: PermissionType) -> bool {
6868
match permission {
69-
PermissionType::Review => self.review_users.contains(username),
70-
PermissionType::Try => self.try_users.contains(username),
69+
PermissionType::Review => self.review_users.contains(user_id),
70+
PermissionType::Try => self.try_users.contains(user_id),
7171
}
7272
}
7373
}
@@ -90,14 +90,14 @@ async fn load_permissions(repo: &GithubRepoName) -> anyhow::Result<UserPermissio
9090

9191
#[derive(serde::Deserialize)]
9292
struct UserPermissionsResponse {
93-
github_users: HashSet<String>,
93+
github_ids: HashSet<u64>,
9494
}
9595

9696
/// Loads users that are allowed to perform try/review from the Rust Team API.
9797
async fn load_users_from_team_api(
9898
repository_name: &str,
9999
permission: PermissionType,
100-
) -> anyhow::Result<HashSet<String>> {
100+
) -> anyhow::Result<HashSet<u64>> {
101101
let permission = match permission {
102102
PermissionType::Review => "review",
103103
PermissionType::Try => "try",
@@ -112,5 +112,5 @@ async fn load_users_from_team_api(
112112
.json::<UserPermissionsResponse>()
113113
.await
114114
.map_err(|error| anyhow::anyhow!("Cannot deserialize users from team API: {error:?}"))?;
115-
Ok(users.github_users)
115+
Ok(users.github_ids)
116116
}

src/tests/event.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::tests::state::{default_merge_sha, default_repo_name};
99

1010
fn default_user() -> GithubUser {
1111
GithubUser {
12+
id: 1,
1213
username: "<user>".to_string(),
1314
html_url: "https://user.com".parse().unwrap(),
1415
}

src/tests/permissions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub struct NoPermissions;
77

88
#[async_trait]
99
impl PermissionResolver for NoPermissions {
10-
async fn has_permission(&self, _username: &str, _permission: PermissionType) -> bool {
10+
async fn has_permission(&self, _username: &u64, _permission: PermissionType) -> bool {
1111
false
1212
}
1313
async fn reload(&self) {}
@@ -17,7 +17,7 @@ pub struct AllPermissions;
1717

1818
#[async_trait]
1919
impl PermissionResolver for AllPermissions {
20-
async fn has_permission(&self, _username: &str, _permission: PermissionType) -> bool {
20+
async fn has_permission(&self, _username: &u64, _permission: PermissionType) -> bool {
2121
true
2222
}
2323
async fn reload(&self) {}
@@ -37,7 +37,7 @@ impl Default for MockPermissions {
3737

3838
#[async_trait]
3939
impl PermissionResolver for Arc<Mutex<MockPermissions>> {
40-
async fn has_permission(&self, _username: &str, _permission: PermissionType) -> bool {
40+
async fn has_permission(&self, _username: &u64, _permission: PermissionType) -> bool {
4141
false
4242
}
4343
async fn reload(&self) {

src/tests/state.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use crate::tests::github::{default_base_branch, PRBuilder};
2929

3030
pub fn test_bot_user() -> GithubUser {
3131
GithubUser {
32+
// just a random one, to reduce the chance of duplicate id
33+
id: 517237103,
3234
username: "<test-bot>".to_string(),
3335
html_url: "https://test-bors.bot.com".parse().unwrap(),
3436
}

0 commit comments

Comments
 (0)