Skip to content

Commit adc93ef

Browse files
committed
Handle custom handlers in a more principled way
1 parent e214442 commit adc93ef

File tree

1 file changed

+82
-126
lines changed

1 file changed

+82
-126
lines changed

src/handlers.rs

Lines changed: 82 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,37 @@ macro_rules! inform {
1515
anyhow::bail!(crate::handlers::UserError($err.into()))
1616
};
1717
}
18+
macro_rules! custom_handlers {
19+
($errors:ident -> $($name:ident: $hd:expr,)*) => {{
20+
// Process the handlers concurrently
21+
let results = futures::join!(
22+
$(
23+
async {
24+
async {
25+
$hd
26+
}
27+
.await
28+
.map_err(|e: anyhow::Error| {
29+
HandlerError::Other(e.context(format!(
30+
"error when processing {} handler",
31+
stringify!($name)
32+
)))
33+
})
34+
}
35+
),*
36+
);
37+
38+
// Destructure the results into named variables
39+
let ($($name,)*) = results;
40+
41+
// Push errors for each handler
42+
$(
43+
if let Err(e) = $name {
44+
$errors.push(e);
45+
}
46+
)*
47+
}}
48+
}
1849

1950
mod assign;
2051
mod autolabel;
@@ -78,132 +109,57 @@ pub async fn handle(ctx: &Context, host: &str, event: &Event) -> Vec<HandlerErro
78109
handle_command(ctx, event, &config, body, &mut errors).await;
79110
}
80111

81-
if let Ok(config) = &config {
82-
if let Err(e) = check_commits::handle(ctx, host, event, &config).await {
83-
log::error!(
84-
"failed to process event {:?} with `check_commits` handler: {:?}",
85-
event,
86-
e
87-
);
88-
}
89-
}
90-
91-
if let Err(e) = project_goals::handle(ctx, event).await {
92-
log::error!(
93-
"failed to process event {:?} with `project_goals` handler: {:?}",
94-
event,
95-
e
96-
);
97-
}
98-
99-
if let Err(e) = notification::handle(ctx, event).await {
100-
log::error!(
101-
"failed to process event {:?} with notification handler: {:?}",
102-
event,
103-
e
104-
);
105-
}
106-
107-
if let Err(e) = rustc_commits::handle(ctx, event).await {
108-
log::error!(
109-
"failed to process event {:?} with rustc_commits handler: {:?}",
110-
event,
111-
e
112-
);
113-
}
114-
115-
if let Err(e) = milestone_prs::handle(ctx, event).await {
116-
log::error!(
117-
"failed to process event {:?} with milestone_prs handler: {:?}",
118-
event,
119-
e
120-
);
121-
}
122-
123-
if let Some(rendered_link_config) = config.as_ref().ok().and_then(|c| c.rendered_link.as_ref())
124-
{
125-
if let Err(e) = rendered_link::handle(ctx, event, rendered_link_config).await {
126-
log::error!(
127-
"failed to process event {:?} with rendered_link handler: {:?}",
128-
event,
129-
e
130-
);
131-
}
132-
}
133-
134-
if let Err(e) = relnotes::handle(ctx, event).await {
135-
log::error!(
136-
"failed to process event {:?} with relnotes handler: {:?}",
137-
event,
138-
e
139-
);
140-
}
141-
142-
if config.as_ref().is_ok_and(|c| c.bot_pull_requests.is_some()) {
143-
if let Err(e) = bot_pull_requests::handle(ctx, event).await {
144-
log::error!(
145-
"failed to process event {:?} with bot_pull_requests handler: {:?}",
146-
event,
147-
e
148-
)
149-
}
150-
}
151-
152-
if let Some(config) = config
153-
.as_ref()
154-
.ok()
155-
.and_then(|c| c.review_submitted.as_ref())
156-
{
157-
if let Err(e) = review_submitted::handle(ctx, event, config).await {
158-
log::error!(
159-
"failed to process event {:?} with review_submitted handler: {:?}",
160-
event,
161-
e
162-
)
163-
}
164-
}
165-
166-
if let Some(config) = config
167-
.as_ref()
168-
.ok()
169-
.and_then(|c| c.review_changes_since.as_ref())
170-
{
171-
if let Err(e) = review_changes_since::handle(ctx, host, event, config).await {
172-
log::error!(
173-
"failed to process event {:?} with review_changes_since handler: {:?}",
174-
event,
175-
e
176-
)
177-
}
178-
}
179-
180-
if let Some(ghr_config) = config
181-
.as_ref()
182-
.ok()
183-
.and_then(|c| c.github_releases.as_ref())
184-
{
185-
if let Err(e) = github_releases::handle(ctx, event, ghr_config).await {
186-
log::error!(
187-
"failed to process event {:?} with github_releases handler: {:?}",
188-
event,
189-
e
190-
);
191-
}
192-
}
193-
194-
if let Some(conflict_config) = config
195-
.as_ref()
196-
.ok()
197-
.and_then(|c| c.merge_conflicts.as_ref())
198-
{
199-
if let Err(e) = merge_conflicts::handle(ctx, event, conflict_config).await {
200-
log::error!(
201-
"failed to process event {:?} with merge_conflicts handler: {:?}",
202-
event,
203-
e
204-
);
205-
}
206-
}
112+
// custom handlers (prefer issue_handlers! for issue event handler)
113+
custom_handlers! { errors ->
114+
project_goals: project_goals::handle(ctx, event).await,
115+
notification: notification::handle(ctx, event).await,
116+
rustc_commits: rustc_commits::handle(ctx, event).await,
117+
milestone_prs: milestone_prs::handle(ctx, event).await,
118+
relnotes: relnotes::handle(ctx, event).await,
119+
check_commits: {
120+
if let Ok(config) = &config {
121+
check_commits::handle(ctx, host, event, &config).await?;
122+
}
123+
Ok(())
124+
},
125+
rendered_link: {
126+
if let Some(rendered_link_config) = config.as_ref().ok().and_then(|c| c.rendered_link.as_ref())
127+
{
128+
rendered_link::handle(ctx, event, rendered_link_config).await?
129+
}
130+
Ok(())
131+
},
132+
bot_pull_requests: {
133+
if config.as_ref().is_ok_and(|c| c.bot_pull_requests.is_some()) {
134+
bot_pull_requests::handle(ctx, event).await?;
135+
}
136+
Ok(())
137+
},
138+
review_submitted: {
139+
if let Some(config) = config.as_ref().ok().and_then(|c| c.review_submitted.as_ref()) {
140+
review_submitted::handle(ctx, event, config).await?;
141+
}
142+
Ok(())
143+
},
144+
review_changes_since: {
145+
if let Some(config) = config.as_ref().ok().and_then(|c| c.review_changes_since.as_ref()) {
146+
review_changes_since::handle(ctx, host, event, config).await?;
147+
}
148+
Ok(())
149+
},
150+
github_releases: {
151+
if let Some(ghr_config) = config.as_ref().ok().and_then(|c| c.github_releases.as_ref()) {
152+
github_releases::handle(ctx, event, ghr_config).await?;
153+
}
154+
Ok(())
155+
},
156+
merge_conflicts: {
157+
if let Some(conflict_config) = config.as_ref().ok().and_then(|c| c.merge_conflicts.as_ref()) {
158+
merge_conflicts::handle(ctx, event, conflict_config).await?;
159+
}
160+
Ok(())
161+
},
162+
};
207163

208164
errors
209165
}

0 commit comments

Comments
 (0)