Return reasonable response if unsupported event type

This commit is contained in:
Brian Tarricone 2023-09-16 20:55:57 -07:00
parent 59d4123321
commit 8e72bedcf1
2 changed files with 33 additions and 17 deletions

View File

@ -202,6 +202,8 @@ pub enum GitlabEvent {
user: User, user: User,
project: Project, project: Project,
}, },
#[serde(other)]
Other,
} }
impl GitlabEventExt for GitlabEvent { impl GitlabEventExt for GitlabEvent {
@ -212,6 +214,7 @@ impl GitlabEventExt for GitlabEvent {
GitlabEvent::Issue { project, .. } => project, GitlabEvent::Issue { project, .. } => project,
GitlabEvent::MergeRequest { project, .. } => &project, GitlabEvent::MergeRequest { project, .. } => &project,
GitlabEvent::Pipeline { project, .. } => &project, GitlabEvent::Pipeline { project, .. } => &project,
GitlabEvent::Other => unreachable!("Unsupported event type"),
} }
} }
@ -222,6 +225,7 @@ impl GitlabEventExt for GitlabEvent {
GitlabEvent::Issue { .. } => None, GitlabEvent::Issue { .. } => None,
GitlabEvent::MergeRequest { object_attributes, .. } => Some(&object_attributes.target_branch), GitlabEvent::MergeRequest { object_attributes, .. } => Some(&object_attributes.target_branch),
GitlabEvent::Pipeline { object_attributes, .. } => Some(&object_attributes.r#ref), GitlabEvent::Pipeline { object_attributes, .. } => Some(&object_attributes.r#ref),
GitlabEvent::Other => unreachable!("Unsupported event type"),
} }
} }
@ -232,6 +236,7 @@ impl GitlabEventExt for GitlabEvent {
GitlabEvent::Issue { user, .. } => &user.name, GitlabEvent::Issue { user, .. } => &user.name,
GitlabEvent::MergeRequest { user, .. } => &user.name, GitlabEvent::MergeRequest { user, .. } => &user.name,
GitlabEvent::Pipeline { user, .. } => &user.name, GitlabEvent::Pipeline { user, .. } => &user.name,
GitlabEvent::Other => unreachable!("Unsupported event type"),
} }
} }
@ -329,6 +334,7 @@ impl GitlabEventExt for GitlabEvent {
vec![] vec![]
} }
} }
GitlabEvent::Other => unreachable!("Unsupported event type"),
} }
} }
} }

View File

@ -185,6 +185,11 @@ async fn run() -> anyhow::Result<()> {
let event_tx = event_tx.clone(); let event_tx = event_tx.clone();
async move { async move {
match event {
GitlabEvent::Other => {
warp::reply::with_status("Unsupported Gitlab event type", StatusCode::BAD_REQUEST)
}
_ => {
let project = event.project(); let project = event.project();
let config_key = project.web_url.replace("http://", "").replace("https://", ""); let config_key = project.web_url.replace("http://", "").replace("https://", "");
if let Some(repo_config) = config.repo_configs.get(&config_key) { if let Some(repo_config) = config.repo_configs.get(&config_key) {
@ -200,7 +205,10 @@ async fn run() -> anyhow::Result<()> {
warp::reply::with_status("OK", StatusCode::OK) warp::reply::with_status("OK", StatusCode::OK)
} else { } else {
info!("Channel not configured for repo '{}'", config_key); info!("Channel not configured for repo '{}'", config_key);
warp::reply::with_status("Matrix room not configured for repo", StatusCode::NOT_FOUND) warp::reply::with_status(
"Matrix room not configured for repo",
StatusCode::NOT_FOUND,
)
} }
} }
} else { } else {
@ -208,6 +216,8 @@ async fn run() -> anyhow::Result<()> {
warp::reply::with_status("Repo not configured", StatusCode::NOT_FOUND) warp::reply::with_status("Repo not configured", StatusCode::NOT_FOUND)
} }
} }
}
}
}); });
let routes = gitlab.with(warp::log("bebot")); let routes = gitlab.with(warp::log("bebot"));