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,27 +185,37 @@ async fn run() -> anyhow::Result<()> {
let event_tx = event_tx.clone(); let event_tx = event_tx.clone();
async move { async move {
let project = event.project(); match event {
let config_key = project.web_url.replace("http://", "").replace("https://", ""); GitlabEvent::Other => {
if let Some(repo_config) = config.repo_configs.get(&config_key) { warp::reply::with_status("Unsupported Gitlab event type", StatusCode::BAD_REQUEST)
if !constant_time_eq(token.as_bytes(), repo_config.token.as_bytes()) { }
warn!("Invalid token for repo '{}'", config_key); _ => {
warp::reply::with_status("Invalid token", StatusCode::FORBIDDEN) let project = event.project();
} else { let config_key = project.web_url.replace("http://", "").replace("https://", "");
debug!("payload: {:?}", event); if let Some(repo_config) = config.repo_configs.get(&config_key) {
if let Some(room) = repo_config.room.as_ref().or(config.default_room.as_ref()) { if !constant_time_eq(token.as_bytes(), repo_config.token.as_bytes()) {
if let Err(err) = event_tx.send((event, room.clone())).await { warn!("Invalid token for repo '{}'", config_key);
warn!("Failed to enqueue payload: {}", err); warp::reply::with_status("Invalid token", StatusCode::FORBIDDEN)
} else {
debug!("payload: {:?}", event);
if let Some(room) = repo_config.room.as_ref().or(config.default_room.as_ref()) {
if let Err(err) = event_tx.send((event, room.clone())).await {
warn!("Failed to enqueue payload: {}", err);
}
warp::reply::with_status("OK", StatusCode::OK)
} else {
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("OK", StatusCode::OK)
} else { } else {
info!("Channel not configured for repo '{}'", config_key); info!("Repo '{}' unconfigured", config_key);
warp::reply::with_status("Matrix room not configured for repo", StatusCode::NOT_FOUND) warp::reply::with_status("Repo not configured", StatusCode::NOT_FOUND)
} }
} }
} else {
info!("Repo '{}' unconfigured", config_key);
warp::reply::with_status("Repo not configured", StatusCode::NOT_FOUND)
} }
} }
}); });