Skip to content

Commit 08f095d

Browse files
refactor: use let else
1 parent 98fe9aa commit 08f095d

File tree

1 file changed

+68
-65
lines changed

1 file changed

+68
-65
lines changed

src/bors/handlers/mod.rs

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -48,77 +48,80 @@ pub async fn handle_bors_repository_event<Client: RepositoryClient>(
4848
ctx: Arc<BorsContext>,
4949
) -> anyhow::Result<()> {
5050
let db = Arc::clone(&ctx.db);
51-
if let Some(repo) = get_repo_state(state, &event.repository()) {
52-
match event {
53-
BorsRepositoryEvent::Comment(comment) => {
54-
// We want to ignore comments made by this bot
55-
if repo.client.is_comment_internal(&comment).await? {
56-
tracing::trace!(
57-
"Ignoring comment {comment:?} because it was authored by this bot"
58-
);
59-
return Ok(());
60-
}
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+
};
6157

62-
let span = tracing::info_span!(
63-
"Comment",
64-
pr = format!("{}#{}", comment.repository, comment.pr_number),
65-
author = comment.author.username
66-
);
67-
let pr_number = comment.pr_number;
68-
if let Err(error) = handle_comment(Arc::clone(&repo), db, ctx, comment)
69-
.instrument(span.clone())
70-
.await
71-
{
72-
span.log_error(error);
73-
repo.client
74-
.post_comment(
75-
pr_number,
76-
Comment::new(
77-
":x: Encountered an error while executing command".to_string(),
78-
),
79-
)
80-
.await
81-
.context("Cannot send comment reacting to an error")?;
82-
}
58+
match event {
59+
BorsRepositoryEvent::Comment(comment) => {
60+
// We want to ignore comments made by this bot
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(());
8364
}
8465

85-
BorsRepositoryEvent::WorkflowStarted(payload) => {
86-
let span = tracing::info_span!(
87-
"Workflow started",
88-
repo = payload.repository.to_string(),
89-
id = payload.run_id.into_inner()
90-
);
91-
if let Err(error) = handle_workflow_started(db, payload)
92-
.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+
)
9384
.await
94-
{
95-
span.log_error(error);
96-
}
85+
.context("Cannot send comment reacting to an error")?;
9786
}
98-
BorsRepositoryEvent::WorkflowCompleted(payload) => {
99-
let span = tracing::info_span!(
100-
"Workflow completed",
101-
repo = payload.repository.to_string(),
102-
id = payload.run_id.into_inner()
103-
);
104-
if let Err(error) = handle_workflow_completed(repo, db, payload)
105-
.instrument(span.clone())
106-
.await
107-
{
108-
span.log_error(error);
109-
}
87+
}
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+
{
99+
span.log_error(error);
110100
}
111-
BorsRepositoryEvent::CheckSuiteCompleted(payload) => {
112-
let span = tracing::info_span!(
113-
"Check suite completed",
114-
repo = payload.repository.to_string(),
115-
);
116-
if let Err(error) = handle_check_suite_completed(repo, db, payload)
117-
.instrument(span.clone())
118-
.await
119-
{
120-
span.log_error(error);
121-
}
101+
}
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);
113+
}
114+
}
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);
122125
}
123126
}
124127
}

0 commit comments

Comments
 (0)