1use anyhow::{Context, Result};
2use minijinja::{Environment, context};
3use serde::Serialize;
4
5const RELEASE_TEMPLATE: &str = include_str!("../templates/changelog.jinja");
7const DEPENDENCY_TEMPLATE: &str = include_str!("../templates/dependency-changelog.jinja");
9
10#[derive(Debug, Clone, Serialize)]
12pub struct PullRequest {
13 pub number: u64,
15 pub url: String,
17 pub user: Option<String>,
19 pub user_url: Option<String>,
21 pub external_author: bool,
24}
25
26#[derive(Debug, Clone, Serialize)]
28pub struct Commit {
29 pub short: String,
31 pub url: String,
33}
34
35#[derive(Debug, Clone, Serialize)]
37pub struct Dependency {
38 pub name: String,
40 pub new_version: String,
42}
43
44#[derive(Debug, Clone)]
46pub struct ReleaseInput {
47 pub summary: String,
49 pub continuations: Vec<String>,
52 pub pull_request: Option<PullRequest>,
54 pub commit: Option<Commit>,
57}
58
59impl ReleaseInput {
60 #[must_use]
63 pub fn with_author_filter(mut self, internal_authors: &[String]) -> Self {
64 if let Some(pr) = &mut self.pull_request {
65 let login = pr.user.as_deref().unwrap_or("");
66 pr.external_author =
67 !login.is_empty() && !internal_authors.iter().any(|name| name == login);
68 }
69 self
70 }
71}
72
73#[must_use]
75pub fn summary_has_terminal(summary: &str) -> bool {
76 summary
77 .trim_end()
78 .chars()
79 .next_back()
80 .is_some_and(|ch| matches!(ch, '.' | '!' | '?'))
81}
82
83#[must_use]
89pub fn summary_body(summary: &str) -> &str {
90 let summary = summary.trim_end();
91 if summary.ends_with("..") {
92 return summary;
93 }
94 summary.strip_suffix('.').unwrap_or(summary)
95}
96
97pub fn render_release(input: &ReleaseInput) -> Result<String> {
104 render_release_template(RELEASE_TEMPLATE, input)
105}
106
107pub fn render_release_template(template: &str, input: &ReleaseInput) -> Result<String> {
114 let mut env = Environment::new();
115 env.add_template("changelog", template)
116 .context("parse changelog template")?;
117 let tmpl = env.get_template("changelog")?;
118 let rendered = tmpl
119 .render(context! {
120 summary => input.summary,
121 continuations => input.continuations,
122 pull_request => input.pull_request,
123 commit => input.commit,
124 summary_body => summary_body(&input.summary),
125 summary_has_terminal => summary_has_terminal(&input.summary),
126 })
127 .context("render changelog")?;
128 Ok(rendered)
129}
130
131pub fn render_dependencies(dependencies: &[Dependency]) -> Result<String> {
138 let mut env = Environment::new();
139 env.add_template("deps", DEPENDENCY_TEMPLATE)
140 .context("parse dependency template")?;
141 let tmpl = env.get_template("deps")?;
142 tmpl.render(context! { dependencies => dependencies })
143 .context("render dependency changelog")
144}