releasaurus-core 0.16.0

A comprehensive release automation tool that streamlines the software release process across multiple programming languages and forge platforms
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
use std::{
    collections::{HashMap, HashSet},
    path::Path,
    rc::Rc,
};

use crate::{
    analyzer::release::Tag,
    error::Result,
    forge::{manager::ForgeManager, request::ForgeCommit},
    orchestrator::{
        config::OrchestratorConfig,
        package::resolved::{ResolvedPackage, ResolvedPackageHash},
    },
};

pub struct CommitsCore {
    orchestrator_config: Rc<OrchestratorConfig>,
    forge: Rc<ForgeManager>,
    package_configs: Rc<ResolvedPackageHash>,
}

impl CommitsCore {
    pub fn new(
        orchestrator_config: Rc<OrchestratorConfig>,
        forge: Rc<ForgeManager>,
        package_configs: Rc<ResolvedPackageHash>,
    ) -> Self {
        Self {
            orchestrator_config,
            forge,
            package_configs,
        }
    }

    /// Retrieves all commits for all packages along with the latest tag for
    /// each package. Uses the oldest tag across all packages as a shared
    /// starting point when possible, avoiding redundant per-package fetches.
    /// Returns `(commits, tags)` so callers can reuse the tags rather than
    /// re-querying the forge.
    pub async fn get_commits_for_all_packages(
        &self,
        target: Option<&str>,
    ) -> Result<(Vec<ForgeCommit>, HashMap<String, Option<Tag>>)> {
        log::info!("attempting to get commits for all packages at once");

        let tags = self.collect_tags_for_packages(target).await?;
        let oldest_sha = self.oldest_tag_sha_from_map(&tags);

        let commits = if let Some(sha) = oldest_sha {
            log::info!("found starting sha: {:#?}", sha);
            self.forge
                .get_commits(
                    Some(self.orchestrator_config.base_branch.clone()),
                    Some(sha),
                )
                .await?
        } else {
            log::warn!(
                "falling back to getting commits for each package separately"
            );
            self.get_commits_for_packages_with_tags(&tags).await?
        };

        Ok((commits, tags))
    }

    /// Filters list of commit to just the commits pertaining to a specific package
    pub fn filter_commits_for_package(
        &self,
        package: &ResolvedPackage,
        tag: Option<&Tag>,
        commits: &[ForgeCommit],
    ) -> Vec<ForgeCommit> {
        let mut package_paths = vec![package.normalized_full_path.clone()];
        package_paths.extend(package.normalized_additional_paths.clone());

        let mut package_commits: Vec<ForgeCommit> = vec![];

        for commit in commits.iter() {
            if let Some(tag) = tag
                && let Some(tag_timestamp) = tag.timestamp
                && commit.timestamp < tag_timestamp
            {
                // commit is older than package's previous release starting point
                continue;
            }
            'file_loop: for file in commit.files.iter() {
                let file_path = Path::new(file);
                for package_path in package_paths.iter() {
                    if file_path.starts_with(package_path) {
                        let raw_message = commit.message.to_string();
                        let split_msg = raw_message
                            .split_once("\n")
                            .map(|(m, b)| (m.to_string(), b.to_string()));

                        let (title, _body) = match split_msg {
                            Some((t, b)) => {
                                if b.is_empty() {
                                    (t.trim().to_string(), None)
                                } else {
                                    (
                                        t.trim().to_string(),
                                        Some(b.trim().to_string()),
                                    )
                                }
                            }
                            None => (raw_message.to_string(), None),
                        };

                        log::debug!(
                            "{}: including commit for analysis : {} : {}",
                            package.name,
                            commit.short_id,
                            title
                        );

                        package_commits.push(commit.clone());
                        break 'file_loop;
                    }
                }
            }
        }

        package_commits
    }

    /// Collects the latest tag for every (target-filtered) package in a
    /// single pass, returning a map keyed by package name.
    async fn collect_tags_for_packages(
        &self,
        target: Option<&str>,
    ) -> Result<HashMap<String, Option<Tag>>> {
        let mut tags = HashMap::new();
        for (name, package) in self.package_configs.hash().iter() {
            if let Some(target) = target
                && name != target
            {
                continue;
            }
            let tag = self
                .forge
                .get_latest_tag_for_prefix(
                    &package.tag_prefix,
                    &self.orchestrator_config.base_branch,
                )
                .await?;
            tags.insert(name.clone(), tag);
        }
        Ok(tags)
    }

    /// Fetches commits per-package using pre-fetched tags, deduplicating via
    /// a HashSet. Used when a unified starting point cannot be determined
    /// (i.e. any package has no tag yet).
    async fn get_commits_for_packages_with_tags(
        &self,
        tags: &HashMap<String, Option<Tag>>,
    ) -> Result<Vec<ForgeCommit>> {
        let mut cache: HashSet<ForgeCommit> = HashSet::new();

        for (name, tag) in tags.iter() {
            let current_sha = tag.as_ref().map(|t| t.sha.clone());

            log::info!(
                "{name}: current tag sha: {:?} : fetching commits",
                current_sha
            );

            let commits = self
                .forge
                .get_commits(
                    Some(self.orchestrator_config.base_branch.clone()),
                    current_sha,
                )
                .await?;

            cache.extend(commits);
        }

        let mut commits = cache.iter().cloned().collect::<Vec<ForgeCommit>>();
        commits.sort_by(|c1, c2| c1.timestamp.cmp(&c2.timestamp));
        Ok(commits)
    }

    /// Returns the SHA of the oldest tag across all packages, or `None` if
    /// any package has no tag (meaning a shared starting point cannot be
    /// determined).
    fn oldest_tag_sha_from_map(
        &self,
        tags: &HashMap<String, Option<Tag>>,
    ) -> Option<String> {
        if tags.values().any(|t| t.is_none()) {
            log::warn!("found package that hasn't been tagged yet");
            return None;
        }

        let mut oldest_timestamp = i64::MAX;
        let mut oldest_sha = None;

        for tag in tags.values().flatten() {
            if let Some(ts) = tag.timestamp
                && ts < oldest_timestamp
            {
                oldest_timestamp = ts;
                oldest_sha = Some(tag.sha.clone());
            }
        }

        oldest_sha
    }
}

#[cfg(test)]
mod tests {
    use url::Url;

    use super::*;
    use crate::{
        analyzer::release::Tag,
        config::{
            Config, package::PackageConfigBuilder, release_type::ReleaseType,
        },
        forge::{
            manager::{ForgeManager, ForgeOptions},
            request::ForgeCommitBuilder,
            traits::MockForge,
        },
        orchestrator::config::{CommitModifiers, GlobalOverrides},
    };
    use std::path::PathBuf;

    fn create_test_package(name: &str, path: &str) -> ResolvedPackage {
        let config = Rc::new(Config::default());
        let pkg_config = PackageConfigBuilder::default()
            .name(name)
            .path(path)
            .release_type(ReleaseType::Node)
            .build()
            .unwrap();

        let orchestrator_config = Rc::new(
            OrchestratorConfig::builder()
                .toml_config(config)
                .repo_name("test-repo")
                .repo_default_branch("main")
                .release_link_base_url(
                    Url::parse("https://example.com/").unwrap(),
                )
                .compare_link_base_url(
                    Url::parse("https://example.com/compare/").unwrap(),
                )
                .package_overrides(std::collections::HashMap::new())
                .global_overrides(GlobalOverrides::default())
                .commit_modifiers(CommitModifiers::default())
                .build()
                .unwrap(),
        );

        ResolvedPackage::builder()
            .orchestrator_config(orchestrator_config)
            .package_config(pkg_config)
            .build()
            .unwrap()
    }

    fn create_test_commits_core() -> CommitsCore {
        let config = Rc::new(Config::default());
        let orchestrator_config = Rc::new(
            OrchestratorConfig::builder()
                .toml_config(config)
                .repo_name("test-repo")
                .repo_default_branch("main")
                .release_link_base_url(
                    Url::parse("https://example.com/").unwrap(),
                )
                .compare_link_base_url(
                    Url::parse("https://example.com/compare/").unwrap(),
                )
                .package_overrides(std::collections::HashMap::new())
                .global_overrides(GlobalOverrides::default())
                .commit_modifiers(CommitModifiers::default())
                .build()
                .unwrap(),
        );

        let forge = Rc::new(ForgeManager::new(
            Box::new(MockForge::new()),
            ForgeOptions { dry_run: false },
        ));

        let package_configs =
            Rc::new(ResolvedPackageHash::new(vec![]).unwrap());

        CommitsCore::new(orchestrator_config, forge, package_configs)
    }

    #[test]
    fn filters_commits_by_package_path() {
        let commits = vec![
            ForgeCommitBuilder::default()
                .id("commit1")
                .short_id("c1")
                .message("feat: add feature to pkg-a")
                .timestamp(1000)
                .files(vec!["packages/pkg-a/src/main.rs".to_string()])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("commit2")
                .short_id("c2")
                .message("fix: bug in pkg-b")
                .timestamp(2000)
                .files(vec!["packages/pkg-b/src/lib.rs".to_string()])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("commit3")
                .short_id("c3")
                .message("docs: update pkg-a readme")
                .timestamp(3000)
                .files(vec!["packages/pkg-a/README.md".to_string()])
                .build()
                .unwrap(),
        ];

        let package = create_test_package("pkg-a", "packages/pkg-a");
        let core = create_test_commits_core();

        let filtered =
            core.filter_commits_for_package(&package, None, &commits);

        // Should only include commits that touched packages/pkg-a
        assert_eq!(filtered.len(), 2);
        assert_eq!(filtered[0].id, "commit1");
        assert_eq!(filtered[1].id, "commit3");
    }

    #[test]
    fn filters_commits_by_timestamp_when_tag_provided() {
        let commits = vec![
            ForgeCommitBuilder::default()
                .id("old-commit")
                .short_id("old")
                .message("feat: old feature")
                .timestamp(1000)
                .files(vec!["packages/pkg-a/src/old.rs".to_string()])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("new-commit")
                .short_id("new")
                .message("feat: new feature")
                .timestamp(3000)
                .files(vec!["packages/pkg-a/src/new.rs".to_string()])
                .build()
                .unwrap(),
        ];

        let package = create_test_package("pkg-a", "packages/pkg-a");
        let tag = Tag {
            name: "v1.0.0".to_string(),
            timestamp: Some(2000),
            ..Default::default()
        };

        let core = create_test_commits_core();

        let filtered =
            core.filter_commits_for_package(&package, Some(&tag), &commits);

        // Should only include commits newer than tag timestamp
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].id, "new-commit");
    }

    #[test]
    fn includes_commit_when_any_file_matches_package_path() {
        let commits = vec![
            ForgeCommitBuilder::default()
                .id("multi-file-commit")
                .short_id("mfc")
                .message("feat: touch multiple packages")
                .timestamp(1000)
                .files(vec![
                    "packages/pkg-b/src/lib.rs".to_string(),
                    "packages/pkg-a/src/main.rs".to_string(),
                    "packages/pkg-c/README.md".to_string(),
                ])
                .build()
                .unwrap(),
        ];

        let package = create_test_package("pkg-a", "packages/pkg-a");
        let core = create_test_commits_core();

        let filtered =
            core.filter_commits_for_package(&package, None, &commits);

        // Should include the commit since one of its files matches
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].id, "multi-file-commit");
    }

    #[test]
    fn returns_empty_when_no_commits_match_package() {
        let commits = vec![
            ForgeCommitBuilder::default()
                .id("commit1")
                .short_id("c1")
                .message("feat: work on pkg-b")
                .timestamp(1000)
                .files(vec!["packages/pkg-b/src/main.rs".to_string()])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("commit2")
                .short_id("c2")
                .message("feat: work on pkg-c")
                .timestamp(2000)
                .files(vec!["packages/pkg-c/src/lib.rs".to_string()])
                .build()
                .unwrap(),
        ];

        let package = create_test_package("pkg-a", "packages/pkg-a");
        let core = create_test_commits_core();

        let filtered =
            core.filter_commits_for_package(&package, None, &commits);

        assert_eq!(filtered.len(), 0);
    }

    #[test]
    fn handles_root_level_package() {
        let commits = vec![
            ForgeCommitBuilder::default()
                .id("root-commit")
                .short_id("rc")
                .message("feat: root level change")
                .timestamp(1000)
                .files(vec!["src/main.rs".to_string()])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("nested-commit")
                .short_id("nc")
                .message("feat: nested change")
                .timestamp(2000)
                .files(vec!["packages/nested/src/lib.rs".to_string()])
                .build()
                .unwrap(),
        ];

        let package = create_test_package("root-pkg", ".");
        let core = create_test_commits_core();

        let filtered =
            core.filter_commits_for_package(&package, None, &commits);

        // Root package should match all commits
        assert_eq!(filtered.len(), 2);
    }

    #[test]
    fn includes_commits_from_additional_paths() {
        let commits = vec![
            ForgeCommitBuilder::default()
                .id("main-path-commit")
                .short_id("mpc")
                .message("feat: change in main path")
                .timestamp(1000)
                .files(vec!["packages/pkg-a/src/main.rs".to_string()])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("additional-path-commit")
                .short_id("apc")
                .message("feat: change in additional path")
                .timestamp(2000)
                .files(vec!["shared/common/utils.rs".to_string()])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("unrelated-commit")
                .short_id("uc")
                .message("feat: unrelated change")
                .timestamp(3000)
                .files(vec!["packages/pkg-b/src/lib.rs".to_string()])
                .build()
                .unwrap(),
        ];

        let mut package = create_test_package("pkg-a", "packages/pkg-a");
        // Add additional paths to the package
        package.normalized_additional_paths =
            vec![PathBuf::from("shared/common"), PathBuf::from("docs")];

        let core = create_test_commits_core();

        let filtered =
            core.filter_commits_for_package(&package, None, &commits);

        // Should include commits from both main path and additional paths
        assert_eq!(filtered.len(), 2);
        assert_eq!(filtered[0].id, "main-path-commit");
        assert_eq!(filtered[1].id, "additional-path-commit");
    }

    #[tokio::test]
    async fn get_commits_uses_oldest_tag_when_all_packages_tagged() {
        let config = Rc::new(Config::default());
        let orchestrator_config = Rc::new(
            OrchestratorConfig::builder()
                .toml_config(config.clone())
                .repo_name("test-repo")
                .repo_default_branch("main")
                .release_link_base_url(
                    Url::parse("https://example.com/").unwrap(),
                )
                .compare_link_base_url(
                    Url::parse("https://example.com/compare/").unwrap(),
                )
                .package_overrides(std::collections::HashMap::new())
                .global_overrides(GlobalOverrides::default())
                .commit_modifiers(CommitModifiers::default())
                .build()
                .unwrap(),
        );

        let mut mock_forge = MockForge::new();

        // One tag fetch per package (collected once, not re-fetched later)
        mock_forge
            .expect_get_latest_tag_for_prefix()
            .times(2)
            .returning(|prefix, _branch| {
                if prefix.contains("pkg-a") {
                    // pkg-a has newer tag (timestamp 2000)
                    Ok(Some(Tag {
                        sha: "newer-sha".to_string(),
                        timestamp: Some(2000),
                        ..Default::default()
                    }))
                } else {
                    // pkg-b has older tag (timestamp 1000)
                    Ok(Some(Tag {
                        sha: "older-sha".to_string(),
                        timestamp: Some(1000),
                        ..Default::default()
                    }))
                }
            });

        // Should use the older SHA
        mock_forge
            .expect_get_commits()
            .times(1)
            .withf(|branch, sha| {
                branch.as_ref().unwrap() == "main"
                    && sha.as_ref().unwrap() == "older-sha"
            })
            .returning(|_, _| Ok(vec![]));

        let forge = Rc::new(ForgeManager::new(
            Box::new(mock_forge),
            ForgeOptions { dry_run: false },
        ));

        // Create two packages
        let pkg_a_config = PackageConfigBuilder::default()
            .name("pkg-a")
            .path("packages/pkg-a")
            .release_type(ReleaseType::Node)
            .build()
            .unwrap();

        let pkg_b_config = PackageConfigBuilder::default()
            .name("pkg-b")
            .path("packages/pkg-b")
            .release_type(ReleaseType::Node)
            .build()
            .unwrap();

        let pkg_a = ResolvedPackage::builder()
            .orchestrator_config(Rc::clone(&orchestrator_config))
            .package_config(pkg_a_config)
            .build()
            .unwrap();

        let pkg_b = ResolvedPackage::builder()
            .orchestrator_config(Rc::clone(&orchestrator_config))
            .package_config(pkg_b_config)
            .build()
            .unwrap();

        let package_configs =
            Rc::new(ResolvedPackageHash::new(vec![pkg_a, pkg_b]).unwrap());

        let commits_core = CommitsCore::new(
            Rc::clone(&orchestrator_config),
            forge,
            package_configs,
        );

        let (commits, tags) = commits_core
            .get_commits_for_all_packages(None)
            .await
            .unwrap();
        assert_eq!(commits.len(), 0);
        assert_eq!(tags.len(), 2);
    }

    #[tokio::test]
    async fn get_commits_falls_back_when_package_has_no_tag() {
        let config = Rc::new(Config::default());
        let orchestrator_config = Rc::new(
            OrchestratorConfig::builder()
                .toml_config(config.clone())
                .repo_name("test-repo")
                .repo_default_branch("main")
                .release_link_base_url(
                    Url::parse("https://example.com/").unwrap(),
                )
                .compare_link_base_url(
                    Url::parse("https://example.com/compare/").unwrap(),
                )
                .package_overrides(std::collections::HashMap::new())
                .global_overrides(GlobalOverrides::default())
                .commit_modifiers(CommitModifiers::default())
                .build()
                .unwrap(),
        );

        let mut mock_forge = MockForge::new();

        // Tags are collected once in a single pass (2 calls total).
        // The fallback fetch reuses the already-collected tags.
        mock_forge
            .expect_get_latest_tag_for_prefix()
            .times(2)
            .returning(|prefix, _branch| {
                if prefix.contains("pkg-a") {
                    Ok(Some(Tag {
                        sha: "some-sha".to_string(),
                        timestamp: Some(1000),
                        ..Default::default()
                    }))
                } else {
                    // pkg-b has no tag yet
                    Ok(None)
                }
            });

        // Should fall back to getting commits per package (2 calls)
        mock_forge
            .expect_get_commits()
            .times(2)
            .returning(|_, _| Ok(vec![]));

        let forge = Rc::new(ForgeManager::new(
            Box::new(mock_forge),
            ForgeOptions { dry_run: false },
        ));

        let pkg_a_config = PackageConfigBuilder::default()
            .name("pkg-a")
            .path("packages/pkg-a")
            .release_type(ReleaseType::Node)
            .build()
            .unwrap();

        let pkg_b_config = PackageConfigBuilder::default()
            .name("pkg-b")
            .path("packages/pkg-b")
            .release_type(ReleaseType::Node)
            .build()
            .unwrap();

        let pkg_a = ResolvedPackage::builder()
            .orchestrator_config(Rc::clone(&orchestrator_config))
            .package_config(pkg_a_config)
            .build()
            .unwrap();

        let pkg_b = ResolvedPackage::builder()
            .orchestrator_config(Rc::clone(&orchestrator_config))
            .package_config(pkg_b_config)
            .build()
            .unwrap();

        let package_configs =
            Rc::new(ResolvedPackageHash::new(vec![pkg_a, pkg_b]).unwrap());

        let commits_core = CommitsCore::new(
            Rc::clone(&orchestrator_config),
            forge,
            package_configs,
        );

        let (commits, tags) = commits_core
            .get_commits_for_all_packages(None)
            .await
            .unwrap();
        assert_eq!(commits.len(), 0);
        assert_eq!(tags.len(), 2);
    }
}