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
use std::fmt;
use std::sync::LazyLock;

use regex::Regex;
use serde::{Deserialize, Serialize};

use crate::analyzer::commit::Commit;

/// Commit categories based on conventional commit types, used for grouping
/// changes in the changelog.
#[derive(
    Debug,
    Copy,
    Clone,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Serialize,
    Deserialize,
)]
pub enum Group {
    #[serde(rename = "<!-- 00 -->โŒ Breaking")]
    Breaking,
    #[serde(rename = "<!-- 01 -->๐Ÿš€ Features")]
    Feat,
    #[serde(rename = "<!-- 02 -->๐Ÿ› Bug Fixes")]
    Fix,
    #[serde(rename = "<!-- 03 -->โ—€๏ธ Revert")]
    Revert,
    #[serde(rename = "<!-- 04 -->๐Ÿšœ Refactor")]
    Refactor,
    #[serde(rename = "<!-- 05 -->โšก Performance")]
    Perf,
    #[serde(rename = "<!-- 06 -->๐Ÿ“š Documentation")]
    Doc,
    #[serde(rename = "<!-- 07 -->๐ŸŽจ Styling")]
    Style,
    #[serde(rename = "<!-- 08 -->๐Ÿงช Testing")]
    Test,
    #[serde(rename = "<!-- 09 -->๐Ÿงน Chore")]
    Chore,
    #[serde(rename = "<!-- 10 -->โฉ CI/CD")]
    Ci,
    #[serde(rename = "<!-- 11 -->โš™๏ธ Miscellaneous Tasks")]
    #[default]
    Miscellaneous,
}

impl fmt::Display for Group {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Group::Breaking => "breaking",
            Group::Feat => "feat",
            Group::Fix => "fix",
            Group::Revert => "revert",
            Group::Refactor => "refactor",
            Group::Perf => "perf",
            Group::Doc => "doc",
            Group::Style => "style",
            Group::Test => "test",
            Group::Chore => "chore",
            Group::Ci => "ci",
            Group::Miscellaneous => "miscellaneous",
        };
        write!(f, "{s}")
    }
}

// Parser data structure that can parse groups from commit message patterns
struct Parser {
    pattern: Regex,
    target_group: Group,
}

impl Parser {
    fn new(pattern: Regex, target_group: Group) -> Self {
        Self {
            pattern,
            target_group,
        }
    }

    pub fn parse(&self, c: &Commit) -> Option<Group> {
        if self.pattern.is_match(c.raw_message.trim()) {
            return Some(self.target_group);
        }

        None
    }
}

// CHORE
static CHORE_PARSER: LazyLock<Parser> =
    LazyLock::new(|| Parser::new(Regex::new(r"^chore").unwrap(), Group::Chore));

// CI
static CI_PARSER: LazyLock<Parser> =
    LazyLock::new(|| Parser::new(Regex::new(r"^ci").unwrap(), Group::Ci));

// DOC
static DOC_PARSER: LazyLock<Parser> =
    LazyLock::new(|| Parser::new(Regex::new(r"^doc").unwrap(), Group::Doc));

// FEAT
static FEAT_PARSER: LazyLock<Parser> =
    LazyLock::new(|| Parser::new(Regex::new(r"^feat").unwrap(), Group::Feat));

// FIX
static FIX_PARSER: LazyLock<Parser> =
    LazyLock::new(|| Parser::new(Regex::new(r"^fix").unwrap(), Group::Fix));

// PERF
static PERF_PARSER: LazyLock<Parser> =
    LazyLock::new(|| Parser::new(Regex::new(r"^perf").unwrap(), Group::Perf));

// REFACTOR
static REFACTOR_PARSER: LazyLock<Parser> = LazyLock::new(|| {
    Parser::new(Regex::new(r"^refactor").unwrap(), Group::Refactor)
});

// REVERT
static REVERT_PARSER: LazyLock<Parser> = LazyLock::new(|| {
    Parser::new(Regex::new(r"^revert").unwrap(), Group::Revert)
});

// STYLE
static STYLE_PARSER: LazyLock<Parser> =
    LazyLock::new(|| Parser::new(Regex::new(r"^style").unwrap(), Group::Style));

// TEST
static TEST_PARSER: LazyLock<Parser> =
    LazyLock::new(|| Parser::new(Regex::new(r"^test").unwrap(), Group::Test));

static GROUP_PARSERS: [&LazyLock<Parser>; 10] = [
    &FEAT_PARSER,
    &FIX_PARSER,
    &REVERT_PARSER,
    &REFACTOR_PARSER,
    &PERF_PARSER,
    &DOC_PARSER,
    &STYLE_PARSER,
    &TEST_PARSER,
    &CHORE_PARSER,
    &CI_PARSER,
];

/// Determines which changelog category a commit belongs to by matching
/// against conventional commit type patterns.
#[derive(Default)]
pub struct GroupParser {}

impl GroupParser {
    /// Determine the changelog category for a commit by checking breaking
    /// changes first, then matching commit type prefixes.
    pub fn parse(&self, commit: &Commit) -> Group {
        if commit.breaking {
            return Group::Breaking;
        }

        for parser in GROUP_PARSERS {
            if let Some(group) = parser.parse(commit) {
                return group;
            }
        }

        Group::default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_commit(raw_message: &str, breaking: bool) -> Commit {
        Commit {
            id: "abc123".to_string(),
            short_id: "abc".to_string(),
            group: Group::default(),
            scope: None,
            title: "test message".to_string(),
            body: None,
            link: "https://example.com".to_string(),
            breaking,
            breaking_description: None,
            merge_commit: false,
            timestamp: 1640995200,
            raw_title: "test message".to_string(),
            raw_message: raw_message.to_string(),
            author_name: "".into(),
            author_email: "".into(),
        }
    }

    #[test]
    fn test_group_default() {
        let group = Group::default();
        assert_eq!(group, Group::Miscellaneous);
    }

    #[test]
    fn test_group_equality() {
        assert_eq!(Group::Feat, Group::Feat);
        assert_eq!(Group::Fix, Group::Fix);
        assert_eq!(Group::Breaking, Group::Breaking);
        assert_ne!(Group::Feat, Group::Fix);
        assert_ne!(Group::Breaking, Group::Miscellaneous);
    }

    #[test]
    fn test_group_ordering() {
        // Test that Breaking comes first in sort order
        let mut groups = [Group::Fix, Group::Breaking, Group::Feat];
        groups.sort();
        assert_eq!(groups[0], Group::Breaking);

        // Test other orderings
        assert!(Group::Breaking < Group::Feat);
        assert!(Group::Feat < Group::Fix);
        assert!(Group::Miscellaneous > Group::Ci); // Other should be last
    }

    #[test]
    fn test_group_serialization() {
        let test_cases = vec![
            (Group::Breaking, "โŒ Breaking"),
            (Group::Feat, "๐Ÿš€ Features"),
            (Group::Fix, "๐Ÿ› Bug Fixes"),
            (Group::Revert, "โ—€๏ธ Revert"),
            (Group::Refactor, "๐Ÿšœ Refactor"),
            (Group::Perf, "โšก Performance"),
            (Group::Doc, "๐Ÿ“š Documentation"),
            (Group::Style, "๐ŸŽจ Styling"),
            (Group::Test, "๐Ÿงช Testing"),
            (Group::Chore, "๐Ÿงน Chore"),
            (Group::Ci, "โฉ CI/CD"),
            (Group::Miscellaneous, "โš™๏ธ Miscellaneous Tasks"),
        ];

        for (group, expected) in test_cases {
            let json = serde_json::to_string(&group)
                .expect("Failed to serialize group");
            assert!(
                json.contains(expected),
                "Group {:?} should serialize to contain '{}'",
                group,
                expected
            );
        }
    }

    #[test]
    fn test_group_parser_breaking_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("feat!: breaking change", true);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Breaking);
    }

    #[test]
    fn test_group_parser_feat_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("feat: add new feature", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Feat);
    }

    #[test]
    fn test_group_parser_fix_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("fix: resolve bug", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Fix);
    }

    #[test]
    fn test_group_parser_chore_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("chore: update dependencies", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Chore);
    }

    #[test]
    fn test_group_parser_ci_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("ci: update workflow", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Ci);
    }

    #[test]
    fn test_group_parser_doc_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("doc: update readme", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Doc);
    }

    #[test]
    fn test_group_parser_perf_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("perf: optimize algorithm", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Perf);
    }

    #[test]
    fn test_group_parser_refactor_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("refactor: clean up code", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Refactor);
    }

    #[test]
    fn test_group_parser_revert_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("revert: undo previous change", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Revert);
    }

    #[test]
    fn test_group_parser_style_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("style: format code", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Style);
    }

    #[test]
    fn test_group_parser_test_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("test: add unit tests", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Test);
    }

    #[test]
    fn test_group_parser_unknown_commit() {
        let parser = GroupParser::default();
        let commit = create_test_commit("random: unknown type", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Miscellaneous);
    }

    #[test]
    fn test_group_parser_empty_message() {
        let parser = GroupParser::default();
        let commit = create_test_commit("", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Miscellaneous);
    }

    #[test]
    fn test_group_parser_whitespace_handling() {
        let parser = GroupParser::default();
        let commit =
            create_test_commit("  feat: feature with leading spaces", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Feat);
    }

    #[test]
    fn test_group_parser_case_sensitivity() {
        let parser = GroupParser::default();

        // Lowercase should match
        let commit1 = create_test_commit("feat: lowercase", false);
        assert_eq!(parser.parse(&commit1), Group::Feat);

        // Uppercase should not match (our regexes are case-sensitive)
        let commit2 = create_test_commit("FEAT: uppercase", false);
        assert_eq!(parser.parse(&commit2), Group::Miscellaneous);
    }

    #[test]
    fn test_group_parser_breaking_takes_precedence() {
        let parser = GroupParser::default();
        // Even if it matches feat pattern, breaking should take precedence
        let commit = create_test_commit("feat!: breaking feature", true);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Breaking);
    }

    #[test]
    fn test_group_parser_with_scope() {
        let parser = GroupParser::default();
        let commit = create_test_commit("feat(api): add endpoint", false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Feat);
    }

    #[test]
    fn test_group_parser_multiline_message() {
        let parser = GroupParser::default();
        let multiline_msg = "fix: resolve issue\n\nThis is a longer description\nwith multiple lines";
        let commit = create_test_commit(multiline_msg, false);
        let group = parser.parse(&commit);
        assert_eq!(group, Group::Fix);
    }

    #[test]
    fn test_all_groups_covered() {
        let parser = GroupParser::default();

        // Test that we have parsers for all the main groups
        let test_cases = vec![
            ("feat: test", Group::Feat),
            ("fix: test", Group::Fix),
            ("chore: test", Group::Chore),
            ("doc: test", Group::Doc),
            ("style: test", Group::Style),
            ("refactor: test", Group::Refactor),
            ("perf: test", Group::Perf),
            ("test: test", Group::Test),
            ("revert: test", Group::Revert),
            ("ci: test", Group::Ci),
        ];

        for (message, expected_group) in test_cases {
            let commit = create_test_commit(message, false);
            let parsed_group = parser.parse(&commit);
            assert_eq!(
                parsed_group, expected_group,
                "Failed for message: {}",
                message
            );
        }
    }

    #[test]
    fn test_group_parser_order_matters() {
        let parser = GroupParser::default();

        // Breaking should always take precedence over other types
        let breaking_feat = create_test_commit(
            "feat: breaking feature\n\nBREAKING CHANGE: it broke",
            true,
        );
        assert_eq!(parser.parse(&breaking_feat), Group::Breaking);

        let breaking_fix = create_test_commit("fix!: breaking fix", true);
        assert_eq!(parser.parse(&breaking_fix), Group::Breaking);
    }
}