use forge_ci::config::{Event, ReleaseNotes, ReleaseSpec}; use forge_core::ci::{CiJob, CiRun, RunState}; use forge_core::releases::{MAX_BODY_CHARS, MAX_TITLE_CHARS, Release}; use forge_core::{Repo, User}; use forge_git::browse::CommitInfo; use forge_git::history::log; use forge_git::pulls::{is_ancestor, range_log}; use forge_git::repo::repo_path; use forge_git::tags::previous_tag; use tokio::io::AsyncWriteExt; use super::api::fit_to_cap; use super::{artifact_key, log_path, schedule, status_creator}; use crate::release_ops::{self, CreateRelease, ReleaseError}; use crate::render::commit_message::CommitMessage; use crate::state::AppState; const MAX_NOTE_COMMITS: usize = 200; const MIB: u64 = 1024 * 1024; pub async fn on_run_finished(state: &AppState, run: &CiRun) { if run.state != RunState::Success { return; } let Some(tag) = run.git_ref.strip_prefix("refs/tags/") else { return; }; let repo = match state.store.repo_by_id(&run.repo_id).await { Ok(Some(repo)) => repo, Ok(None) => return, Err(e) => { tracing::error!(error = %e, run_id = %run.id, "release: loading the repo failed"); return; } }; let Some(config) = schedule::config_at(state, &repo.id, &run.commit_oid).await else { return; }; let Some(spec) = config .release .as_ref() .filter(|_| config.wants_release(&Event::Tag { tag })) else { return; }; let text = publish(state, &repo, run, tag, spec).await; let jobs = match state.store.jobs_for_run(&run.id).await { Ok(jobs) => jobs, Err(e) => { tracing::error!(error = %e, run_id = %run.id, "release: listing the jobs failed"); return; } }; if let Some(job) = jobs.iter().max_by(|a, b| a.finished_at.cmp(&b.finished_at)) { note(state, job, &text).await; } } async fn publish( state: &AppState, repo: &Repo, run: &CiRun, tag: &str, spec: &ReleaseSpec, ) -> String { let Some(actor) = actor_of(state, repo, run).await else { return format!("Could not publish release {tag}: no user to publish as"); }; let release = match release_for(state, repo, run, tag, spec, &actor).await { Ok(release) => release, Err(reason) => return format!("Could not publish release {tag}: {reason}"), }; let jobs = match state.store.jobs_for_run(&run.id).await { Ok(jobs) => jobs, Err(e) => { tracing::error!(error = %e, run_id = %run.id, "release: listing the jobs failed"); return format!("Could not publish release {tag}: could not list the jobs"); } }; let max_bytes = state.live().releases.max_asset_mb.saturating_mul(MIB); let mut attached = 0usize; let mut failed: Vec = Vec::new(); for job in &jobs { let artifacts = match state.store.list_artifacts(&job.id).await { Ok(artifacts) => artifacts, Err(e) => { tracing::error!(error = %e, job_id = %job.id, "release: listing artifacts failed"); failed.push(format!("{}: could not list the artifacts", job.name)); continue; } }; for artifact in artifacts .iter() .filter(|a| spec.asset_matches(&job.name, &a.name)) { match attach( state, repo, &release, &actor, &artifact.id, &artifact.name, max_bytes, ) .await { Ok(()) => attached += 1, Err(reason) => { tracing::error!(run_id = %run.id, tag, artifact = %artifact.name, %reason, "release: attaching an asset failed"); failed.push(format!("{}: {reason}", artifact.name)); } } } } let mut text = format!("Published release {tag} with {attached} assets."); if !failed.is_empty() { text.push_str(&format!(" {} failed: {}.", failed.len(), failed.join(", "))); } text } async fn actor_of(state: &AppState, repo: &Repo, run: &CiRun) -> Option { let id = status_creator(state, repo, &run.actor_id).await?; match state.store.user_by_id(&id).await { Ok(user) => user, Err(e) => { tracing::error!(error = %e, run_id = %run.id, "release: loading the actor failed"); None } } } async fn release_for( state: &AppState, repo: &Repo, run: &CiRun, tag: &str, spec: &ReleaseSpec, actor: &User, ) -> Result { match state.store.release_by_tag(&repo.id, tag).await { Ok(Some(existing)) if existing.target_oid == run.commit_oid => return Ok(existing), Ok(Some(existing)) => { return Ok(follow_tag(state, repo, run, tag, spec, actor, existing).await); } Ok(None) => {} Err(e) => { tracing::error!(error = %e, run_id = %run.id, "release: reading the release failed"); return Err("could not read the release".to_owned()); } } let input = CreateRelease { tag: tag.to_owned(), target: Some(run.commit_oid.clone()), title: spec.title_for(tag).chars().take(MAX_TITLE_CHARS).collect(), body: notes(state, repo, run, tag, spec).await, draft: spec.draft, prerelease: spec.is_prerelease(tag), }; match release_ops::create(state, repo, actor, input).await { Ok(release) => Ok(release), Err(ReleaseError::Conflict(_)) => match state.store.release_by_tag(&repo.id, tag).await { Ok(Some(existing)) => Ok(existing), _ => Err("a release for this tag could not be made".to_owned()), }, Err(e) => { tracing::error!(error = ?e, run_id = %run.id, tag, "release: creating the release failed"); Err(e.message()) } } } async fn follow_tag( state: &AppState, repo: &Repo, run: &CiRun, tag: &str, spec: &ReleaseSpec, actor: &User, existing: Release, ) -> Release { let generated = !matches!(spec.notes, ReleaseNotes::Off) && changelog(state, repo, &existing.target_oid, tag) .await .as_deref() == Ok(existing.body.as_str()); let body = if generated { Some(notes(state, repo, run, tag, spec).await) } else { None }; match release_ops::follow_moved_tag(state, repo, actor, &existing, &run.commit_oid, body).await { Ok(Some(moved)) => moved, Ok(None) => existing, Err(e) => { tracing::error!(error = ?e, run_id = %run.id, tag, "release: moving the target failed"); existing } } } async fn attach( state: &AppState, repo: &Repo, release: &Release, actor: &User, artifact_id: &str, name: &str, max_bytes: u64, ) -> Result<(), String> { let read = match state.blobs.read(&artifact_key(artifact_id)).await { Ok(Some(read)) => read, Ok(None) => return Err("the file is missing from storage".to_owned()), Err(e) => { tracing::error!(error = %e, artifact_id, "release: reading an artifact failed"); return Err("the file could not be read".to_owned()); } }; release_ops::put_asset( state, repo, release, actor, name, None, read.stream, max_bytes, ) .await .map(|_| ()) .map_err(|e| { tracing::error!(error = ?e, artifact_id, "release: storing an asset failed"); e.message() }) } async fn notes( state: &AppState, repo: &Repo, run: &CiRun, tag: &str, spec: &ReleaseSpec, ) -> String { if matches!(spec.notes, ReleaseNotes::Off) { return String::new(); } match changelog(state, repo, &run.commit_oid, tag).await { Ok(text) => text, Err(e) => { tracing::error!(error = %e, run_id = %run.id, tag, "release: building the notes failed"); String::new() } } } async fn changelog( state: &AppState, repo: &Repo, commit: &str, tag: &str, ) -> Result { let dir = repo_path(&state.git.data_dir, &repo.id).map_err(|e| e.to_string())?; let env = &state.git; let releases = state .store .list_releases(&repo.id) .await .map_err(|e| e.to_string())?; let mut previous: Option<(String, String)> = None; for release in releases.iter().filter(|r| !r.draft && r.tag != tag) { if is_ancestor(env, &dir, &release.target_oid, commit) .await .unwrap_or(false) { previous = Some((release.tag.clone(), release.target_oid.clone())); break; } } if previous.is_none() { previous = previous_tag(env, &dir, commit) .await .map_err(|e| e.to_string())?; } let commits = match &previous { Some((_, base)) => { let mut commits = range_log(env, &dir, base, commit, MAX_NOTE_COMMITS) .await .map_err(|e| e.to_string())?; commits.reverse(); commits } None => log(env, &dir, commit, None, 0, MAX_NOTE_COMMITS) .await .map_err(|e| e.to_string())?, }; let footer = previous .as_ref() .map(|(name, _)| format!("Full changelog: {name}...{tag}")); Ok(bullets(&commits, footer.as_deref())) } const SECTIONS: [&str; 7] = [ "Features", "Fixes", "Performance", "Style", "CI", "Documentation", "Other", ]; fn section_of(kind: &str) -> usize { match kind { "feat" => 0, "fix" => 1, "perf" => 2, "style" | "ui" | "ux" => 3, "ci" => 4, "docs" => 5, _ => 6, } } fn bullets(commits: &[CommitInfo], footer: Option<&str>) -> String { let footer_len = footer.map_or(0, |f| f.chars().count() + 2); let structured = commits .iter() .any(|commit| CommitMessage::parse(&commit.summary).is_some()); let body = if structured { grouped_bullets(commits, footer_len) } else { flat_bullets(commits, footer_len) }; with_footer(&body, footer) } fn flat_bullets(commits: &[CommitInfo], footer_len: usize) -> String { let mut body = String::new(); let mut used = 0usize; for commit in commits { let short = commit.oid.get(..7).unwrap_or(&commit.oid); let line = format!("- {} ({short})\n", commit.summary); let len = line.chars().count(); if used + len + footer_len > MAX_BODY_CHARS { break; } used += len; body.push_str(&line); } body } fn grouped_bullets(commits: &[CommitInfo], footer_len: usize) -> String { let mut sections: Vec> = vec![Vec::new(); SECTIONS.len()]; let mut used = 0usize; 'commits: for commit in commits { let short = commit.oid.get(..7).unwrap_or(&commit.oid); let clauses: Vec<(usize, String)> = match CommitMessage::parse(&commit.summary) { Some(message) => message .groups .iter() .flat_map(|group| { let section = section_of(&group.kind); group .clauses .iter() .map(move |clause| (section, clause.clone())) }) .collect(), None => vec![(section_of(""), commit.summary.clone())], }; for (section, text) in clauses { let line = format!("- {text} ({short})\n"); let mut len = line.chars().count(); if sections[section].is_empty() { len += SECTIONS[section].chars().count() + 6; } if used + len + footer_len > MAX_BODY_CHARS { break 'commits; } used += len; sections[section].push(line); } } let mut body = String::new(); for (name, lines) in SECTIONS.iter().zip(§ions) { if lines.is_empty() { continue; } if !body.is_empty() { body.push('\n'); } body.push_str(&format!("### {name}\n")); for line in lines { body.push_str(line); } } body } fn with_footer(body: &str, footer: Option<&str>) -> String { let mut body = body.trim_end().to_owned(); if let Some(footer) = footer { if !body.is_empty() { body.push_str("\n\n"); } body.push_str(footer); } body } async fn note(state: &AppState, job: &CiJob, text: &str) { let line = format!("[forge] {text}\n"); let cap = state.live().ci.max_log_mb.saturating_mul(MIB); let path = log_path(&state.config.data_dir, &job.id); let current = match tokio::fs::metadata(&path).await { Ok(meta) => meta.len(), Err(e) if e.kind() == std::io::ErrorKind::NotFound => 0, Err(e) => { tracing::error!(error = %e, job_id = %job.id, "release: reading the job log failed"); return; } }; let bytes = fit_to_cap(current, cap, line.as_bytes()); if bytes.is_empty() { return; } let written: std::io::Result<()> = async { if let Some(parent) = path.parent() { tokio::fs::create_dir_all(parent).await?; } let mut file = tokio::fs::OpenOptions::new() .create(true) .append(true) .open(&path) .await?; file.write_all(&bytes).await?; file.flush().await } .await; if let Err(e) = written { tracing::error!(error = %e, job_id = %job.id, "release: writing the job log failed"); return; } if let Err(e) = state.store.add_log_bytes(&job.id, bytes.len() as i64).await { tracing::error!(error = %e, job_id = %job.id, "release: counting the job log bytes failed"); return; } state.ci_live.bump_job(&job.id); } #[cfg(test)] mod tests { use forge_core::ci::{CiArtifact, JobState}; use super::*; use crate::ci::schedule::CONFIG_PATH; use crate::ci::schedule::tests::{Fixture, ZERO, fixture, git, land, push}; use crate::post_update::after_ref_update; const RELEASE: &str = r#" [on] push = ["main"] tags = ["v*"] [jobs.build] image = "alpine:3" run = "make" artifacts = ["dist/*"] [jobs.docs] image = "alpine:3" run = "make docs" artifacts = ["site/*"] [release] assets = ["build:dist_*"] title = "forge {tag}" "#; async fn finish_all(fx: &Fixture, run_id: &str, failing: Option<&str>) { loop { let jobs = fx.state.store.jobs_for_run(run_id).await.expect("jobs"); let Some(job) = jobs.iter().find(|j| !j.state.is_terminal()) else { break; }; let fails = failing == Some(job.name.as_str()); let (outcome, code) = if fails { (JobState::Failure, 1) } else { (JobState::Success, 0) }; fx.state .store .finish_job(&job.id, outcome, Some(code), None) .await .expect("finish"); } } async fn run_of(fx: &Fixture, number: i64) -> CiRun { fx.state .store .run_by_number(&fx.repo.id, number) .await .expect("query") .expect("run") } async fn job_of(fx: &Fixture, run: &CiRun, name: &str) -> CiJob { fx.state .store .jobs_for_run(&run.id) .await .expect("jobs") .into_iter() .find(|j| j.name == name) .expect("job") } async fn add_artifact(fx: &Fixture, job: &CiJob, name: &str, body: &[u8]) -> CiArtifact { let tmp = format!("ci/tmp/{}", ulid::Ulid::generate()); let mut writer = fx.state.blobs.begin(&tmp, 1 << 30).await.expect("begin"); writer.push(body).await.expect("push"); let (size, sha) = writer.finish().await.expect("finish"); let artifact = fx .state .store .add_artifact(&job.id, name, size as i64, &sha) .await .expect("artifact"); fx.state .blobs .rename(&tmp, &artifact_key(&artifact.id)) .await .expect("rename"); artifact } fn commit_message(work: &std::path::Path, message: &str) -> String { git(work, &["commit", "--allow-empty", "-m", message]); git(work, &["rev-parse", "HEAD"]) } async fn release_named(fx: &Fixture, tag: &str) -> Option { fx.state .store .release_by_tag(&fx.repo.id, tag) .await .expect("query") } async fn asset_names(fx: &Fixture, release: &Release) -> Vec<(String, u64)> { let mut assets: Vec<(String, u64)> = fx .state .store .list_release_assets(&release.id) .await .expect("assets") .into_iter() .map(|a| (a.name, a.size)) .collect(); assets.sort(); assets } #[tokio::test] async fn tag_run_publishes_release_with_matching_assets() { let fx = fixture().await; push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; let run = run_of(&fx, 1).await; let build = job_of(&fx, &run, "build").await; let docs = job_of(&fx, &run, "docs").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await; add_artifact(&fx, &build, "dist_SHA256SUMS", b"sums").await; add_artifact(&fx, &docs, "dist_docs-only", b"x").await; add_artifact(&fx, &docs, "site_index.html", b"").await; finish_all(&fx, &run.id, None).await; let run = run_of(&fx, 1).await; assert_eq!(run.state, RunState::Success); on_run_finished(&fx.state, &run).await; let release = release_named(&fx, "v1.0").await.expect("release made"); assert_eq!(release.title, "forge v1.0"); assert_eq!(release.target_oid, run.commit_oid); assert!(!release.draft); assert!(!release.prerelease); assert_eq!(release.author_id, fx.owner.id); assert_eq!( asset_names(&fx, &release).await, vec![ ("dist_SHA256SUMS".to_owned(), 4), ("dist_forge-linux-x86_64".to_owned(), 6), ], "only build's dist_* artifacts are attached" ); let mut log_text = String::new(); for job in fx.state.store.jobs_for_run(&run.id).await.expect("jobs") { let path = crate::ci::log_path(&fx.state.config.data_dir, &job.id); if let Ok(text) = std::fs::read_to_string(path) { log_text.push_str(&text); } } assert!(log_text.contains("[forge] Published release v1.0 with 2 assets.")); } #[tokio::test] async fn notes_list_commits_since_the_previous_release() { let fx = fixture().await; push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; finish_all(&fx, &run_of(&fx, 1).await.id, None).await; on_run_finished(&fx.state, &run_of(&fx, 1).await).await; commit_message(&fx.work, "feat: one"); let oid = commit_message(&fx.work, "fix: two"); land(&fx, &oid, "refs/tags/v1.1"); after_ref_update( &fx.state, &fx.repo, &fx.owner.id, "refs/tags/v1.1", ZERO, &oid, ) .await .expect("post update"); finish_all(&fx, &run_of(&fx, 2).await.id, None).await; on_run_finished(&fx.state, &run_of(&fx, 2).await).await; let release = release_named(&fx, "v1.1").await.expect("release made"); let short = &oid[..7]; assert!(release.body.starts_with("### Features\n- one (")); assert!( release .body .contains(&format!("### Fixes\n- two ({short})")) ); assert!(release.body.ends_with("Full changelog: v1.0...v1.1")); assert!( !release.body.contains("- change ("), "older commits are not repeated" ); } #[tokio::test] async fn moved_tag_moves_the_release_and_its_generated_notes() { let fx = fixture().await; let first = push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; finish_all(&fx, &run_of(&fx, 1).await.id, None).await; on_run_finished(&fx.state, &run_of(&fx, 1).await).await; let before = release_named(&fx, "v1.0").await.expect("release made"); assert_eq!(before.target_oid, first); let oid = commit_message(&fx.work, "fix: later"); land(&fx, &oid, "refs/tags/v1.0"); after_ref_update( &fx.state, &fx.repo, &fx.owner.id, "refs/tags/v1.0", &first, &oid, ) .await .expect("post update"); finish_all(&fx, &run_of(&fx, 2).await.id, None).await; on_run_finished(&fx.state, &run_of(&fx, 2).await).await; let after = release_named(&fx, "v1.0").await.expect("release kept"); assert_eq!(after.id, before.id); assert_eq!(after.target_oid, oid); assert!( after.body.contains("later"), "notes follow the tag: {}", after.body ); } #[tokio::test] async fn existing_manual_release_keeps_its_fields_and_gets_assets() { let fx = fixture().await; push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; let run = run_of(&fx, 1).await; let manual = release_ops::create( &fx.state, &fx.repo, &fx.owner, CreateRelease { tag: "v1.0".to_owned(), target: None, title: "Hand made".to_owned(), body: "Keep me".to_owned(), draft: true, prerelease: true, }, ) .await .expect("manual release"); let build = job_of(&fx, &run, "build").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await; finish_all(&fx, &run.id, None).await; on_run_finished(&fx.state, &run_of(&fx, 1).await).await; let release = release_named(&fx, "v1.0").await.expect("release"); assert_eq!(release.id, manual.id); assert_eq!(release.title, "Hand made"); assert_eq!(release.body, "Keep me"); assert!(release.draft); assert!(release.prerelease); assert_eq!(asset_names(&fx, &release).await.len(), 1); } #[tokio::test] async fn failed_runs_create_no_release() { let fx = fixture().await; push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; let run = run_of(&fx, 1).await; let build = job_of(&fx, &run, "build").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await; finish_all(&fx, &run.id, Some("docs")).await; let run = run_of(&fx, 1).await; assert_eq!(run.state, RunState::Failure, "in order"); on_run_finished(&fx.state, &run).await; assert!(release_named(&fx, "v1.0").await.is_none(), "in order"); push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v2.0").await; let run = run_of(&fx, 2).await; let build = job_of(&fx, &run, "build").await; let docs = job_of(&fx, &run, "docs").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await; let after_build = fx .state .store .finish_job(&build.id, JobState::Success, Some(0), None) .await .expect("finish build"); assert_eq!(after_build.state, RunState::Running, "docs is still queued"); on_run_finished(&fx.state, &after_build).await; assert!(release_named(&fx, "v2.0").await.is_none(), "parallel"); let after_docs = fx .state .store .finish_job(&docs.id, JobState::Failure, Some(1), None) .await .expect("finish docs"); assert_eq!(after_docs.state, RunState::Failure, "parallel"); on_run_finished(&fx.state, &after_docs).await; assert!(release_named(&fx, "v2.0").await.is_none(), "parallel"); push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v3.0").await; let run = run_of(&fx, 3).await; let build = job_of(&fx, &run, "build").await; let docs = job_of(&fx, &run, "docs").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await; let after_docs = fx .state .store .finish_job(&docs.id, JobState::Failure, Some(1), None) .await .expect("finish docs"); assert_eq!(after_docs.state, RunState::Failure, "fail fast"); assert_eq!(job_of(&fx, &run, "build").await.state, JobState::Cancelled); on_run_finished(&fx.state, &after_docs).await; assert!(release_named(&fx, "v3.0").await.is_none(), "fail fast"); } #[tokio::test] async fn branch_push_run_creates_no_release() { let fx = fixture().await; push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/heads/main").await; let run = run_of(&fx, 1).await; finish_all(&fx, &run.id, None).await; on_run_finished(&fx.state, &run_of(&fx, 1).await).await; assert!(release_named(&fx, "v1.0").await.is_none()); assert!(release_named(&fx, "main").await.is_none()); } #[tokio::test] async fn rerun_replaces_assets_by_name() { let fx = fixture().await; push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; let first = run_of(&fx, 1).await; let build = job_of(&fx, &first, "build").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"one").await; finish_all(&fx, &first.id, None).await; on_run_finished(&fx.state, &run_of(&fx, 1).await).await; let second = fx .state .store .rerun(&first.id, &fx.owner.id) .await .expect("rerun"); let build = job_of(&fx, &second, "build").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"second!").await; finish_all(&fx, &second.id, None).await; on_run_finished(&fx.state, &run_of(&fx, 2).await).await; let release = release_named(&fx, "v1.0").await.expect("release"); assert_eq!( asset_names(&fx, &release).await, vec![("dist_forge-linux-x86_64".to_owned(), 7)] ); } #[tokio::test] async fn notes_fall_back_to_the_previous_git_tag() { let fx = fixture().await; push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; commit_message(&fx.work, "feat: one"); let oid = commit_message(&fx.work, "fix: two"); land(&fx, &oid, "refs/tags/v1.1"); after_ref_update( &fx.state, &fx.repo, &fx.owner.id, "refs/tags/v1.1", ZERO, &oid, ) .await .expect("post update"); finish_all(&fx, &run_of(&fx, 2).await.id, None).await; on_run_finished(&fx.state, &run_of(&fx, 2).await).await; let release = release_named(&fx, "v1.1").await.expect("release made"); assert!(release.body.starts_with("### Features\n- one (")); assert!(release.body.contains("### Fixes\n- two (")); assert!(release.body.ends_with("Full changelog: v1.0...v1.1")); assert!(release_named(&fx, "v1.0").await.is_none()); } #[test] fn bullets_put_the_footer_last_and_respect_the_limit() { let commit = |oid: &str, summary: &str| CommitInfo { oid: oid.to_owned(), parents: Vec::new(), author_name: String::new(), author_email: String::new(), author_time: 0, committer_name: String::new(), committer_email: String::new(), committer_time: 0, summary: summary.to_owned(), message: String::new(), }; let commits = vec![ commit("abcdef1234567", "new"), commit("1234567abcdef", "old"), ]; assert_eq!( bullets(&commits, Some("Full changelog: a...b")), "- new (abcdef1)\n- old (1234567)\n\nFull changelog: a...b" ); assert_eq!(bullets(&[], None), ""); } fn info(oid: &str, summary: &str) -> CommitInfo { CommitInfo { oid: oid.to_owned(), parents: Vec::new(), author_name: String::new(), author_email: String::new(), author_time: 0, committer_name: String::new(), committer_email: String::new(), committer_time: 0, summary: summary.to_owned(), message: String::new(), } } #[test] fn bullets_group_structured_commits_in_section_order() { let commits = vec![ info("aaaaaaa1111", "🐛 fix: b; ✨ feat: a"), info("bbbbbbb2222", "Update the readme"), info("ccccccc3333", "✨ feat: c"), ]; assert_eq!( bullets(&commits, None), "### Features\n- a (aaaaaaa)\n- c (ccccccc)\n\n### Fixes\n- b (aaaaaaa)\n\n### Other\n- Update the readme (bbbbbbb)" ); } #[test] fn bullets_count_headings_and_footer_in_the_limit() { let long = "x".repeat(1000); let commits: Vec = (0..200) .map(|i| info(&format!("{i:07}abcdef"), &format!("✨ feat: {long}{i}"))) .collect(); let footer = "Full changelog: a...b"; let body = bullets(&commits, Some(footer)); assert!(body.chars().count() <= MAX_BODY_CHARS); assert!(body.starts_with("### Features\n- ")); assert!(body.ends_with(footer)); } }