Skip to content

Commit d545399

Browse files
committed
Remove the RepositoryLoader trait
1 parent 263b7df commit d545399

File tree

5 files changed

+32
-33
lines changed

5 files changed

+32
-33
lines changed

src/bin/bors.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ fn try_main(opts: Opts) -> anyhow::Result<()> {
8888
"https://api.github.com".to_string(),
8989
opts.private_key.into_bytes().into(),
9090
)?;
91-
let repos = client.load_repositories(&team_api).await?;
91+
let repos = RepositoryLoader::new(client.clone())
92+
.load_repositories(&team_api)
93+
.await?;
9294
Ok::<_, anyhow::Error>((client, repos))
9395
})?;
9496

src/bors/handlers/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub static WAIT_FOR_REFRESH: TestSyncMarker = TestSyncMarker::new();
121121
pub async fn handle_bors_global_event(
122122
event: BorsGlobalEvent,
123123
ctx: Arc<BorsContext>,
124-
repo_loader: &dyn RepositoryLoader,
124+
repo_loader: &RepositoryLoader,
125125
team_api_client: &TeamApiClient,
126126
) -> anyhow::Result<()> {
127127
let db = Arc::clone(&ctx.db);
@@ -243,7 +243,7 @@ async fn handle_comment(
243243

244244
async fn reload_repos(
245245
ctx: Arc<BorsContext>,
246-
repo_loader: &dyn RepositoryLoader,
246+
repo_loader: &RepositoryLoader,
247247
team_api_client: &TeamApiClient,
248248
) -> anyhow::Result<()> {
249249
let reloaded_repos = repo_loader.load_repositories(team_api_client).await?;

src/bors/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

33
use arc_swap::ArcSwap;
4-
use axum::async_trait;
4+
use octocrab::Octocrab;
55

66
pub use command::CommandParser;
77
pub use comment::Comment;
@@ -12,6 +12,7 @@ pub use handlers::{handle_bors_global_event, handle_bors_repository_event};
1212

1313
use crate::config::RepositoryConfig;
1414
use crate::github::api::client::GithubRepositoryClient;
15+
use crate::github::api::load_repositories;
1516
use crate::github::GithubRepoName;
1617
use crate::permissions::UserPermissions;
1718
use crate::TeamApiClient;
@@ -22,15 +23,23 @@ mod context;
2223
pub mod event;
2324
mod handlers;
2425

25-
/// Temporary trait to sastify the test mocking.
26-
/// TODO: Remove this trait once we move to mock REST API call.
27-
#[async_trait]
28-
pub trait RepositoryLoader: Send + Sync {
26+
/// Loads repositories through a global GitHub client.
27+
pub struct RepositoryLoader {
28+
client: Octocrab,
29+
}
30+
31+
impl RepositoryLoader {
32+
pub fn new(client: Octocrab) -> Self {
33+
Self { client }
34+
}
35+
2936
/// Load state of repositories.
30-
async fn load_repositories(
37+
pub async fn load_repositories(
3138
&self,
3239
team_api_client: &TeamApiClient,
33-
) -> anyhow::Result<HashMap<GithubRepoName, anyhow::Result<RepositoryState>>>;
40+
) -> anyhow::Result<HashMap<GithubRepoName, anyhow::Result<RepositoryState>>> {
41+
load_repositories(&self.client, team_api_client).await
42+
}
3443
}
3544

3645
#[derive(Clone, Debug)]

src/github/api/client.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
use std::collections::HashMap;
2-
31
use anyhow::Context;
4-
use axum::async_trait;
52
use octocrab::models::{App, Repository};
63
use octocrab::{Error, Octocrab};
74
use tracing::log;
85

96
use crate::bors::event::PullRequestComment;
10-
use crate::bors::{CheckSuite, CheckSuiteStatus, Comment, RepositoryLoader, RepositoryState};
7+
use crate::bors::{CheckSuite, CheckSuiteStatus, Comment};
118
use crate::config::{RepositoryConfig, CONFIG_FILE_PATH};
129
use crate::database::RunId;
1310
use crate::github::api::base_github_html_url;
1411
use crate::github::api::operations::{merge_branches, set_branch_to_commit, MergeError};
1512
use crate::github::{Branch, CommitSha, GithubRepoName, PullRequest, PullRequestNumber};
16-
use crate::permissions::TeamApiClient;
17-
18-
use super::load_repositories;
1913

2014
/// Provides access to a single app installation (repository) using the GitHub API.
2115
pub struct GithubRepositoryClient {
@@ -288,16 +282,6 @@ impl GithubRepositoryClient {
288282
}
289283
}
290284

291-
#[async_trait]
292-
impl RepositoryLoader for Octocrab {
293-
async fn load_repositories(
294-
&self,
295-
team_api_client: &TeamApiClient,
296-
) -> anyhow::Result<HashMap<GithubRepoName, anyhow::Result<RepositoryState>>> {
297-
load_repositories(self, team_api_client).await
298-
}
299-
}
300-
301285
fn github_pr_to_pr(pr: octocrab::models::pulls::PullRequest) -> PullRequest {
302286
PullRequest {
303287
number: pr.number.into(),
@@ -343,7 +327,10 @@ mod tests {
343327
.await;
344328
let client = mock.github_client();
345329
let team_api_client = mock.team_api_client();
346-
let mut repos = client.load_repositories(&team_api_client).await.unwrap();
330+
let mut repos = RepositoryLoader::new(client)
331+
.load_repositories(&team_api_client)
332+
.await
333+
.unwrap();
347334
assert_eq!(repos.len(), 2);
348335

349336
let repo = repos

src/github/server.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::bors::event::BorsEvent;
22
use crate::bors::{handle_bors_global_event, handle_bors_repository_event, BorsContext};
33
use crate::github::webhook::GitHubWebhook;
44
use crate::github::webhook::WebhookSecret;
5-
use crate::{BorsGlobalEvent, BorsRepositoryEvent, TeamApiClient};
5+
use crate::{BorsGlobalEvent, BorsRepositoryEvent, RepositoryLoader, TeamApiClient};
66

77
use anyhow::Error;
88
use axum::extract::State;
@@ -87,6 +87,7 @@ pub fn create_bors_process(
8787
) {
8888
let (repository_tx, repository_rx) = mpsc::channel::<BorsRepositoryEvent>(1024);
8989
let (global_tx, global_rx) = mpsc::channel::<BorsGlobalEvent>(1024);
90+
let repo_loader = RepositoryLoader::new(gh_client);
9091

9192
let service = async move {
9293
let ctx = Arc::new(ctx);
@@ -99,7 +100,7 @@ pub fn create_bors_process(
99100
{
100101
tokio::join!(
101102
consume_repository_events(ctx.clone(), repository_rx),
102-
consume_global_events(ctx.clone(), global_rx, gh_client, team_api)
103+
consume_global_events(ctx.clone(), global_rx, repo_loader, team_api)
103104
);
104105
}
105106
// In real execution, the bot runs forever. If there is something that finishes
@@ -111,7 +112,7 @@ pub fn create_bors_process(
111112
_ = consume_repository_events(ctx.clone(), repository_rx) => {
112113
tracing::error!("Repository event handling process has ended");
113114
}
114-
_ = consume_global_events(ctx.clone(), global_rx, gh_client, team_api) => {
115+
_ = consume_global_events(ctx.clone(), global_rx, repo_loader, team_api) => {
115116
tracing::error!("Global event handling process has ended");
116117
}
117118
}
@@ -141,15 +142,15 @@ async fn consume_repository_events(
141142
async fn consume_global_events(
142143
ctx: Arc<BorsContext>,
143144
mut global_rx: mpsc::Receiver<BorsGlobalEvent>,
144-
gh_client: Octocrab,
145+
loader: RepositoryLoader,
145146
team_api: TeamApiClient,
146147
) {
147148
while let Some(event) = global_rx.recv().await {
148149
let ctx = ctx.clone();
149150

150151
let span = tracing::info_span!("GlobalEvent");
151152
tracing::debug!("Received global event: {event:#?}");
152-
if let Err(error) = handle_bors_global_event(event, ctx, &gh_client, &team_api)
153+
if let Err(error) = handle_bors_global_event(event, ctx, &loader, &team_api)
153154
.instrument(span.clone())
154155
.await
155156
{

0 commit comments

Comments
 (0)