Skip to content

Commit ea02dca

Browse files
authored
Merge pull request #69 from vohoanglong0107/refactor-move-comment-internal
refactor: move is_internal_comment to per repo
2 parents dc634ea + 775d8f1 commit ea02dca

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use anyhow::Context;
22
use axum::async_trait;
33
use base64::Engine;
4-
use octocrab::models::{Repository, RunId};
4+
use octocrab::models::{App, 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};
@@ -13,6 +14,7 @@ use crate::github::{Branch, CommitSha, GithubRepoName, PullRequest, PullRequestN
1314

1415
/// Provides access to a single app installation (repository) using the GitHub API.
1516
pub struct GithubRepositoryClient {
17+
pub app: App,
1618
/// The client caches the access token for this given repository and refreshes it once it
1719
/// expires.
1820
pub client: Octocrab,
@@ -42,6 +44,11 @@ impl RepositoryClient for GithubRepositoryClient {
4244
self.name()
4345
}
4446

47+
/// Was the comment created by the bot?
48+
async fn is_comment_internal(&self, comment: &PullRequestComment) -> anyhow::Result<bool> {
49+
Ok(comment.author.html_url == self.app.html_url)
50+
}
51+
4552
/// Loads repository configuration from a file located at `[CONFIG_FILE_PATH]` in the main
4653
/// branch.
4754
async fn load_config(&self) -> anyhow::Result<RepositoryConfig> {

src/github/api/mod.rs

Lines changed: 7 additions & 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
})
@@ -135,7 +126,13 @@ async fn create_repo_state(
135126
let name = GithubRepoName::new(&owner.login, &repo.name);
136127
tracing::info!("Found repository {name}");
137128

129+
let app = repo_client
130+
.current()
131+
.app()
132+
.await
133+
.context("Could not load Github App")?;
138134
let client = GithubRepositoryClient {
135+
app,
139136
client: repo_client,
140137
repo_name: name.clone(),
141138
repository: repo,
@@ -167,10 +164,6 @@ async fn create_repo_state(
167164

168165
#[async_trait]
169166
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-
174167
fn get_repo_state(
175168
&self,
176169
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)