Skip to content

Commit

Permalink
Add proper config for rendered link handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Dec 17, 2024
1 parent 0400514 commit 7a21772
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
15 changes: 15 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(crate) struct Config {
pub(crate) transfer: Option<TransferConfig>,
pub(crate) merge_conflicts: Option<MergeConflictConfig>,
pub(crate) bot_pull_requests: Option<BotPullRequests>,
pub(crate) rendered_link: Option<RenderedLinkConfig>,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
Expand Down Expand Up @@ -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<String>,
}

fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationError>> {
let cache = CONFIG_CACHE.read().unwrap();
cache.get(repo).and_then(|(config, fetch_time)| {
Expand Down Expand Up @@ -528,6 +536,9 @@ mod tests {
infra = "T-infra"
[shortcut]
[rendered-link]
trigger-files = ["posts/"]
"#;
let config = toml::from_str::<Config>(&config).unwrap();
let mut ping_teams = HashMap::new();
Expand Down Expand Up @@ -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()]
})
}
);
}
Expand Down Expand Up @@ -649,6 +663,7 @@ mod tests {
transfer: None,
merge_conflicts: None,
bot_pull_requests: None,
rendered_link: None,
}
);
}
Expand Down
15 changes: 9 additions & 6 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
);
}

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 {
Expand Down
33 changes: 19 additions & 14 deletions src/handlers/rendered_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@ 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(());
};

if !e.issue.is_pr() {
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
Expand All @@ -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();
Expand Down

0 comments on commit 7a21772

Please sign in to comment.