Skip to content

Commit fbebfd4

Browse files
vohoanglong0107Kobzol
authored andcommitted
refactor: move internal_comment to per repo
1 parent dc634ea commit fbebfd4

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

src/bors/handlers/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ pub async fn handle_bors_event<Client: RepositoryClient>(
3333
match event {
3434
BorsEvent::Comment(comment) => {
3535
// We want to ignore comments made by this bot
36-
if state.is_comment_internal(&comment) {
37-
tracing::trace!("Ignoring comment {comment:?} because it was authored by this bot");
38-
return Ok(());
39-
}
40-
4136
if let Some(repo) = get_repo_state(state, &comment.repository) {
37+
if repo.client.is_comment_internal(&comment).await? {
38+
tracing::trace!(
39+
"Ignoring comment {comment:?} because it was authored by this bot"
40+
);
41+
return Ok(());
42+
}
43+
4244
let span = tracing::info_span!(
4345
"Comment",
4446
pr = format!("{}#{}", comment.repository, comment.pr_number),

src/bors/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ pub use handlers::handle_bors_event;
2424
pub trait RepositoryClient: Send + Sync {
2525
fn repository(&self) -> &GithubRepoName;
2626

27+
/// Was the comment created by the bot?
28+
async fn is_comment_internal(&self, comment: &PullRequestComment) -> anyhow::Result<bool>;
29+
2730
/// Load repository config.
2831
async fn load_config(&self) -> anyhow::Result<RepositoryConfig>;
2932

@@ -85,9 +88,6 @@ pub struct CheckSuite {
8588
/// It is behind a trait to allow easier mocking in tests.
8689
#[async_trait]
8790
pub trait BorsState<Client: RepositoryClient>: Send + Sync {
88-
/// Was the comment created by the bot?
89-
fn is_comment_internal(&self, comment: &PullRequestComment) -> bool;
90-
9191
/// Get repository and database state for the given repository name.
9292
fn get_repo_state(&self, repo: &GithubRepoName) -> Option<Arc<RepositoryState<Client>>>;
9393

src/github/api/client.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use octocrab::models::{Repository, RunId};
55
use octocrab::{Error, Octocrab};
66
use tracing::log;
77

8+
use crate::bors::event::PullRequestComment;
89
use crate::bors::{CheckSuite, CheckSuiteStatus, Comment, RepositoryClient};
910
use crate::config::{RepositoryConfig, CONFIG_FILE_PATH};
1011
use crate::github::api::operations::{merge_branches, set_branch_to_commit, MergeError};
@@ -42,6 +43,17 @@ impl RepositoryClient for GithubRepositoryClient {
4243
self.name()
4344
}
4445

46+
/// Was the comment created by the bot?
47+
async fn is_comment_internal(&self, comment: &PullRequestComment) -> anyhow::Result<bool> {
48+
let app = self
49+
.client
50+
.current()
51+
.app()
52+
.await
53+
.context("Could not load Github App")?;
54+
Ok(comment.author.html_url == app.html_url)
55+
}
56+
4557
/// Loads repository configuration from a file located at `[CONFIG_FILE_PATH]` in the main
4658
/// branch.
4759
async fn load_config(&self) -> anyhow::Result<RepositoryConfig> {

src/github/api/mod.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ use std::sync::Arc;
44
use anyhow::Context;
55
use arc_swap::ArcSwap;
66
use axum::async_trait;
7-
use octocrab::models::{App, AppId, InstallationRepositories, Repository};
7+
use octocrab::models::{AppId, InstallationRepositories, Repository};
88
use octocrab::Octocrab;
99
use secrecy::{ExposeSecret, SecretVec};
1010

1111
use client::GithubRepositoryClient;
1212

13-
use crate::bors::event::PullRequestComment;
1413
use crate::bors::{BorsState, RepositoryClient, RepositoryState};
1514
use crate::github::GithubRepoName;
1615
use crate::permissions::load_permissions;
@@ -32,7 +31,6 @@ fn base_github_url() -> &'static str {
3231

3332
/// Provides access to managed GitHub repositories.
3433
pub struct GithubAppState {
35-
app: App,
3634
client: Octocrab,
3735
repositories: ArcSwap<RepositoryMap>,
3836
}
@@ -48,15 +46,8 @@ impl GithubAppState {
4846
.build()
4947
.context("Could not create octocrab builder")?;
5048

51-
let app = client
52-
.current()
53-
.app()
54-
.await
55-
.context("Could not load Github App")?;
56-
5749
let repositories = load_repositories(&client).await?;
5850
Ok(GithubAppState {
59-
app,
6051
client,
6152
repositories: ArcSwap::new(Arc::new(repositories)),
6253
})
@@ -167,10 +158,6 @@ async fn create_repo_state(
167158

168159
#[async_trait]
169160
impl BorsState<GithubRepositoryClient> for GithubAppState {
170-
fn is_comment_internal(&self, comment: &PullRequestComment) -> bool {
171-
comment.author.html_url == self.app.html_url
172-
}
173-
174161
fn get_repo_state(
175162
&self,
176163
repo: &GithubRepoName,

src/tests/state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ impl TestBorsState {
135135

136136
#[async_trait]
137137
impl BorsState<Arc<TestRepositoryClient>> for TestBorsState {
138-
fn is_comment_internal(&self, comment: &PullRequestComment) -> bool {
139-
comment.author == test_bot_user()
140-
}
141-
142138
fn get_repo_state(&self, repo: &GithubRepoName) -> Option<Arc<TestRepositoryState>> {
143139
self.repos.get(repo).map(|repo| Arc::clone(repo))
144140
}
@@ -393,6 +389,10 @@ impl RepositoryClient for Arc<TestRepositoryClient> {
393389
&self.name
394390
}
395391

392+
async fn is_comment_internal(&self, comment: &PullRequestComment) -> anyhow::Result<bool> {
393+
Ok(comment.author == test_bot_user())
394+
}
395+
396396
async fn get_branch_sha(&self, name: &str) -> anyhow::Result<CommitSha> {
397397
let sha = self
398398
.branch_history

0 commit comments

Comments
 (0)