From ef9c6d4f93f5d047af2ff35a374f665a0c196d36 Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Tue, 17 Nov 2026 10:35:37 +0100 Subject: [PATCH] debian/changelog: collect LP: #NNN references from entry bodies dpkg's Ubuntu vendor module collects 'LP: #NNN' changelog trailers into the .changes 'Launchpad-Bugs-Fixed' field, which Launchpad uses to link uploads to the bugs they fix. Port the same parsing (case-insensitive 'lp:' prefix with a mandatory space, '#'-prefixed comma-separated numbers) and rendering (numeric sort, de-duplication, space-joined) so pkh-built uploads can carry the same field. --- src/debian/changelog.rs | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/debian/changelog.rs b/src/debian/changelog.rs index 4480aed..9165b80 100644 --- a/src/debian/changelog.rs +++ b/src/debian/changelog.rs @@ -34,6 +34,11 @@ pub struct ChangelogEntry { /// Bug numbers collected from `(Closes: #NNN)` mentions in the body, /// sorted numerically and de-duplicated (like dpkg's `find_closes`). pub closes: Option, + /// Launchpad bug numbers collected from `LP: #NNN` mentions in the + /// body, sorted numerically and de-duplicated: a port of dpkg's Ubuntu + /// vendor module (`Dpkg::Vendor::Ubuntu::find_launchpad_closes`), + /// feeding the `.changes` `Launchpad-Bugs-Fixed` field. + pub lp_bugs: Option, } /// Parse up to `limit` entries of a Debian changelog file, newest first @@ -213,6 +218,7 @@ fn parse_one_entry( } let closes = find_closes(&body_lines); + let lp_bugs = find_lp_bugs(&body_lines); Ok(ChangelogEntry { source, @@ -226,6 +232,7 @@ fn parse_one_entry( timestamp, changes_field, closes, + lp_bugs, }) } @@ -260,6 +267,41 @@ fn find_closes(body_lines: &[String]) -> Option { ) } +/// Extract Launchpad bug numbers from `LP: #NNN` mentions in changelog +/// body lines, sorted numerically and de-duplicated: a port of dpkg's +/// Ubuntu vendor module (`Dpkg::Vendor::Ubuntu::find_launchpad_closes`), +/// which fills the `.changes` `Launchpad-Bugs-Fixed` field. Same `lp:` +/// prefix (case-insensitive, at least one space), same `#`-prefixed +/// comma-separated list, same space-joined rendering. +fn find_lp_bugs(body_lines: &[String]) -> Option { + let re = Regex::new(r"(?i)lp:\s+#\d+(?:,\s*#\d+)*").ok()?; + let number = Regex::new(r"#?\s?(\d+)").ok()?; + let mut numbers: Vec = Vec::new(); + for line in body_lines { + for statement in re.find_iter(line) { + for capture in number.captures_iter(statement.as_str()) { + if let Some(n) = capture.get(1) + && let Ok(parsed) = n.as_str().parse::() + { + numbers.push(parsed); + } + } + } + } + if numbers.is_empty() { + return None; + } + numbers.sort_unstable(); + numbers.dedup(); + Some( + numbers + .iter() + .map(u64::to_string) + .collect::>() + .join(" "), + ) +} + /// Heuristic check for a changelog entry header line /// (`name (version) dist; urgency=...`). fn looks_like_header(line: &str) -> bool { @@ -280,6 +322,42 @@ fn looks_like_header(line: &str) -> bool { mod tests { use super::*; + /// Parse one entry from a changelog text with this body. + fn entry_with_body(body: &str) -> ChangelogEntry { + parse_changelog_entries_from_str( + &format!( + "pkg (1.0-1) unstable; urgency=medium\n\n{body}\n \ + -- A B Thu, 01 Jan 2026 00:00:00 +0000\n" + ), + Some(1), + ) + .unwrap() + .remove(0) + } + + #[test] + fn launchpad_bug_references() { + // Single reference + let entry = entry_with_body(" * Fix crash (LP: #123456)"); + assert_eq!(entry.lp_bugs.as_deref(), Some("123456")); + + // Comma-separated list, sorted numerically like dpkg renders them + let entry = entry_with_body(" * Fix crash (LP: #123456, #99, #45)"); + assert_eq!(entry.lp_bugs.as_deref(), Some("45 99 123456")); + + // De-duplicated across several statements and lines + let entry = entry_with_body(" * Fix A (LP: #5)\n * Fix B (lp: #5, #6)"); + assert_eq!(entry.lp_bugs.as_deref(), Some("5 6")); + + // Without the space dpkg's Ubuntu parser requires: no match + let entry = entry_with_body(" * Fix crash (LP:#123456)"); + assert_eq!(entry.lp_bugs, None); + + // Plain bug mentions are not Launchpad references + let entry = entry_with_body(" * Fix crash (#123456)\n * Closes: #42"); + assert_eq!(entry.lp_bugs, None); + } + #[test] fn changelog_parsing() { let dir = tempfile::tempdir().unwrap();