releasaurus-core 0.17.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
//! Data types for releases, tags, and commits.
use semver::Version;
use serde::{Deserialize, Serialize, ser::SerializeStruct};
use std::fmt::Display;

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

/// Git tag that represents a release version, linking a semantic version to
/// a specific commit SHA.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tag {
    /// Git commit SHA of the tag.
    pub sha: String,
    /// Tag name.
    pub name: String,
    /// Semantic version parsed from tag name.
    pub semver: semver::Version,
    /// Timestamp of tag
    pub timestamp: Option<i64>,
}

impl Default for Tag {
    fn default() -> Self {
        Self {
            name: "".into(),
            semver: Version::new(0, 0, 0),
            sha: "".into(),
            timestamp: None,
        }
    }
}

impl Display for Tag {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.name)
    }
}

impl Serialize for Tag {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut s = serializer.serialize_struct("Tag", 3)?;
        s.serialize_field("sha", &self.sha)?;
        s.serialize_field("name", &self.name)?;
        s.serialize_field("semver", &self.semver.to_string())?;
        s.end()
    }
}

/// Represents the serialized structure of a release that we actually use when
/// passing as a context to the Tera template. Really the only difference
/// is that we flatten the full Tag struct to separate tag_name and version
/// fields to make it easier to use in Tera template. Below we provide
/// #[serde(from = "ShadowRelease")] and a From implementation so we can
/// load from the generated json files and Deserialize back to a normal Release
/// struct enabling the "recompile-notes" feature
#[derive(Debug, Deserialize)]
struct ShadowRelease {
    pub version: String,
    pub tag_name: String,
    pub link: String,
    pub sha: String,
    pub commits: Vec<Commit>,
    pub include_author: bool,
    pub notes: String,
    pub timestamp: i64,
    // optional to for backward compatibility
    pub tag_compare_link: Option<String>,
    pub sha_compare_link: Option<String>,
}

/// Complete release package containing version tag, changelog notes, and all
/// associated commits for publishing.
#[derive(Clone, Default, Deserialize)]
#[serde(from = "ShadowRelease")]
pub struct Release {
    /// Associated version tag.
    pub tag: Tag,
    /// Release URL link.
    pub link: String,
    /// Link to diff between new tag and previous release tag
    /// This won't be valid until after we finish tagging the release
    /// but we still want to reference it when updating changelog
    pub tag_compare_link: String,
    /// Link to diff between new release sha and previous release tag
    /// This should always be valid
    pub sha_compare_link: String,
    /// Git commit SHA for the release.
    pub sha: String,
    /// Commits included in this release.
    pub commits: Vec<Commit>,
    /// Whether or not to include author name for each commit in changelog
    pub include_author: bool,
    /// Generated release notes.
    pub notes: String,
    /// Release timestamp.
    pub timestamp: i64,
}

impl From<ShadowRelease> for Release {
    fn from(value: ShadowRelease) -> Self {
        Self {
            commits: value.commits,
            include_author: value.include_author,
            link: value.link,
            tag_compare_link: value.tag_compare_link.unwrap_or_default(),
            sha_compare_link: value.sha_compare_link.unwrap_or_default(),
            notes: value.notes,
            sha: value.sha,
            timestamp: value.timestamp,
            tag: Tag {
                name: value.tag_name,
                semver: semver::Version::parse(&value.version)
                    .unwrap_or(semver::Version::new(0, 0, 0)),
                sha: "".into(),
                timestamp: None,
            },
        }
    }
}

impl std::fmt::Debug for Release {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Release")
            .field("tag", &self.tag)
            .field("link", &self.link)
            .field("tag_compare_link", &self.tag_compare_link)
            .field("sha_compare_link", &self.sha_compare_link)
            .field("sha", &self.sha)
            .field("include_author", &self.include_author)
            .field("timestamp", &self.timestamp)
            .finish()
    }
}

impl Serialize for Release {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut s = serializer.serialize_struct("Release", 10)?;
        s.serialize_field("link", &self.link)?;
        s.serialize_field("tag_compare_link", &self.tag_compare_link)?;
        s.serialize_field("sha_compare_link", &self.sha_compare_link)?;
        s.serialize_field("version", &self.tag.semver.to_string())?;
        s.serialize_field("tag_name", &self.tag.name)?;
        s.serialize_field("sha", &self.sha)?;
        s.serialize_field("include_author", &self.include_author)?;
        s.serialize_field("commits", &self.commits)?;
        s.serialize_field("notes", &self.notes)?;
        s.serialize_field("timestamp", &self.timestamp)?;
        s.end()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analyzer::commit::Commit;
    use semver::Version;
    use serde_json;

    // Tag tests

    #[test]
    fn tag_default_creates_empty_tag() {
        let tag = Tag::default();
        assert_eq!(tag.name, "");
        assert_eq!(tag.sha, "");
        assert_eq!(tag.semver, Version::new(0, 0, 0));
        assert_eq!(tag.timestamp, None);
    }

    #[test]
    fn tag_display_shows_name() {
        let tag = Tag {
            name: "v1.2.3".to_string(),
            sha: "abc123".to_string(),
            semver: Version::new(1, 2, 3),
            timestamp: Some(1234567890),
        };
        assert_eq!(format!("{}", tag), "v1.2.3");
    }

    #[test]
    fn tag_display_handles_empty_name() {
        let tag = Tag::default();
        assert_eq!(format!("{}", tag), "");
    }

    #[test]
    fn tag_serialize_includes_all_fields() {
        let tag = Tag {
            name: "v1.2.3".to_string(),
            sha: "abc123def456".to_string(),
            semver: Version::new(1, 2, 3),
            timestamp: Some(1234567890),
        };

        let json = serde_json::to_value(&tag).unwrap();
        assert_eq!(json["name"], "v1.2.3");
        assert_eq!(json["sha"], "abc123def456");
        assert_eq!(json["semver"], "1.2.3");
    }

    #[test]
    fn tag_serialize_with_prerelease() {
        let tag = Tag {
            name: "v2.0.0-beta.1".to_string(),
            sha: "xyz789".to_string(),
            semver: Version::parse("2.0.0-beta.1").unwrap(),
            timestamp: None,
        };

        let json = serde_json::to_value(&tag).unwrap();
        assert_eq!(json["semver"], "2.0.0-beta.1");
    }

    #[test]
    fn tag_equality_works() {
        let tag1 = Tag {
            name: "v1.0.0".to_string(),
            sha: "abc".to_string(),
            semver: Version::new(1, 0, 0),
            timestamp: Some(123),
        };
        let tag2 = Tag {
            name: "v1.0.0".to_string(),
            sha: "abc".to_string(),
            semver: Version::new(1, 0, 0),
            timestamp: Some(123),
        };
        assert_eq!(tag1, tag2);
    }

    #[test]
    fn tag_inequality_by_semver() {
        let tag1 = Tag {
            name: "v1.0.0".to_string(),
            sha: "abc".to_string(),
            semver: Version::new(1, 0, 0),
            timestamp: None,
        };
        let tag2 = Tag {
            name: "v1.0.1".to_string(),
            sha: "abc".to_string(),
            semver: Version::new(1, 0, 1),
            timestamp: None,
        };
        assert_ne!(tag1, tag2);
    }

    // Release tests

    #[test]
    fn release_debug_excludes_commits_and_notes() {
        let release = Release {
            tag: Tag {
                name: "v1.0.0".to_string(),
                sha: "tag_sha".to_string(),
                semver: Version::new(1, 0, 0),
                timestamp: Some(1234567890),
            },
            link: "https://example.com/release".to_string(),
            tag_compare_link: "https://example.com/compare/v0.9.0...v1.0.0"
                .into(),
            sha_compare_link:
                "https://example.com/compare/v0.9.0...release_sha".into(),
            sha: "release_sha".to_string(),
            commits: vec![Commit::default()],
            include_author: true,
            notes: "Some long release notes...".to_string(),
            timestamp: 9876543210,
        };

        let debug_str = format!("{:?}", release);

        // Should include these fields
        assert!(debug_str.contains("Release"));
        assert!(debug_str.contains("tag"));
        assert!(debug_str.contains("link"));
        assert!(debug_str.contains("tag_compare_link"));
        assert!(debug_str.contains("sha_compare_link"));
        assert!(debug_str.contains("sha"));
        assert!(debug_str.contains("include_author"));
        assert!(debug_str.contains("timestamp"));

        // Should NOT include commits or notes in debug output
        assert!(!debug_str.contains("commits"));
        assert!(!debug_str.contains("notes"));
    }

    #[test]
    fn release_serialize_includes_all_fields() {
        let tag = Tag {
            name: "v2.1.0".to_string(),
            sha: "tag_sha_123".to_string(),
            semver: Version::new(2, 1, 0),
            timestamp: Some(1111111111),
        };

        let commit = Commit {
            id: "commit_sha".to_string(),
            raw_message: "feat: new feature".to_string(),
            ..Default::default()
        };

        let release = Release {
            tag,
            link: "https://github.com/owner/repo/releases/tag/v2.1.0"
                .to_string(),
            tag_compare_link:
                "https://github.com/owner/repo/compare/v2.0.0...v2.1.0".into(),
            sha_compare_link:
                "https://github.com/owner/repo/compare/v2.0.0...release_sha_456"
                    .into(),
            sha: "release_sha_456".to_string(),
            commits: vec![commit],
            include_author: true,
            notes: "# Release Notes\n\n- Added feature".to_string(),
            timestamp: 1234567890,
        };

        let json = serde_json::to_value(&release).unwrap();

        assert_eq!(
            json["link"],
            "https://github.com/owner/repo/releases/tag/v2.1.0"
        );
        assert_eq!(
            json["tag_compare_link"],
            "https://github.com/owner/repo/compare/v2.0.0...v2.1.0"
        );
        assert_eq!(
            json["sha_compare_link"],
            "https://github.com/owner/repo/compare/v2.0.0...release_sha_456"
        );
        assert_eq!(json["version"], "2.1.0");
        assert_eq!(json["sha"], "release_sha_456");
        assert_eq!(json["include_author"], true);
        assert!(json["commits"].is_array());
        assert_eq!(json["commits"].as_array().unwrap().len(), 1);
        assert_eq!(json["notes"], "# Release Notes\n\n- Added feature");
        assert_eq!(json["timestamp"], 1234567890);
    }

    #[test]
    fn release_serialize_empty_commits() {
        let release = Release {
            tag: Tag::default(),
            link: "".to_string(),
            tag_compare_link: "".to_string(),
            sha_compare_link: "".to_string(),
            sha: "".to_string(),
            commits: vec![],
            include_author: false,
            notes: "".to_string(),
            timestamp: 0,
        };

        let json = serde_json::to_value(&release).unwrap();
        assert!(json["commits"].is_array());
        assert_eq!(json["commits"].as_array().unwrap().len(), 0);
    }

    #[test]
    fn release_with_multiple_commits() {
        let commits = vec![
            Commit {
                id: "sha1".to_string(),
                raw_message: "feat: feature 1".to_string(),
                ..Default::default()
            },
            Commit {
                id: "sha2".to_string(),
                raw_message: "fix: bug fix".to_string(),
                ..Default::default()
            },
            Commit {
                id: "sha3".to_string(),
                raw_message: "docs: update docs".to_string(),
                ..Default::default()
            },
        ];

        let release = Release {
            tag: Tag::default(),
            link: "".to_string(),
            tag_compare_link: "".to_string(),
            sha_compare_link: "".to_string(),
            sha: "".to_string(),
            commits,
            include_author: false,
            notes: "".to_string(),
            timestamp: 0,
        };

        assert_eq!(release.commits.len(), 3);
        assert_eq!(release.commits[0].id, "sha1");
        assert_eq!(release.commits[1].id, "sha2");
        assert_eq!(release.commits[2].id, "sha3");
    }
}