@@ -3,7 +3,7 @@ use std::sync::Arc;
3
3
use tracing:: Instrument ;
4
4
5
5
use crate :: bors:: command:: { BorsCommand , CommandParseError } ;
6
- use crate :: bors:: event:: { BorsEvent , PullRequestComment } ;
6
+ use crate :: bors:: event:: { BorsRepositoryEvent , PullRequestComment } ;
7
7
use crate :: bors:: handlers:: help:: command_help;
8
8
use crate :: bors:: handlers:: ping:: command_ping;
9
9
use crate :: bors:: handlers:: refresh:: refresh_repository;
@@ -16,6 +16,8 @@ use crate::database::DbClient;
16
16
use crate :: github:: GithubRepoName ;
17
17
use crate :: utils:: logging:: LogError ;
18
18
19
+ use super :: event:: { BorsEvent , BorsGlobalEvent } ;
20
+
19
21
mod help;
20
22
mod labels;
21
23
mod ping;
@@ -28,93 +30,118 @@ pub async fn handle_bors_event<Client: RepositoryClient>(
28
30
event : BorsEvent ,
29
31
state : Arc < dyn BorsState < Client > > ,
30
32
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 > ,
31
49
) -> anyhow:: Result < ( ) > {
32
50
let db = Arc :: clone ( & ctx. db ) ;
51
+ let Some ( repo) = get_repo_state ( state, event. repository ( ) ) else {
52
+ return Err ( anyhow:: anyhow!(
53
+ "Repository {} not found in the bot state" ,
54
+ event. repository( )
55
+ ) ) ;
56
+ } ;
57
+
33
58
match event {
34
- BorsEvent :: Comment ( comment) => {
59
+ BorsRepositoryEvent :: Comment ( comment) => {
35
60
// We want to ignore comments made by this bot
36
- 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
- }
61
+ if repo. client . is_comment_internal ( & comment) . await ? {
62
+ tracing:: trace!( "Ignoring comment {comment:?} because it was authored by this bot" ) ;
63
+ return Ok ( ( ) ) ;
64
+ }
43
65
44
- let span = tracing:: info_span!(
45
- "Comment" ,
46
- pr = format!( "{}#{}" , comment. repository, comment. pr_number) ,
47
- author = comment. author. username
48
- ) ;
49
- let pr_number = comment. pr_number ;
50
- if let Err ( error) = handle_comment ( Arc :: clone ( & repo) , db, ctx, comment)
51
- . instrument ( span. clone ( ) )
66
+ let span = tracing:: info_span!(
67
+ "Comment" ,
68
+ pr = format!( "{}#{}" , comment. repository, comment. pr_number) ,
69
+ author = comment. author. username
70
+ ) ;
71
+ let pr_number = comment. pr_number ;
72
+ if let Err ( error) = handle_comment ( Arc :: clone ( & repo) , db, ctx, comment)
73
+ . instrument ( span. clone ( ) )
74
+ . await
75
+ {
76
+ span. log_error ( error) ;
77
+ repo. client
78
+ . post_comment (
79
+ pr_number,
80
+ Comment :: new (
81
+ ":x: Encountered an error while executing command" . to_string ( ) ,
82
+ ) ,
83
+ )
52
84
. await
53
- {
54
- span. log_error ( error) ;
55
- repo. client
56
- . post_comment (
57
- pr_number,
58
- Comment :: new (
59
- ":x: Encountered an error while executing command" . to_string ( ) ,
60
- ) ,
61
- )
62
- . await
63
- . context ( "Cannot send comment reacting to an error" ) ?;
64
- }
85
+ . context ( "Cannot send comment reacting to an error" ) ?;
65
86
}
66
87
}
67
- BorsEvent :: InstallationsChanged => {
68
- let span = tracing:: info_span!( "Repository reload" ) ;
69
- if let Err ( error) = state. reload_repositories ( ) . instrument ( span. clone ( ) ) . await {
88
+
89
+ BorsRepositoryEvent :: WorkflowStarted ( payload) => {
90
+ let span = tracing:: info_span!(
91
+ "Workflow started" ,
92
+ repo = payload. repository. to_string( ) ,
93
+ id = payload. run_id. into_inner( )
94
+ ) ;
95
+ if let Err ( error) = handle_workflow_started ( db, payload)
96
+ . instrument ( span. clone ( ) )
97
+ . await
98
+ {
70
99
span. log_error ( error) ;
71
100
}
72
101
}
73
- BorsEvent :: WorkflowStarted ( payload) => {
74
- if let Some ( _) = get_repo_state ( state, & payload. repository ) {
75
- let span = tracing:: info_span!(
76
- "Workflow started" ,
77
- repo = payload. repository. to_string( ) ,
78
- id = payload. run_id. into_inner( )
79
- ) ;
80
- if let Err ( error) = handle_workflow_started ( db, payload)
81
- . instrument ( span. clone ( ) )
82
- . await
83
- {
84
- span. log_error ( error) ;
85
- }
102
+ BorsRepositoryEvent :: WorkflowCompleted ( payload) => {
103
+ let span = tracing:: info_span!(
104
+ "Workflow completed" ,
105
+ repo = payload. repository. to_string( ) ,
106
+ id = payload. run_id. into_inner( )
107
+ ) ;
108
+ if let Err ( error) = handle_workflow_completed ( repo, db, payload)
109
+ . instrument ( span. clone ( ) )
110
+ . await
111
+ {
112
+ span. log_error ( error) ;
86
113
}
87
114
}
88
- BorsEvent :: WorkflowCompleted ( payload) => {
89
- if let Some ( repo) = get_repo_state ( state, & payload. repository ) {
90
- let span = tracing:: info_span!(
91
- "Workflow completed" ,
92
- repo = payload. repository. to_string( ) ,
93
- id = payload. run_id. into_inner( )
94
- ) ;
95
- if let Err ( error) = handle_workflow_completed ( repo, db, payload)
96
- . instrument ( span. clone ( ) )
97
- . await
98
- {
99
- span. log_error ( error) ;
100
- }
115
+ BorsRepositoryEvent :: CheckSuiteCompleted ( payload) => {
116
+ let span = tracing:: info_span!(
117
+ "Check suite completed" ,
118
+ repo = payload. repository. to_string( ) ,
119
+ ) ;
120
+ if let Err ( error) = handle_check_suite_completed ( repo, db, payload)
121
+ . instrument ( span. clone ( ) )
122
+ . await
123
+ {
124
+ span. log_error ( error) ;
101
125
}
102
126
}
103
- BorsEvent :: CheckSuiteCompleted ( payload) => {
104
- if let Some ( repo) = get_repo_state ( state, & payload. repository ) {
105
- let span = tracing:: info_span!(
106
- "Check suite completed" ,
107
- repo = payload. repository. to_string( ) ,
108
- ) ;
109
- if let Err ( error) = handle_check_suite_completed ( repo, db, payload)
110
- . instrument ( span. clone ( ) )
111
- . await
112
- {
113
- span. log_error ( error) ;
114
- }
127
+ }
128
+ Ok ( ( ) )
129
+ }
130
+
131
+ pub async fn handle_bors_global_event < Client : RepositoryClient > (
132
+ event : BorsGlobalEvent ,
133
+ state : Arc < dyn BorsState < Client > > ,
134
+ ctx : Arc < BorsContext > ,
135
+ ) -> anyhow:: Result < ( ) > {
136
+ let db = Arc :: clone ( & ctx. db ) ;
137
+ match event {
138
+ BorsGlobalEvent :: InstallationsChanged => {
139
+ let span = tracing:: info_span!( "Repository reload" ) ;
140
+ if let Err ( error) = state. reload_repositories ( ) . instrument ( span. clone ( ) ) . await {
141
+ span. log_error ( error) ;
115
142
}
116
143
}
117
- BorsEvent :: Refresh => {
144
+ BorsGlobalEvent :: Refresh => {
118
145
let span = tracing:: info_span!( "Refresh" ) ;
119
146
let repos = state. get_all_repos ( ) ;
120
147
futures:: future:: join_all ( repos. into_iter ( ) . map ( |repo| {
0 commit comments