Skip to content

Commit ed10bcb

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

File tree

7 files changed

+34
-17
lines changed

7 files changed

+34
-17
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

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! for working with (GitHub) repositories.
33
use std::fmt::{Debug, Display, Formatter};
44

5+
use octocrab::models::UserId;
56
use url::Url;
67

78
pub mod api;
@@ -46,6 +47,7 @@ impl Display for GithubRepoName {
4647

4748
#[derive(Debug, PartialEq)]
4849
pub struct GithubUser {
50+
pub id: UserId,
4951
pub username: String,
5052
pub html_url: Url,
5153
}

src/github/webhook.rs

+10
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,
285286
username: user.login,
286287
html_url: user.html_url,
287288
}
@@ -381,6 +382,9 @@ mod tests {
381382
name: "bors-kindergarten",
382383
},
383384
author: GithubUser {
385+
id: UserId(
386+
4539057,
387+
),
384388
username: "Kobzol",
385389
html_url: Url {
386390
scheme: "https",
@@ -424,6 +428,9 @@ mod tests {
424428
name: "bors-kindergarten",
425429
},
426430
author: GithubUser {
431+
id: UserId(
432+
4539057,
433+
),
427434
username: "Kobzol",
428435
html_url: Url {
429436
scheme: "https",
@@ -467,6 +474,9 @@ mod tests {
467474
name: "bors-kindergarten",
468475
},
469476
author: GithubUser {
477+
id: UserId(
478+
4539057,
479+
),
470480
username: "Kobzol",
471481
html_url: Url {
472482
scheme: "https",

src/permissions.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use axum::async_trait;
2+
use octocrab::models::UserId;
23
use std::collections::HashSet;
34
use tokio::sync::Mutex;
45

@@ -14,7 +15,7 @@ pub enum PermissionType {
1415
/// Decides if a GitHub user can perform various actions using the bot.
1516
#[async_trait]
1617
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;
1819
async fn reload(&self);
1920
}
2021

@@ -46,11 +47,11 @@ impl TeamApiPermissionResolver {
4647

4748
#[async_trait]
4849
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 {
5051
self.permissions
5152
.lock()
5253
.await
53-
.has_permission(username, permission)
54+
.has_permission(user_id, permission)
5455
}
5556

5657
async fn reload(&self) {
@@ -59,15 +60,15 @@ impl PermissionResolver for TeamApiPermissionResolver {
5960
}
6061

6162
pub struct UserPermissions {
62-
review_users: HashSet<String>,
63-
try_users: HashSet<String>,
63+
review_users: HashSet<UserId>,
64+
try_users: HashSet<UserId>,
6465
}
6566

6667
impl UserPermissions {
67-
fn has_permission(&self, username: &str, permission: PermissionType) -> bool {
68+
fn has_permission(&self, user_id: &UserId, permission: PermissionType) -> bool {
6869
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),
7172
}
7273
}
7374
}
@@ -90,14 +91,14 @@ async fn load_permissions(repo: &GithubRepoName) -> anyhow::Result<UserPermissio
9091

9192
#[derive(serde::Deserialize)]
9293
struct UserPermissionsResponse {
93-
github_users: HashSet<String>,
94+
github_ids: HashSet<UserId>,
9495
}
9596

9697
/// Loads users that are allowed to perform try/review from the Rust Team API.
9798
async fn load_users_from_team_api(
9899
repository_name: &str,
99100
permission: PermissionType,
100-
) -> anyhow::Result<HashSet<String>> {
101+
) -> anyhow::Result<HashSet<UserId>> {
101102
let permission = match permission {
102103
PermissionType::Review => "review",
103104
PermissionType::Try => "try",
@@ -112,5 +113,5 @@ async fn load_users_from_team_api(
112113
.json::<UserPermissionsResponse>()
113114
.await
114115
.map_err(|error| anyhow::anyhow!("Cannot deserialize users from team API: {error:?}"))?;
115-
Ok(users.github_users)
116+
Ok(users.github_ids)
116117
}

src/tests/event.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use derive_builder::Builder;
2-
use octocrab::models::RunId;
2+
use octocrab::models::{RunId, UserId};
33

44
use crate::bors::event::PullRequestComment;
55
use crate::bors::{event, CheckSuite, CheckSuiteStatus};
@@ -9,6 +9,7 @@ use crate::tests::state::{default_merge_sha, default_repo_name};
99

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

src/tests/permissions.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use std::sync::{Arc, Mutex};
22

33
use crate::permissions::{PermissionResolver, PermissionType};
44
use axum::async_trait;
5+
use octocrab::models::UserId;
56

67
pub struct NoPermissions;
78

89
#[async_trait]
910
impl PermissionResolver for NoPermissions {
10-
async fn has_permission(&self, _username: &str, _permission: PermissionType) -> bool {
11+
async fn has_permission(&self, _username: &UserId, _permission: PermissionType) -> bool {
1112
false
1213
}
1314
async fn reload(&self) {}
@@ -17,7 +18,7 @@ pub struct AllPermissions;
1718

1819
#[async_trait]
1920
impl PermissionResolver for AllPermissions {
20-
async fn has_permission(&self, _username: &str, _permission: PermissionType) -> bool {
21+
async fn has_permission(&self, _username: &UserId, _permission: PermissionType) -> bool {
2122
true
2223
}
2324
async fn reload(&self) {}
@@ -37,7 +38,7 @@ impl Default for MockPermissions {
3738

3839
#[async_trait]
3940
impl PermissionResolver for Arc<Mutex<MockPermissions>> {
40-
async fn has_permission(&self, _username: &str, _permission: PermissionType) -> bool {
41+
async fn has_permission(&self, _username: &UserId, _permission: PermissionType) -> bool {
4142
false
4243
}
4344
async fn reload(&self) {

src/tests/state.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::time::Duration;
77
use crate::config::RepositoryConfig;
88
use axum::async_trait;
99
use derive_builder::Builder;
10-
use octocrab::models::RunId;
10+
use octocrab::models::{RunId, UserId};
1111

1212
use super::permissions::AllPermissions;
1313
use crate::bors::event::{
@@ -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: UserId(517237103),
3234
username: "<test-bot>".to_string(),
3335
html_url: "https://test-bors.bot.com".parse().unwrap(),
3436
}

0 commit comments

Comments
 (0)