Skip to content

Commit 16c927e

Browse files
authored
Merge pull request #2140 from Urgau/axum-follow-up
Follow-up to the Axum migration
2 parents 1f38e6b + 9b7c1d3 commit 16c927e

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/github/webhook.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ pub async fn webhook(
172172
return (StatusCode::BAD_REQUEST, "Payload must be UTF-8").into_response();
173173
};
174174

175-
tracing::info!(?event, ?payload);
176-
177175
match process_payload(event, payload, &ctx).await {
178176
Ok(true) => ("processed request",).into_response(),
179177
Ok(false) => ("ignored request",).into_response(),

src/interactions.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@ impl<'a> ErrorComment<'a> {
2424
}
2525
}
2626

27-
pub async fn post(&self, client: &GithubClient) -> anyhow::Result<Comment> {
27+
pub fn markdown(message: &str) -> anyhow::Result<String> {
2828
let mut body = String::new();
29-
writeln!(body, "**Error**: {}", self.message)?;
29+
writeln!(body, "**Error**: {message}")?;
3030
writeln!(body)?;
3131
writeln!(
3232
body,
3333
"Please file an issue on GitHub at [triagebot](https://github.com/rust-lang/triagebot) if there's \
3434
a problem with this bot, or reach out on [#t-infra](https://rust-lang.zulipchat.com/#narrow/stream/242791-t-infra) on Zulip."
3535
)?;
36+
Ok(body)
37+
}
38+
39+
pub async fn post(&self, client: &GithubClient) -> anyhow::Result<Comment> {
40+
let body = Self::markdown(&self.message)?;
3641
self.issue.post_comment(client, &body).await
3742
}
3843
}

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::Context as _;
44
use axum::body::Body;
55
use axum::error_handling::HandleErrorLayer;
66
use axum::http::HeaderName;
7-
use axum::response::Html;
7+
use axum::response::{Html, Response};
88
use axum::routing::{get, post};
99
use axum::{BoxError, Router};
1010
use hyper::{Request, StatusCode};
@@ -132,6 +132,9 @@ async fn run_server(addr: SocketAddr) -> anyhow::Result<()> {
132132
})
133133
.on_request(|request: &Request<Body>, _span: &tracing::Span| {
134134
tracing::info!(?request);
135+
})
136+
.on_response(|response: &Response<_>, dur, _span: &tracing::Span| {
137+
tracing::info!("response={} in {dur:?}", response.status());
135138
}),
136139
)
137140
.layer(PropagateRequestIdLayer::new(X_REQUEST_ID))

src/zulip.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::handlers::Context;
1212
use crate::handlers::docs_update::docs_update;
1313
use crate::handlers::pr_tracking::get_assigned_prs;
1414
use crate::handlers::project_goals::{self, ping_project_goals_owners};
15+
use crate::interactions::ErrorComment;
1516
use crate::utils::pluralize;
1617
use crate::zulip::api::{MessageApiResponse, Recipient};
1718
use crate::zulip::client::ZulipClient;
@@ -21,6 +22,7 @@ use crate::zulip::commands::{
2122
use anyhow::{Context as _, format_err};
2223
use axum::Json;
2324
use axum::extract::State;
25+
use axum::extract::rejection::JsonRejection;
2426
use axum::response::IntoResponse;
2527
use rust_team_data::v1::{TeamKind, TeamMember};
2628
use std::cmp::Reverse;
@@ -92,11 +94,24 @@ struct Response {
9294
/// Top-level handler for Zulip webhooks.
9395
///
9496
/// Returns a JSON response or a 400 with an error message.
95-
// TODO: log JsonRejection
9697
pub async fn webhook(
9798
State(ctx): State<Arc<Context>>,
98-
Json(req): Json<Request>,
99+
req: Result<Json<Request>, JsonRejection>,
99100
) -> axum::response::Response {
101+
let Json(req) = match req {
102+
Ok(req) => req,
103+
Err(rejection) => {
104+
tracing::error!(?rejection);
105+
return Json(Response {
106+
content: ErrorComment::markdown(
107+
"unable to handle this Zulip request: invalid JSON input",
108+
)
109+
.expect("creating a error message without fail"),
110+
})
111+
.into_response();
112+
}
113+
};
114+
100115
tracing::info!(?req);
101116
let response = process_zulip_request(ctx, req).await;
102117
tracing::info!(?response);
@@ -111,9 +126,6 @@ pub async fn webhook(
111126
// We are mixing network errors and "logic" error (like clap errors)
112127
// so don't return a 500. Long term we should decouple those.
113128

114-
// Log the full error
115-
tracing::error!(?err);
116-
117129
// Reply with a 200 and reply only with outermost error
118130
Json(Response {
119131
content: err.to_string(),
@@ -150,8 +162,6 @@ async fn process_zulip_request(ctx: Arc<Context>, req: Request) -> anyhow::Resul
150162
anyhow::bail!("Invalid authorization.");
151163
}
152164

153-
log::trace!("zulip hook: {req:?}");
154-
155165
// Zulip commands are only available to users in the team database
156166
let gh_id = match ctx.team.zulip_to_github_id(req.message.sender_id).await {
157167
Ok(Some(gh_id)) => gh_id,

0 commit comments

Comments
 (0)