Skip to content

Commit 0eb786e

Browse files
refactor: split global event from per repo ones
1 parent ea02dca commit 0eb786e

File tree

6 files changed

+291
-198
lines changed

6 files changed

+291
-198
lines changed

src/bin/bors.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::time::Duration;
55

66
use anyhow::Context;
77
use bors::{
8-
create_app, create_bors_process, BorsContext, BorsEvent, CommandParser, GithubAppState,
9-
SeaORMClient, ServerState, WebhookSecret,
8+
create_app, create_bors_process, BorsContext, BorsEvent, BorsGlobalEvent, CommandParser,
9+
GithubAppState, SeaORMClient, ServerState, WebhookSecret,
1010
};
1111
use clap::Parser;
1212
use sea_orm::Database;
@@ -85,7 +85,9 @@ fn try_main(opts: Opts) -> anyhow::Result<()> {
8585
let refresh_process = async move {
8686
loop {
8787
tokio::time::sleep(PERIODIC_REFRESH).await;
88-
refresh_tx.send(BorsEvent::Refresh).await?;
88+
refresh_tx
89+
.send(BorsEvent::Global(BorsGlobalEvent::Refresh))
90+
.await?;
8991
}
9092
};
9193

src/bors/event.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::github::{CommitSha, GithubRepoName, GithubUser, PullRequestNumber};
33
use octocrab::models::RunId;
44

55
#[derive(Debug)]
6-
pub enum BorsEvent {
6+
pub enum BorsRepositoryEvent {
77
/// A comment was posted on a pull request.
88
Comment(PullRequestComment),
99
/// A workflow run on Github Actions or a check run from external CI system has been started.
@@ -13,12 +13,24 @@ pub enum BorsEvent {
1313
/// A check suite has been completed, either as a workflow run on Github Actions, or as a
1414
/// workflow from some external CI system.
1515
CheckSuiteCompleted(CheckSuiteCompleted),
16+
}
17+
18+
#[derive(Debug)]
19+
pub enum BorsGlobalEvent {
1620
/// The configuration of some repository has been changed for the bot's Github App.
1721
InstallationsChanged,
1822
/// Periodic event that serves for checking e.g. timeouts.
1923
Refresh,
2024
}
2125

26+
#[derive(Debug)]
27+
pub enum BorsEvent {
28+
/// An event that happen per repository basis.
29+
Repository(BorsRepositoryEvent),
30+
/// An event that happen with bors globally.
31+
Global(BorsGlobalEvent),
32+
}
33+
2234
#[derive(Debug)]
2335
pub struct PullRequestComment {
2436
pub repository: GithubRepoName,

src/bors/handlers/mod.rs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use tracing::Instrument;
44

55
use crate::bors::command::{BorsCommand, CommandParseError};
6-
use crate::bors::event::{BorsEvent, PullRequestComment};
6+
use crate::bors::event::{BorsRepositoryEvent, PullRequestComment};
77
use crate::bors::handlers::help::command_help;
88
use crate::bors::handlers::ping::command_ping;
99
use crate::bors::handlers::refresh::refresh_repository;
@@ -16,6 +16,8 @@ use crate::database::DbClient;
1616
use crate::github::GithubRepoName;
1717
use crate::utils::logging::LogError;
1818

19+
use super::event::{BorsEvent, BorsGlobalEvent};
20+
1921
mod help;
2022
mod labels;
2123
mod ping;
@@ -28,10 +30,26 @@ pub async fn handle_bors_event<Client: RepositoryClient>(
2830
event: BorsEvent,
2931
state: Arc<dyn BorsState<Client>>,
3032
ctx: Arc<BorsContext>,
33+
) -> anyhow::Result<()> {
34+
match event {
35+
BorsEvent::Repository(event) => {
36+
handle_bors_repository_event(event, state, ctx).await?;
37+
}
38+
BorsEvent::Global(event) => {
39+
handle_bors_global_event(event, state, ctx).await?;
40+
}
41+
}
42+
Ok(())
43+
}
44+
45+
pub async fn handle_bors_repository_event<Client: RepositoryClient>(
46+
event: BorsRepositoryEvent,
47+
state: Arc<dyn BorsState<Client>>,
48+
ctx: Arc<BorsContext>,
3149
) -> anyhow::Result<()> {
3250
let db = Arc::clone(&ctx.db);
3351
match event {
34-
BorsEvent::Comment(comment) => {
52+
BorsRepositoryEvent::Comment(comment) => {
3553
// We want to ignore comments made by this bot
3654
if let Some(repo) = get_repo_state(state, &comment.repository) {
3755
if repo.client.is_comment_internal(&comment).await? {
@@ -64,13 +82,8 @@ pub async fn handle_bors_event<Client: RepositoryClient>(
6482
}
6583
}
6684
}
67-
BorsEvent::InstallationsChanged => {
68-
let span = tracing::info_span!("Repository reload");
69-
if let Err(error) = state.reload_repositories().instrument(span.clone()).await {
70-
span.log_error(error);
71-
}
72-
}
73-
BorsEvent::WorkflowStarted(payload) => {
85+
86+
BorsRepositoryEvent::WorkflowStarted(payload) => {
7487
if let Some(_) = get_repo_state(state, &payload.repository) {
7588
let span = tracing::info_span!(
7689
"Workflow started",
@@ -85,7 +98,7 @@ pub async fn handle_bors_event<Client: RepositoryClient>(
8598
}
8699
}
87100
}
88-
BorsEvent::WorkflowCompleted(payload) => {
101+
BorsRepositoryEvent::WorkflowCompleted(payload) => {
89102
if let Some(repo) = get_repo_state(state, &payload.repository) {
90103
let span = tracing::info_span!(
91104
"Workflow completed",
@@ -100,7 +113,7 @@ pub async fn handle_bors_event<Client: RepositoryClient>(
100113
}
101114
}
102115
}
103-
BorsEvent::CheckSuiteCompleted(payload) => {
116+
BorsRepositoryEvent::CheckSuiteCompleted(payload) => {
104117
if let Some(repo) = get_repo_state(state, &payload.repository) {
105118
let span = tracing::info_span!(
106119
"Check suite completed",
@@ -114,7 +127,24 @@ pub async fn handle_bors_event<Client: RepositoryClient>(
114127
}
115128
}
116129
}
117-
BorsEvent::Refresh => {
130+
}
131+
Ok(())
132+
}
133+
134+
pub async fn handle_bors_global_event<Client: RepositoryClient>(
135+
event: BorsGlobalEvent,
136+
state: Arc<dyn BorsState<Client>>,
137+
ctx: Arc<BorsContext>,
138+
) -> anyhow::Result<()> {
139+
let db = Arc::clone(&ctx.db);
140+
match event {
141+
BorsGlobalEvent::InstallationsChanged => {
142+
let span = tracing::info_span!("Repository reload");
143+
if let Err(error) = state.reload_repositories().instrument(span.clone()).await {
144+
span.log_error(error);
145+
}
146+
}
147+
BorsGlobalEvent::Refresh => {
118148
let span = tracing::info_span!("Refresh");
119149
let repos = state.get_all_repos();
120150
futures::future::join_all(repos.into_iter().map(|repo| {

0 commit comments

Comments
 (0)