Skip to content

Commit bea2233

Browse files
committed
Remove the DbClient trait
1 parent 4b202f7 commit bea2233

File tree

7 files changed

+38
-110
lines changed

7 files changed

+38
-110
lines changed

src/bors/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ use std::{
33
sync::{Arc, RwLock},
44
};
55

6-
use crate::{bors::command::CommandParser, database::DbClient, github::GithubRepoName};
6+
use crate::{bors::command::CommandParser, github::GithubRepoName, PgDbClient};
77

88
use super::{RepositoryClient, RepositoryState};
99

1010
pub struct BorsContext<Client: RepositoryClient> {
1111
pub parser: CommandParser,
12-
pub db: Arc<dyn DbClient>,
12+
pub db: Arc<PgDbClient>,
1313
pub repositories: RwLock<HashMap<GithubRepoName, Arc<RepositoryState<Client>>>>,
1414
}
1515

1616
impl<Client: RepositoryClient> BorsContext<Client> {
1717
pub fn new(
1818
parser: CommandParser,
19-
db: Arc<dyn DbClient>,
19+
db: Arc<PgDbClient>,
2020
repositories: HashMap<GithubRepoName, Arc<RepositoryState<Client>>>,
2121
) -> Self {
2222
let repositories = RwLock::new(repositories);

src/bors/handlers/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use crate::bors::handlers::workflow::{
1313
handle_check_suite_completed, handle_workflow_completed, handle_workflow_started,
1414
};
1515
use crate::bors::{BorsContext, Comment, RepositoryClient, RepositoryLoader, RepositoryState};
16-
use crate::database::DbClient;
17-
use crate::TeamApiClient;
16+
use crate::{PgDbClient, TeamApiClient};
1817

1918
#[cfg(test)]
2019
use crate::tests::util::TestSyncMarker;
@@ -158,7 +157,7 @@ pub async fn handle_bors_global_event<Client: RepositoryClient>(
158157

159158
async fn handle_comment<Client: RepositoryClient>(
160159
repo: Arc<RepositoryState<Client>>,
161-
database: Arc<dyn DbClient>,
160+
database: Arc<PgDbClient>,
162161
ctx: Arc<BorsContext<Client>>,
163162
comment: PullRequestComment,
164163
) -> anyhow::Result<()> {

src/bors/handlers/refresh.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use chrono::{DateTime, Utc};
77
use crate::bors::handlers::trybuild::cancel_build_workflows;
88
use crate::bors::Comment;
99
use crate::bors::{RepositoryClient, RepositoryState};
10-
use crate::database::{BuildStatus, DbClient};
11-
use crate::TeamApiClient;
10+
use crate::database::BuildStatus;
11+
use crate::{PgDbClient, TeamApiClient};
1212

1313
pub async fn refresh_repository<Client: RepositoryClient>(
1414
repo: Arc<RepositoryState<Client>>,
15-
db: Arc<dyn DbClient>,
15+
db: Arc<PgDbClient>,
1616
team_api_client: &TeamApiClient,
1717
) -> anyhow::Result<()> {
1818
let repo = repo.as_ref();
@@ -30,7 +30,7 @@ pub async fn refresh_repository<Client: RepositoryClient>(
3030

3131
async fn cancel_timed_out_builds<Client: RepositoryClient>(
3232
repo: &RepositoryState<Client>,
33-
db: &dyn DbClient,
33+
db: &PgDbClient,
3434
) -> anyhow::Result<()> {
3535
let running_builds = db.get_running_builds(&repo.repository).await?;
3636
tracing::info!("Found {} running build(s)", running_builds.len());

src/bors/handlers/trybuild.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ use crate::bors::Comment;
88
use crate::bors::RepositoryClient;
99
use crate::bors::RepositoryState;
1010
use crate::database::RunId;
11-
use crate::database::{
12-
BuildModel, BuildStatus, DbClient, PullRequestModel, WorkflowStatus, WorkflowType,
13-
};
11+
use crate::database::{BuildModel, BuildStatus, PullRequestModel, WorkflowStatus, WorkflowType};
1412
use crate::github::{
1513
CommitSha, GithubUser, LabelTrigger, MergeError, PullRequest, PullRequestNumber,
1614
};
1715
use crate::permissions::PermissionType;
16+
use crate::PgDbClient;
1817

1918
// This branch serves for preparing the final commit.
2019
// It will be reset to master and merged with the branch that should be tested.
@@ -32,7 +31,7 @@ pub(super) const TRY_BRANCH_NAME: &str = "automation/bors/try";
3231
/// Otherwise, it will use the latest commit on the main repository branch.
3332
pub(super) async fn command_try_build<Client: RepositoryClient>(
3433
repo: Arc<RepositoryState<Client>>,
35-
db: Arc<dyn DbClient>,
34+
db: Arc<PgDbClient>,
3635
pr: &PullRequest,
3736
author: &GithubUser,
3837
parent: Option<Parent>,
@@ -143,7 +142,7 @@ pub(super) async fn command_try_build<Client: RepositoryClient>(
143142

144143
pub(super) async fn command_try_cancel<Client: RepositoryClient>(
145144
repo: Arc<RepositoryState<Client>>,
146-
db: Arc<dyn DbClient>,
145+
db: Arc<PgDbClient>,
147146
pr: &PullRequest,
148147
author: &GithubUser,
149148
) -> anyhow::Result<()> {
@@ -212,7 +211,7 @@ Cancelled workflows:"#
212211

213212
pub async fn cancel_build_workflows<Client: RepositoryClient>(
214213
repo: &RepositoryState<Client>,
215-
db: &dyn DbClient,
214+
db: &PgDbClient,
216215
build: &BuildModel,
217216
) -> anyhow::Result<Vec<RunId>> {
218217
let pending_workflows = db
@@ -317,7 +316,7 @@ async fn check_try_permissions<Client: RepositoryClient>(
317316
mod tests {
318317
use crate::bors::handlers::trybuild::{TRY_BRANCH_NAME, TRY_MERGE_BRANCH_NAME};
319318
use crate::database::operations::get_all_workflows;
320-
use crate::database::{BuildStatus, DbClient};
319+
use crate::database::BuildStatus;
321320
use crate::github::{CommitSha, PullRequestNumber};
322321
use crate::tests::mocks::{
323322
default_pr_number, default_repo_name, run_test, BorsBuilder, Permissions, Workflow,

src/bors/handlers/workflow.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use crate::bors::handlers::is_bors_observed_branch;
66
use crate::bors::handlers::labels::handle_label_trigger;
77
use crate::bors::{CheckSuiteStatus, Comment};
88
use crate::bors::{RepositoryClient, RepositoryState};
9-
use crate::database::{BuildStatus, DbClient, WorkflowStatus};
9+
use crate::database::{BuildStatus, WorkflowStatus};
1010
use crate::github::LabelTrigger;
11+
use crate::PgDbClient;
1112

1213
pub(super) async fn handle_workflow_started(
13-
db: Arc<dyn DbClient>,
14+
db: Arc<PgDbClient>,
1415
payload: WorkflowStarted,
1516
) -> anyhow::Result<()> {
1617
if !is_bors_observed_branch(&payload.branch) {
@@ -59,7 +60,7 @@ pub(super) async fn handle_workflow_started(
5960

6061
pub(super) async fn handle_workflow_completed<Client: RepositoryClient>(
6162
repo: Arc<RepositoryState<Client>>,
62-
db: Arc<dyn DbClient>,
63+
db: Arc<PgDbClient>,
6364
payload: WorkflowCompleted,
6465
) -> anyhow::Result<()> {
6566
if !is_bors_observed_branch(&payload.branch) {
@@ -81,7 +82,7 @@ pub(super) async fn handle_workflow_completed<Client: RepositoryClient>(
8182

8283
pub(super) async fn handle_check_suite_completed<Client: RepositoryClient>(
8384
repo: Arc<RepositoryState<Client>>,
84-
db: Arc<dyn DbClient>,
85+
db: Arc<PgDbClient>,
8586
payload: CheckSuiteCompleted,
8687
) -> anyhow::Result<()> {
8788
if !is_bors_observed_branch(&payload.branch) {
@@ -99,7 +100,7 @@ pub(super) async fn handle_check_suite_completed<Client: RepositoryClient>(
99100
/// Try to complete a pending build.
100101
async fn try_complete_build<Client: RepositoryClient>(
101102
repo: &RepositoryState<Client>,
102-
db: &dyn DbClient,
103+
db: &PgDbClient,
103104
payload: CheckSuiteCompleted,
104105
) -> anyhow::Result<()> {
105106
if !is_bors_observed_branch(&payload.branch) {

src/database/client.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use axum::async_trait;
21
use sqlx::PgPool;
32

43
use crate::database::{
@@ -12,7 +11,7 @@ use super::operations::{
1211
get_pull_request, get_running_builds, get_workflows_for_build, update_build_status,
1312
update_pr_build_id, update_workflow_status,
1413
};
15-
use super::{DbClient, RunId};
14+
use super::RunId;
1615

1716
/// Provides access to a database using sqlx operations.
1817
#[derive(Clone)]
@@ -24,11 +23,8 @@ impl PgDbClient {
2423
pub fn new(pool: PgPool) -> Self {
2524
Self { pool }
2625
}
27-
}
2826

29-
#[async_trait]
30-
impl DbClient for PgDbClient {
31-
async fn get_or_create_pull_request(
27+
pub async fn get_or_create_pull_request(
3228
&self,
3329
repo: &GithubRepoName,
3430
pr_number: PullRequestNumber,
@@ -44,14 +40,14 @@ impl DbClient for PgDbClient {
4440
Ok(pr)
4541
}
4642

47-
async fn find_pr_by_build(
43+
pub async fn find_pr_by_build(
4844
&self,
4945
build: &BuildModel,
5046
) -> anyhow::Result<Option<PullRequestModel>> {
5147
find_pr_by_build(&self.pool, build.id).await
5248
}
5349

54-
async fn attach_try_build(
50+
pub async fn attach_try_build(
5551
&self,
5652
pr: PullRequestModel,
5753
branch: String,
@@ -66,7 +62,7 @@ impl DbClient for PgDbClient {
6662
Ok(())
6763
}
6864

69-
async fn find_build(
65+
pub async fn find_build(
7066
&self,
7167
repo: &GithubRepoName,
7268
branch: String,
@@ -75,19 +71,22 @@ impl DbClient for PgDbClient {
7571
find_build(&self.pool, repo, &branch, &commit_sha).await
7672
}
7773

78-
async fn get_running_builds(&self, repo: &GithubRepoName) -> anyhow::Result<Vec<BuildModel>> {
74+
pub async fn get_running_builds(
75+
&self,
76+
repo: &GithubRepoName,
77+
) -> anyhow::Result<Vec<BuildModel>> {
7978
get_running_builds(&self.pool, repo).await
8079
}
8180

82-
async fn update_build_status(
81+
pub async fn update_build_status(
8382
&self,
8483
build: &BuildModel,
8584
status: BuildStatus,
8685
) -> anyhow::Result<()> {
8786
update_build_status(&self.pool, build.id, status).await
8887
}
8988

90-
async fn create_workflow(
89+
pub async fn create_workflow(
9190
&self,
9291
build: &BuildModel,
9392
name: String,
@@ -108,15 +107,15 @@ impl DbClient for PgDbClient {
108107
.await
109108
}
110109

111-
async fn update_workflow_status(
110+
pub async fn update_workflow_status(
112111
&self,
113112
run_id: u64,
114113
status: WorkflowStatus,
115114
) -> anyhow::Result<()> {
116115
update_workflow_status(&self.pool, run_id, status).await
117116
}
118117

119-
async fn get_workflows_for_build(
118+
pub async fn get_workflows_for_build(
120119
&self,
121120
build: &BuildModel,
122121
) -> anyhow::Result<Vec<WorkflowModel>> {

src/database/mod.rs

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
//! Provides access to the database.
2-
mod client;
3-
pub(crate) mod operations;
4-
52
use std::fmt::{Display, Formatter};
63

7-
use axum::async_trait;
84
use chrono::{DateTime, Utc};
95

106
pub use client::PgDbClient;
117

12-
use crate::github::{CommitSha, GithubRepoName, PullRequestNumber};
8+
use crate::github::{GithubRepoName, PullRequestNumber};
9+
10+
mod client;
11+
pub(crate) mod operations;
1312

1413
type PrimaryKey = i32;
1514

@@ -155,72 +154,3 @@ pub struct WorkflowModel {
155154
pub status: WorkflowStatus,
156155
pub created_at: DateTime<Utc>,
157156
}
158-
159-
/// Provides access to a database.
160-
#[async_trait]
161-
pub trait DbClient: Sync + Send {
162-
/// Finds a Pull request row for the given repository and PR number.
163-
/// If it doesn't exist, a new row is created.
164-
async fn get_or_create_pull_request(
165-
&self,
166-
repo: &GithubRepoName,
167-
pr_number: PullRequestNumber,
168-
) -> anyhow::Result<PullRequestModel>;
169-
170-
/// Finds a Pull request by a build (either a try or merge one).
171-
async fn find_pr_by_build(
172-
&self,
173-
build: &BuildModel,
174-
) -> anyhow::Result<Option<PullRequestModel>>;
175-
176-
/// Attaches an existing build to the given PR.
177-
async fn attach_try_build(
178-
&self,
179-
pr: PullRequestModel,
180-
branch: String,
181-
commit_sha: CommitSha,
182-
parent: CommitSha,
183-
) -> anyhow::Result<()>;
184-
185-
/// Finds a build row by its repository, commit SHA and branch.
186-
async fn find_build(
187-
&self,
188-
repo: &GithubRepoName,
189-
branch: String,
190-
commit_sha: CommitSha,
191-
) -> anyhow::Result<Option<BuildModel>>;
192-
193-
/// Returns all builds that have not been completed yet.
194-
async fn get_running_builds(&self, repo: &GithubRepoName) -> anyhow::Result<Vec<BuildModel>>;
195-
196-
/// Updates the status of this build in the DB.
197-
async fn update_build_status(
198-
&self,
199-
build: &BuildModel,
200-
status: BuildStatus,
201-
) -> anyhow::Result<()>;
202-
203-
/// Creates a new workflow attached to a build.
204-
async fn create_workflow(
205-
&self,
206-
build: &BuildModel,
207-
name: String,
208-
url: String,
209-
run_id: RunId,
210-
workflow_type: WorkflowType,
211-
status: WorkflowStatus,
212-
) -> anyhow::Result<()>;
213-
214-
/// Updates the status of a workflow with the given run ID in the DB.
215-
async fn update_workflow_status(
216-
&self,
217-
run_id: u64,
218-
status: WorkflowStatus,
219-
) -> anyhow::Result<()>;
220-
221-
/// Get all workflows attached to a build.
222-
async fn get_workflows_for_build(
223-
&self,
224-
build: &BuildModel,
225-
) -> anyhow::Result<Vec<WorkflowModel>>;
226-
}

0 commit comments

Comments
 (0)