releasaurus-core 0.20.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
use regex::Regex;
use semver::{Prerelease, Version};
use std::{borrow::Cow, sync::LazyLock};

use crate::{
    analyzer::{
        commit::Commit, config::AnalyzerConfig, group::GroupParser,
        release::Release,
    },
    config::prerelease::PrereleaseStrategy,
    forge::request::ForgeCommit,
    result::Result,
};

/// Matches 3 or more consecutive new lines
static EXTRA_NEW_LINES_REGEX: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\n{3,}").unwrap());

/// Add a parsed commit to the release and update the release SHA and
/// timestamp to reflect the latest commit.
pub fn update_release_with_commit(
    group_parser: &GroupParser,
    release: &mut Release,
    forge_commit: &ForgeCommit,
    config: &AnalyzerConfig,
) {
    // create git_cliff commit from git2 commit
    if let Some(commit) =
        Commit::parse_forge_commit(group_parser, forge_commit, config)
    {
        let commit_id = commit.id.to_string();

        log::info!(
            "processing commit: {} : {}",
            commit.short_id,
            commit.raw_title
        );

        // add commit to release
        release.commits.push(commit);
        // set release commit - this will keep getting updated until we
        // get to the last commit in the release, which will be a tag
        release.sha = commit_id;
        release.timestamp = forge_commit.timestamp;
    }
}

/// Normalize changelog formatting by replacing consecutive blank lines (3+)
/// with double newlines and trimming whitespace.
pub fn strip_extra_lines(changelog: &str) -> String {
    EXTRA_NEW_LINES_REGEX
        .replace_all(changelog, "\n\n")
        .trim()
        .to_string()
}

/// Adds a prerelease identifier to a stable version (e.g., "1.1.0" with "alpha" -> "1.1.0-alpha.1").
pub fn add_prerelease(
    mut version: Version,
    identifier: &str,
    strategy: PrereleaseStrategy,
) -> Result<Version> {
    // Use Cow to avoid allocation for Static strategy
    let pre_str: Cow<str> = if matches!(strategy, PrereleaseStrategy::Versioned)
    {
        Cow::Owned(format!("{}.1", identifier))
    } else {
        Cow::Borrowed(identifier)
    };
    version.pre = Prerelease::new(&pre_str)?;
    Ok(version)
}

/// Removes prerelease identifiers from a version (e.g., "1.0.0-alpha.5" -> "1.0.0").
pub fn graduate_prerelease(version: &Version) -> Version {
    let mut new_version = version.clone();
    new_version.pre = Prerelease::EMPTY;
    new_version
}

#[cfg(test)]
mod tests {
    use crate::forge::request::ForgeCommitBuilder;

    use super::*;

    #[test]
    fn test_update_release_with_commit() {
        let analyzer_config = AnalyzerConfig::default();
        let group_parser = GroupParser::default();
        let mut release = Release::default();

        let forge_commit1 = ForgeCommitBuilder::default()
            .id("commit1")
            .short_id("comm1")
            .link("https://example.com/commit/commit1")
            .author_name("Author 1")
            .author_email("author1@example.com")
            .merge_commit(false)
            .message("fix: first commit")
            .timestamp(1640995200)
            .files(vec![])
            .build()
            .unwrap();

        let forge_commit2 = ForgeCommitBuilder::default()
            .id("commit2")
            .short_id("comm2")
            .link("https://example.com/commit/commit2")
            .author_name("Author 2")
            .author_email("author2@example.com")
            .merge_commit(true)
            .message("feat: second commit")
            .timestamp(1640995300)
            .files(vec![])
            .build()
            .unwrap();

        update_release_with_commit(
            &group_parser,
            &mut release,
            &forge_commit1,
            &analyzer_config,
        );
        update_release_with_commit(
            &group_parser,
            &mut release,
            &forge_commit2,
            &analyzer_config,
        );

        // Should have 1 commit (merge commit 2 is filtered by default skip_merge_commits: true)
        assert_eq!(release.commits.len(), 1);

        // SHA should be from the last commit
        assert_eq!(release.sha, "commit1");

        // Timestamp should be from the last commit
        assert_eq!(release.timestamp, 1640995200);
    }

    #[test]
    fn test_strip_extra_lines_removes_triple_newlines() {
        let input = "Line 1\n\n\nLine 2";
        let expected = "Line 1\n\nLine 2";
        let result = strip_extra_lines(input);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_strip_extra_lines_removes_many_newlines() {
        let input = "Line 1\n\n\n\n\n\n\nLine 2";
        let expected = "Line 1\n\nLine 2";
        let result = strip_extra_lines(input);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_strip_extra_lines_preserves_single_newlines() {
        let input = "Line 1\nLine 2\nLine 3";
        let expected = "Line 1\nLine 2\nLine 3";
        let result = strip_extra_lines(input);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_strip_extra_lines_preserves_double_newlines() {
        let input = "Line 1\n\nLine 2\n\nLine 3";
        let expected = "Line 1\n\nLine 2\n\nLine 3";
        let result = strip_extra_lines(input);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_strip_extra_lines_handles_empty_string() {
        let input = "";
        let expected = "";
        let result = strip_extra_lines(input);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_strip_extra_lines_handles_only_newlines() {
        let input = "\n\n\n\n";
        let expected = "";
        let result = strip_extra_lines(input);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_strip_extra_lines_trims_leading_and_trailing() {
        let input = "\n\n\nLine 1\n\n\nLine 2\n\n\n";
        let expected = "Line 1\n\nLine 2";
        let result = strip_extra_lines(input);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_strip_extra_lines_real_changelog_example() {
        let input = r#"# Changelog


## [1.0.0] - 2022-01-01


### Features

- Added new feature



### Bug Fixes

- Fixed bug 1


- Fixed bug 2



## [0.9.0] - 2021-12-01



### Features

- Initial release"#;

        let result = strip_extra_lines(input);

        // Should not contain any triple newlines
        assert!(!result.contains("\n\n\n"));

        // Should still contain double newlines for proper formatting
        assert!(result.contains("\n\n"));

        // Should not be empty
        assert!(!result.is_empty());
    }

    #[test]
    fn test_update_release_with_commit_skip_ci() {
        let analyzer_config = AnalyzerConfig {
            skip_ci: true,
            ..AnalyzerConfig::default()
        };
        let group_parser = GroupParser::default();
        let mut release = Release::default();

        let ci_commit = ForgeCommitBuilder::default()
            .id("ci123")
            .short_id("ci1")
            .link("https://example.com/commit/ci123")
            .author_name("CI Bot")
            .author_email("ci@example.com")
            .merge_commit(false)
            .message("ci: update workflow")
            .timestamp(1640995100)
            .files(vec![])
            .build()
            .unwrap();

        let feat_commit = ForgeCommitBuilder::default()
            .id("feat123")
            .short_id("feat1")
            .link("https://example.com/commit/feat123")
            .author_name("Developer")
            .author_email("dev@example.com")
            .merge_commit(false)
            .message("feat: add feature")
            .timestamp(1640995200)
            .files(vec![])
            .build()
            .unwrap();

        update_release_with_commit(
            &group_parser,
            &mut release,
            &ci_commit,
            &analyzer_config,
        );
        update_release_with_commit(
            &group_parser,
            &mut release,
            &feat_commit,
            &analyzer_config,
        );

        // Should only have 1 commit (feat), ci filtered out
        assert_eq!(release.commits.len(), 1);
        assert_eq!(release.commits[0].id, "feat123");
    }

    #[test]
    fn test_update_release_with_commit_skip_chore() {
        let analyzer_config = AnalyzerConfig {
            skip_chore: true,
            ..AnalyzerConfig::default()
        };
        let group_parser = GroupParser::default();
        let mut release = Release::default();

        let chore_commit = ForgeCommitBuilder::default()
            .id("chore123")
            .short_id("cho1")
            .link("https://example.com/commit/chore123")
            .author_name("Maintainer")
            .author_email("maint@example.com")
            .merge_commit(false)
            .message("chore: update deps")
            .timestamp(1640995100)
            .files(vec![])
            .build()
            .unwrap();

        let fix_commit = ForgeCommitBuilder::default()
            .id("fix123")
            .short_id("fi1")
            .link("https://example.com/commit/fix123")
            .author_name("Developer")
            .author_email("dev@example.com")
            .merge_commit(false)
            .message("fix: bug fix")
            .timestamp(1640995200)
            .files(vec![])
            .build()
            .unwrap();

        update_release_with_commit(
            &group_parser,
            &mut release,
            &chore_commit,
            &analyzer_config,
        );
        update_release_with_commit(
            &group_parser,
            &mut release,
            &fix_commit,
            &analyzer_config,
        );

        // Should only have 1 commit (fix), chore filtered out
        assert_eq!(release.commits.len(), 1);
        assert_eq!(release.commits[0].id, "fix123");
    }

    #[test]
    fn test_update_release_with_commit_skip_miscellaneous() {
        let analyzer_config = AnalyzerConfig {
            skip_miscellaneous: true,
            ..AnalyzerConfig::default()
        };
        let group_parser = GroupParser::default();
        let mut release = Release::default();

        let misc_commit = ForgeCommitBuilder::default()
            .id("misc123")
            .short_id("mi1")
            .link("https://example.com/commit/misc123")
            .author_name("Random")
            .author_email("random@example.com")
            .merge_commit(false)
            .message("random message")
            .timestamp(1640995100)
            .files(vec![])
            .build()
            .unwrap();

        let feat_commit = ForgeCommitBuilder::default()
            .id("feat123")
            .short_id("fe1")
            .link("https://example.com/commit/feat123")
            .author_name("Developer")
            .author_email("dev@example.com")
            .merge_commit(false)
            .message("feat: add feature")
            .timestamp(1640995200)
            .files(vec![])
            .build()
            .unwrap();

        update_release_with_commit(
            &group_parser,
            &mut release,
            &misc_commit,
            &analyzer_config,
        );
        update_release_with_commit(
            &group_parser,
            &mut release,
            &feat_commit,
            &analyzer_config,
        );

        // Should only have 1 commit (feat), miscellaneous filtered out
        assert_eq!(release.commits.len(), 1);
        assert_eq!(release.commits[0].id, "feat123");
    }

    #[test]
    fn test_update_release_with_commit_skip_multiple_types() {
        let analyzer_config = AnalyzerConfig {
            skip_ci: true,
            skip_chore: true,
            skip_miscellaneous: true,
            ..AnalyzerConfig::default()
        };
        let group_parser = GroupParser::default();
        let mut release = Release::default();

        let commits = vec![
            ForgeCommitBuilder::default()
                .id("ci123")
                .short_id("ci1")
                .link("https://example.com/commit/ci123")
                .author_name("CI Bot")
                .author_email("ci@example.com")
                .merge_commit(false)
                .message("ci: update workflow")
                .timestamp(1640995100)
                .files(vec![])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("chore123")
                .short_id("ch1")
                .link("https://example.com/commit/chore123")
                .author_name("Maintainer")
                .author_email("maint@example.com")
                .merge_commit(false)
                .message("chore: update deps")
                .timestamp(1640995200)
                .files(vec![])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("misc123")
                .short_id("mi1")
                .link("https://example.com/commit/misc123")
                .author_name("Random")
                .author_email("random@example.com")
                .merge_commit(false)
                .message("random message")
                .timestamp(1640995300)
                .files(vec![])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("feat123")
                .short_id("fe1")
                .link("https://example.com/commit/feat123")
                .author_name("Developer")
                .author_email("dev@example.com")
                .merge_commit(false)
                .message("feat: add feature")
                .timestamp(1640995400)
                .files(vec![])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("fix123")
                .short_id("fi1")
                .link("https://example.com/commit/fix123")
                .author_name("Developer")
                .author_email("dev@example.com")
                .merge_commit(false)
                .message("fix: bug fix")
                .timestamp(1640995500)
                .files(vec![])
                .build()
                .unwrap(),
        ];

        for commit in &commits {
            update_release_with_commit(
                &group_parser,
                &mut release,
                commit,
                &analyzer_config,
            );
        }

        // Should only have 2 commits (feat and fix)
        assert_eq!(release.commits.len(), 2);
        assert_eq!(release.commits[0].id, "feat123");
        assert_eq!(release.commits[1].id, "fix123");
    }

    #[test]
    fn test_update_release_with_commit_preserves_author_info() {
        let analyzer_config = AnalyzerConfig::default();
        let group_parser = GroupParser::default();
        let mut release = Release::default();

        let commit_with_author = ForgeCommitBuilder::default()
            .id("author123")
            .short_id("au1")
            .link("https://example.com/commit/author123")
            .author_name("Jane Smith")
            .author_email("jane.smith@example.com")
            .merge_commit(false)
            .message("feat: new feature")
            .timestamp(1640995100)
            .files(vec![])
            .build()
            .unwrap();

        update_release_with_commit(
            &group_parser,
            &mut release,
            &commit_with_author,
            &analyzer_config,
        );

        assert_eq!(release.commits.len(), 1);
        assert_eq!(release.commits[0].author_name, "Jane Smith");
        assert_eq!(release.commits[0].author_email, "jane.smith@example.com");
    }

    #[test]
    fn test_update_release_with_commit_author_info_with_skip_options() {
        let analyzer_config = AnalyzerConfig {
            skip_ci: true,
            ..AnalyzerConfig::default()
        };
        let group_parser = GroupParser::default();
        let mut release = Release::default();

        let ci_commit = ForgeCommitBuilder::default()
            .id("ci123")
            .short_id("ci1")
            .link("https://example.com/commit/ci123")
            .author_name("CI Bot")
            .author_email("ci@example.com")
            .merge_commit(false)
            .message("ci: update workflow")
            .timestamp(1640995100)
            .files(vec![])
            .build()
            .unwrap();

        let feat_commit = ForgeCommitBuilder::default()
            .id("feat123")
            .short_id("fe1")
            .link("https://example.com/commit/feat123")
            .author_name("John Doe")
            .author_email("john.doe@example.com")
            .merge_commit(false)
            .message("feat: add feature")
            .timestamp(1640995200)
            .files(vec![])
            .build()
            .unwrap();

        update_release_with_commit(
            &group_parser,
            &mut release,
            &ci_commit,
            &analyzer_config,
        );
        update_release_with_commit(
            &group_parser,
            &mut release,
            &feat_commit,
            &analyzer_config,
        );

        // Should only have feat commit with author info preserved
        assert_eq!(release.commits.len(), 1);
        assert_eq!(release.commits[0].author_name, "John Doe");
        assert_eq!(release.commits[0].author_email, "john.doe@example.com");
    }

    #[test]
    fn test_update_release_with_commit_no_skip_includes_all() {
        let analyzer_config = AnalyzerConfig::default();
        let group_parser = GroupParser::default();
        let mut release = Release::default();

        let commits = vec![
            ForgeCommitBuilder::default()
                .id("ci123")
                .short_id("ci1")
                .link("https://example.com/commit/ci123")
                .author_name("CI Bot")
                .author_email("ci@example.com")
                .merge_commit(false)
                .message("ci: update workflow")
                .timestamp(1640995100)
                .files(vec![])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("chore123")
                .short_id("ch1")
                .link("https://example.com/commit/chore123")
                .author_name("Maintainer")
                .author_email("maint@example.com")
                .merge_commit(false)
                .message("chore: update deps")
                .timestamp(1640995200)
                .files(vec![])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("misc123")
                .short_id("mi1")
                .link("https://example.com/commit/misc123")
                .author_name("Random")
                .author_email("random@example.com")
                .merge_commit(false)
                .message("random message")
                .timestamp(1640995300)
                .files(vec![])
                .build()
                .unwrap(),
            ForgeCommitBuilder::default()
                .id("feat123")
                .short_id("fe1")
                .link("https://example.com/commit/feat123")
                .author_name("Developer")
                .author_email("dev@example.com")
                .merge_commit(false)
                .message("feat: add feature")
                .timestamp(1640995400)
                .files(vec![])
                .build()
                .unwrap(),
        ];

        for commit in &commits {
            update_release_with_commit(
                &group_parser,
                &mut release,
                commit,
                &analyzer_config,
            );
        }

        // Should have all 4 commits when no skip options are enabled
        assert_eq!(release.commits.len(), 4);
    }

    #[test]
    fn test_add_versioned_prerelease() {
        let version = Version::parse("1.0.0").unwrap();
        let result =
            add_prerelease(version, "alpha", PrereleaseStrategy::Versioned)
                .unwrap();
        assert_eq!(result, Version::parse("1.0.0-alpha.1").unwrap());
    }

    #[test]
    fn test_add_static_prerelease() {
        let version = Version::parse("0.1.0").unwrap();
        let result =
            add_prerelease(version, "SNAPSHOT", PrereleaseStrategy::Static)
                .unwrap();
        assert_eq!(result, Version::parse("0.1.0-SNAPSHOT").unwrap());
    }

    #[test]
    fn test_change_prerelease_suffix() {
        // Changing prerelease suffix to an existing prerelease replaces it
        let version = Version::parse("1.0.0-alpha.5").unwrap();
        let result =
            add_prerelease(version, "beta", PrereleaseStrategy::Versioned)
                .unwrap();
        assert_eq!(result, Version::parse("1.0.0-beta.1").unwrap());
    }

    #[test]
    fn test_graduate_prerelease() {
        let version = Version::parse("1.0.0-alpha.1").unwrap();
        let result = graduate_prerelease(&version);
        assert_eq!(result, Version::parse("1.0.0").unwrap());

        let version = Version::parse("2.3.4-beta.5").unwrap();
        let result = graduate_prerelease(&version);
        assert_eq!(result, Version::parse("2.3.4").unwrap());

        let version = Version::parse("0.1.0-rc.10").unwrap();
        let result = graduate_prerelease(&version);
        assert_eq!(result, Version::parse("0.1.0").unwrap());
    }

    #[test]
    fn test_graduate_prerelease_stable_version_unchanged() {
        let version = Version::parse("1.0.0").unwrap();
        let result = graduate_prerelease(&version);
        assert_eq!(result, Version::parse("1.0.0").unwrap());
    }

    #[test]
    fn test_graduate_prerelease_preserves_build_metadata() {
        let version = Version::parse("1.0.0-alpha.1+build.123").unwrap();
        let result = graduate_prerelease(&version);
        assert_eq!(result, Version::parse("1.0.0+build.123").unwrap());
    }
}