From 7a217726f12082c2139c94076de4021fcbebec9f Mon Sep 17 00:00:00 2001 From: Urgau Date: Tue, 17 Dec 2024 18:44:36 +0100 Subject: [PATCH] Add proper config for rendered link handler --- src/config.rs | 15 +++++++++++++++ src/handlers.rs | 15 +++++++++------ src/handlers/rendered_link.rs | 33 +++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6a7480ba..77026c33 100644 --- a/src/config.rs +++ b/src/config.rs @@ -47,6 +47,7 @@ pub(crate) struct Config { pub(crate) transfer: Option, pub(crate) merge_conflicts: Option, pub(crate) bot_pull_requests: Option, + pub(crate) rendered_link: Option, } #[derive(PartialEq, Eq, Debug, serde::Deserialize)] @@ -408,6 +409,13 @@ pub(crate) struct MergeConflictConfig { #[serde(deny_unknown_fields)] pub(crate) struct BotPullRequests {} +#[derive(PartialEq, Eq, Debug, serde::Deserialize)] +#[serde(rename_all = "kebab-case")] +#[serde(deny_unknown_fields)] +pub(crate) struct RenderedLinkConfig { + pub(crate) trigger_files: Vec, +} + fn get_cached_config(repo: &str) -> Option, ConfigurationError>> { let cache = CONFIG_CACHE.read().unwrap(); cache.get(repo).and_then(|(config, fetch_time)| { @@ -528,6 +536,9 @@ mod tests { infra = "T-infra" [shortcut] + + [rendered-link] + trigger-files = ["posts/"] "#; let config = toml::from_str::(&config).unwrap(); let mut ping_teams = HashMap::new(); @@ -587,6 +598,9 @@ mod tests { transfer: None, merge_conflicts: None, bot_pull_requests: None, + rendered_link: Some(RenderedLinkConfig { + trigger_files: vec!["posts/".to_string()] + }) } ); } @@ -649,6 +663,7 @@ mod tests { transfer: None, merge_conflicts: None, bot_pull_requests: None, + rendered_link: None, } ); } diff --git a/src/handlers.rs b/src/handlers.rs index 9218a210..cb125dc8 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -102,12 +102,15 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec { ); } - if let Err(e) = rendered_link::handle(ctx, event).await { - log::error!( - "failed to process event {:?} with rendered_link handler: {:?}", - event, - e - ); + if let Some(rendered_link_config) = config.as_ref().ok().and_then(|c| c.rendered_link.as_ref()) + { + if let Err(e) = rendered_link::handle(ctx, event, rendered_link_config).await { + log::error!( + "failed to process event {:?} with rendered_link handler: {:?}", + event, + e + ); + } } if let Err(e) = relnotes::handle(ctx, event).await { diff --git a/src/handlers/rendered_link.rs b/src/handlers/rendered_link.rs index ec79f57f..0a3fd6a3 100644 --- a/src/handlers/rendered_link.rs +++ b/src/handlers/rendered_link.rs @@ -3,14 +3,17 @@ use std::borrow::Cow; use anyhow::bail; use crate::{ + config::RenderedLinkConfig, github::{Event, IssuesAction, IssuesEvent}, handlers::Context, }; -pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> { - let e = if let Event::Issue(e) = event { - e - } else { +pub async fn handle( + ctx: &Context, + event: &Event, + config: &RenderedLinkConfig, +) -> anyhow::Result<()> { + let Event::Issue(e) = event else { return Ok(()); }; @@ -18,21 +21,18 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> { return Ok(()); } - let repo = e.issue.repository(); - let prefix = match (&*repo.organization, &*repo.repository) { - ("rust-lang", "rfcs") => "text/", - ("rust-lang", "blog.rust-lang.org") => "posts/", - _ => return Ok(()), - }; - - if let Err(e) = add_rendered_link(&ctx, &e, prefix).await { + if let Err(e) = add_rendered_link(&ctx, &e, config).await { tracing::error!("Error adding rendered link: {:?}", e); } Ok(()) } -async fn add_rendered_link(ctx: &Context, e: &IssuesEvent, prefix: &str) -> anyhow::Result<()> { +async fn add_rendered_link( + ctx: &Context, + e: &IssuesEvent, + config: &RenderedLinkConfig, +) -> anyhow::Result<()> { if e.action == IssuesAction::Opened || e.action == IssuesAction::Closed || e.action == IssuesAction::Reopened @@ -42,7 +42,12 @@ async fn add_rendered_link(ctx: &Context, e: &IssuesEvent, prefix: &str) -> anyh let rendered_link = files .iter() - .find(|f| f.filename.starts_with(prefix)) + .find(|f| { + config + .trigger_files + .iter() + .any(|tf| f.filename.starts_with(tf)) + }) .map(|file| { let head = e.issue.head.as_ref().unwrap(); let base = e.issue.base.as_ref().unwrap();