communique 1.6.1

Editorialized release notes powered by AI
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
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use miette::IntoDiagnostic;
use serde::{Deserialize, Serialize};

use crate::{github::GitHubClient, output::ParsedOutput};

#[derive(Debug, Default, Clone, usage_rs::Args)]
pub struct WorkflowOptions {
    /// Save editable release title, body, changelog, and review as JSON
    #[usage(long, effect = "write")]
    pub draft: Option<PathBuf>,
    /// Write a Markdown coverage report with sources and omission reasons
    #[usage(long, effect = "write")]
    pub review_report: Option<PathBuf>,
    /// Write an upgrade guide with affected users and before/after examples
    #[usage(long, effect = "write")]
    pub migration_guide: Option<PathBuf>,
    /// Generate notes for a package configured in communique.toml
    #[usage(long)]
    pub package: Option<String>,
    /// Limit changes to repository-relative paths (repeatable)
    #[usage(long = "path")]
    pub paths: Vec<String>,
    /// Only consider baseline tags matching this git glob
    #[usage(long)]
    pub tag_pattern: Option<String>,
    /// Baseline channel: stable excludes prereleases; all includes them
    #[usage(long, choices("stable", "all"))]
    pub channel: Option<Channel>,
    /// Changelog destination relative to the repository root
    #[usage(long)]
    pub changelog_path: Option<PathBuf>,
    /// Replace only the communique-marked section of a GitHub release
    #[usage(long)]
    pub preserve_sections: bool,
}

#[derive(Debug, Clone, Copy, Default, Deserialize, strum::EnumString)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum Channel {
    Stable,
    #[default]
    All,
}

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Package {
    #[serde(default)]
    pub paths: Vec<String>,
    pub tag_pattern: Option<String>,
    pub channel: Option<Channel>,
    pub changelog_path: Option<PathBuf>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Rules {
    #[serde(default)]
    pub include_labels: Vec<String>,
    #[serde(default)]
    pub exclude_labels: Vec<String>,
    #[serde(default)]
    pub categories: BTreeMap<String, String>,
}

impl WorkflowOptions {
    pub fn resolve(&mut self, config: &crate::config::Config) -> miette::Result<()> {
        if let Some(name) = &self.package {
            let package = config
                .packages
                .get(name)
                .ok_or_else(|| miette::miette!("Unknown package '{name}' in communique.toml"))?;
            if self.paths.is_empty() {
                self.paths = package.paths.clone();
            }
            self.tag_pattern = self.tag_pattern.take().or(package.tag_pattern.clone());
            self.channel = self.channel.or(package.channel);
            self.changelog_path = self
                .changelog_path
                .take()
                .or(package.changelog_path.clone());
        }
        for path in self
            .paths
            .iter()
            .map(Path::new)
            .chain(self.changelog_path.iter().map(PathBuf::as_path))
        {
            if path.as_os_str().is_empty()
                || path.components().any(|c| {
                    !matches!(
                        c,
                        std::path::Component::Normal(_) | std::path::Component::CurDir
                    )
                })
            {
                return Err(miette::miette!(
                    "Paths must be relative to the repository and cannot contain '..': {}",
                    path.display()
                ));
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Review {
    pub coverage: Vec<Coverage>,
    pub migration_guide: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Coverage {
    pub commit: String,
    pub status: String,
    pub reason: String,
}

impl Review {
    pub fn from_submission(input: &serde_json::Value) -> Self {
        Self {
            coverage: serde_json::from_value(input["coverage"].clone()).unwrap_or_default(),
            migration_guide: input["migration_guide"]
                .as_str()
                .unwrap_or_default()
                .to_string(),
        }
    }

    /// Keep only known commits, fill gaps explicitly, and preserve enforced exclusions.
    pub fn reconcile(&mut self, log: &str, excluded: Vec<Coverage>) {
        let mut entries = Vec::new();
        for line in log.lines() {
            let sha = line.split_whitespace().next().unwrap_or_default();
            let matches: Vec<_> = self.coverage.iter().filter(|c| c.commit == sha).collect();
            let entry = if matches.len() == 1
                && ["included", "omitted", "uncertain"].contains(&matches[0].status.as_str())
                && !matches[0].reason.trim().is_empty()
            {
                matches[0].clone()
            } else {
                Coverage { commit: sha.into(), status: "uncertain".into(), reason: "The model did not provide a valid, unique assessment; review this change manually.".into() }
            };
            entries.push(entry);
        }
        entries.extend(excluded);
        self.coverage = entries;
    }

    pub fn markdown(&self, repo: &str, from: &str, to: &str) -> String {
        let mut result = format!(
            "# Release coverage\n\nRange: `{from}..{to}`\n\nAssessments are model-generated, except explicit label exclusions. Uncertain entries require manual review.\n"
        );
        for entry in &self.coverage {
            result.push_str(&format!(
                "\n- **{}** [{}](https://github.com/{repo}/commit/{}): {}\n",
                entry.status, entry.commit, entry.commit, entry.reason
            ));
        }
        result
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Draft {
    pub schema_version: u32,
    pub repo: String,
    pub tag: String,
    pub previous_ref: String,
    pub target_commit: String,
    pub release_title: String,
    pub release_body: String,
    pub changelog: String,
    pub preserve_sections: bool,
    pub review: Review,
}

impl Draft {
    pub fn new(
        repo: &str,
        tag: &str,
        previous_ref: &str,
        target_commit: &str,
        notes: &ParsedOutput,
        preserve_sections: bool,
    ) -> Self {
        Self {
            schema_version: 1,
            repo: repo.into(),
            tag: tag.into(),
            previous_ref: previous_ref.into(),
            target_commit: target_commit.into(),
            release_title: notes.release_title.clone(),
            release_body: notes.release_body.clone(),
            changelog: notes.changelog.clone(),
            preserve_sections,
            review: notes.review.clone(),
        }
    }
    pub fn write(&self, path: &Path) -> miette::Result<()> {
        std::fs::write(
            path,
            format!(
                "{}\n",
                serde_json::to_string_pretty(self).into_diagnostic()?
            ),
        )
        .into_diagnostic()
    }
    fn validate(&self) -> miette::Result<()> {
        if self.schema_version != 1 {
            return Err(miette::miette!(
                "Unsupported draft schema version {}",
                self.schema_version
            ));
        }
        if self.tag == "HEAD" || self.tag.trim().is_empty() {
            return Err(miette::miette!(
                "Publishing requires a release tag, not HEAD"
            ));
        }
        if self.release_title.trim().is_empty() || self.release_body.trim().is_empty() {
            return Err(miette::miette!("Draft title and body cannot be empty"));
        }
        if self.target_commit.len() != 40
            || !self.target_commit.bytes().all(|b| b.is_ascii_hexdigit())
        {
            return Err(miette::miette!(
                "Draft target_commit must be a full 40-character commit SHA"
            ));
        }
        let parts: Vec<_> = self.repo.split('/').collect();
        if parts.len() != 2
            || parts.iter().any(|p| {
                p.is_empty()
                    || !p
                        .chars()
                        .all(|c| c.is_ascii_alphanumeric() || "-_.".contains(c))
            })
        {
            return Err(miette::miette!("Draft repo must be owner/repo"));
        }
        Ok(())
    }
}

pub const START: &str = "<!-- communique:start -->";
pub const END: &str = "<!-- communique:end -->";

/// Locate only the managed content, sharing the same marker validation as publishing.
fn managed_range(existing: &str) -> miette::Result<Option<std::ops::Range<usize>>> {
    let starts: Vec<_> = existing.match_indices(START).collect();
    let ends: Vec<_> = existing.match_indices(END).collect();
    match (starts.as_slice(), ends.as_slice()) {
        ([], []) => Ok(None),
        ([(start, _)], [(end, _)]) if start < end => Ok(Some(start + START.len()..*end)),
        _ => Err(miette::miette!(
            "Release must contain either no communique markers or exactly one ordered start/end pair"
        )),
    }
}

pub fn managed_content(existing: &str) -> miette::Result<Option<&str>> {
    Ok(managed_range(existing)?.map(|range| existing[range].trim()))
}

pub fn merge_body(existing: &str, generated: &str) -> miette::Result<String> {
    if generated.contains(START) || generated.contains(END) {
        return Err(miette::miette!(
            "Generated body must not contain communique section markers"
        ));
    }
    match managed_range(existing)? {
        None => Ok(format!(
            "{}{}{}\n{}\n{}",
            existing,
            if existing.is_empty() { "" } else { "\n\n" },
            START,
            generated,
            END
        )),
        Some(range) => Ok(format!(
            "{}\n{}\n{}",
            &existing[..range.start],
            generated,
            &existing[range.end..]
        )),
    }
}

pub async fn publish_draft(path: &Path, dry_run: bool) -> miette::Result<()> {
    let draft: Draft = serde_json::from_str(&std::fs::read_to_string(path).into_diagnostic()?)
        .into_diagnostic()?;
    draft.validate()?;
    if dry_run && !draft.preserve_sections {
        println!("# {}\n\n{}", draft.release_title, draft.release_body);
        return Ok(());
    }
    let token = std::env::var("GITHUB_TOKEN")
        .into_diagnostic()
        .map_err(|_| miette::miette!("GITHUB_TOKEN is required to read or update the release"))?;
    let gh = GitHubClient::new(token, &draft.repo)?;
    publish_with_client(draft, dry_run, &gh).await
}

async fn publish_with_client(draft: Draft, dry_run: bool, gh: &GitHubClient) -> miette::Result<()> {
    draft.validate()?;
    let release = gh
        .get_release_by_tag(&draft.tag)
        .await?
        .ok_or_else(|| miette::miette!("No GitHub release found for {}", draft.tag))?;
    let body = if draft.preserve_sections {
        merge_body(
            release.body.as_deref().unwrap_or_default(),
            &draft.release_body,
        )?
    } else {
        draft.release_body
    };
    if dry_run {
        let title = if draft.preserve_sections {
            release.name.as_deref().unwrap_or(&draft.tag)
        } else {
            &draft.release_title
        };
        println!("# {title}\n\n{body}");
    } else {
        let remote_commit = gh.tag_commit(&draft.tag).await?;
        if remote_commit != draft.target_commit {
            return Err(miette::miette!(
                "Tag {} now resolves to {}, but this draft was generated for {}. Regenerate and review the draft before publishing.",
                draft.tag,
                remote_commit,
                draft.target_commit
            ));
        }
        gh.update_release(
            release.id,
            &release.tag_name,
            if draft.preserve_sections {
                None
            } else {
                Some(&draft.release_title)
            },
            Some(&body),
        )
        .await?;
        eprintln!("Updated {}/{}", draft.repo, draft.tag);
    }
    Ok(())
}

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

    #[tokio::test]
    async fn stale_or_missing_remote_tag_never_updates_release() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};
        for status in [200, 404, 403] {
            let server = MockServer::start().await;
            Mock::given(method("GET"))
                .and(path("/repos/owner/repo/releases/tags/v1"))
                .respond_with(
                    ResponseTemplate::new(200)
                        .set_body_json(serde_json::json!({"id": 7, "tag_name": "v1"})),
                )
                .mount(&server)
                .await;
            Mock::given(method("GET"))
                .and(path("/repos/owner/repo/commits/refs/tags/v1"))
                .respond_with(ResponseTemplate::new(status).set_body_json(
                    serde_json::json!({"sha": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}),
                ))
                .mount(&server)
                .await;
            Mock::given(method("PATCH"))
                .respond_with(ResponseTemplate::new(200))
                .expect(0)
                .mount(&server)
                .await;
            let gh =
                GitHubClient::with_base_url("token".into(), "owner/repo", server.uri()).unwrap();
            let notes = ParsedOutput {
                changelog: "Change".into(),
                release_title: "Title".into(),
                release_body: "Body".into(),
                usage: Default::default(),
                review: Default::default(),
            };
            let draft = Draft::new(
                "owner/repo",
                "v1",
                "v0",
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                &notes,
                false,
            );
            let err = publish_with_client(draft, false, &gh).await.unwrap_err();
            assert!(err.to_string().contains(if status == 200 {
                "Regenerate and review"
            } else {
                "Cannot resolve remote tag"
            }));
            server.verify().await;
        }
    }

    #[test]
    fn malformed_draft_fields_are_rejected() {
        let notes = ParsedOutput {
            changelog: "Change".into(),
            release_title: "Title".into(),
            release_body: "Body".into(),
            usage: Default::default(),
            review: Default::default(),
        };
        let draft = Draft::new(
            "owner/repo",
            "v1",
            "v0",
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
            &notes,
            false,
        );
        let value = serde_json::to_value(draft).unwrap();
        for (field, invalid) in [
            ("repo", "owner"),
            ("repo", "owner//repo"),
            ("repo", "owner/repo?bad"),
            ("release_title", " "),
            ("release_body", " "),
            ("target_commit", ""),
            ("target_commit", "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"),
            ("tag", ""),
        ] {
            let mut invalid_value = value.clone();
            invalid_value[field] = serde_json::json!(invalid);
            let draft: Draft = serde_json::from_value(invalid_value).unwrap();
            assert!(draft.validate().is_err(), "accepted {field}: {invalid}");
        }
    }

    #[tokio::test]
    async fn publish_sends_exact_edited_content_and_preview_does_not_patch() {
        use wiremock::matchers::{body_json, method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/releases/tags/v1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(
                serde_json::json!({"id": 7, "tag_name": "v1", "name": "Existing", "body": "Old"}),
            ))
            .mount(&server)
            .await;
        Mock::given(method("PATCH"))
            .and(path("/repos/owner/repo/releases/7"))
            .and(body_json(
                serde_json::json!({"tag_name": "v1", "name": "Edited title", "body": "Edited body\n\n"}),
            ))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"id": 7, "tag_name": "v1"})),
            )
            .expect(1)
            .mount(&server)
            .await;
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/commits/refs/tags/v1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(
                serde_json::json!({"sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}),
            ))
            .expect(1)
            .mount(&server)
            .await;
        let gh = GitHubClient::with_base_url("token".into(), "owner/repo", server.uri()).unwrap();
        let notes = ParsedOutput {
            changelog: "Change".into(),
            release_title: "Edited title".into(),
            release_body: "Edited body\n\n".into(),
            usage: Default::default(),
            review: Default::default(),
        };
        publish_with_client(
            Draft::new(
                "owner/repo",
                "v1",
                "v0",
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                &notes,
                false,
            ),
            true,
            &gh,
        )
        .await
        .unwrap();
        publish_with_client(
            Draft::new(
                "owner/repo",
                "v1",
                "v0",
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                &notes,
                false,
            ),
            false,
            &gh,
        )
        .await
        .unwrap();
        server.verify().await;
    }

    #[tokio::test]
    async fn publish_preserves_title_and_surrounding_sections() {
        use wiremock::matchers::{body_json, method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};
        let server = MockServer::start().await;
        let existing = format!("Manual intro\n{START}\nOld\n{END}\nManual footer");
        let expected = format!("Manual intro\n{START}\nNew\n{END}\nManual footer");
        Mock::given(method("GET")).and(path("/repos/owner/repo/releases/tags/v1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"id": 7, "tag_name": "v1", "name": "Manual title", "body": existing})))
            .mount(&server).await;
        Mock::given(method("PATCH"))
            .and(path("/repos/owner/repo/releases/7"))
            .and(body_json(
                serde_json::json!({"tag_name": "v1", "body": expected}),
            ))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"id": 7, "tag_name": "v1"})),
            )
            .expect(1)
            .mount(&server)
            .await;
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/commits/refs/tags/v1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(
                serde_json::json!({"sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}),
            ))
            .expect(1)
            .mount(&server)
            .await;
        let gh = GitHubClient::with_base_url("token".into(), "owner/repo", server.uri()).unwrap();
        let notes = ParsedOutput {
            changelog: "Change".into(),
            release_title: "Generated title".into(),
            release_body: "New".into(),
            usage: Default::default(),
            review: Default::default(),
        };
        publish_with_client(
            Draft::new(
                "owner/repo",
                "v1",
                "v0",
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                &notes,
                true,
            ),
            true,
            &gh,
        )
        .await
        .unwrap();
        publish_with_client(
            Draft::new(
                "owner/repo",
                "v1",
                "v0",
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                &notes,
                true,
            ),
            false,
            &gh,
        )
        .await
        .unwrap();
        server.verify().await;
    }

    #[test]
    fn marked_sections_preserve_manual_text_exactly() {
        let old = format!("Install instructions\n\n{START}\nOld notes\n{END}\n\nThanks!");
        assert_eq!(
            merge_body(&old, "New notes").unwrap(),
            format!("Install instructions\n\n{START}\nNew notes\n{END}\n\nThanks!")
        );
        assert_eq!(
            merge_body("Handwritten", "New").unwrap(),
            format!("Handwritten\n\n{START}\nNew\n{END}")
        );
        for bad in [
            format!("{START} orphan"),
            format!("{END}{START}"),
            format!("{START}{START}{END}"),
        ] {
            assert!(merge_body(&bad, "New").is_err());
        }
        assert!(merge_body("", START).is_err());
    }

    #[test]
    fn coverage_does_not_invent_missing_assessments() {
        let mut review = Review::from_submission(&serde_json::json!({"coverage": [
            {"commit": "abc", "status": "included", "reason": "Described in Added"},
            {"commit": "fake", "status": "included", "reason": "Not a real commit"}
        ]}));
        review.reconcile(
            "abc Feature\ndef Fix",
            vec![Coverage {
                commit: "ghi".into(),
                status: "omitted".into(),
                reason: "Label exclusion".into(),
            }],
        );
        assert_eq!(review.coverage.len(), 3);
        assert_eq!(review.coverage[1].status, "uncertain");
        assert_eq!(review.coverage[2].reason, "Label exclusion");
        assert!(!review.markdown("owner/repo", "v1", "v2").contains("fake"));
    }

    #[test]
    fn package_defaults_and_cli_overrides() {
        let config: crate::config::Config = toml::from_str(
            r#"
[packages.cli]
paths = ["crates/cli"]
tag_pattern = "cli/v*"
channel = "stable"
changelog_path = "crates/cli/CHANGELOG.md"
"#,
        )
        .unwrap();
        let mut options = WorkflowOptions {
            package: Some("cli".into()),
            tag_pattern: Some("cli/v2.*".into()),
            ..Default::default()
        };
        options.resolve(&config).unwrap();
        assert_eq!(options.paths, ["crates/cli"]);
        assert_eq!(options.tag_pattern.as_deref(), Some("cli/v2.*"));
        options.paths = vec!["../elsewhere".into()];
        assert!(options.resolve(&config).is_err());
        options.package = Some("missing".into());
        assert!(options.resolve(&config).is_err());
    }

    #[test]
    fn draft_roundtrip_keeps_editorial_changes() {
        let notes = ParsedOutput {
            changelog: "- Change".into(),
            release_title: "Title".into(),
            release_body: "Body\n\n".into(),
            usage: Default::default(),
            review: Default::default(),
        };
        let mut draft = Draft::new(
            "owner/repo",
            "v1",
            "v0",
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
            &notes,
            false,
        );
        draft.release_body = "Hand-edited body\n\n".into();
        let serialized = serde_json::to_string(&draft).unwrap();
        let decoded: Draft = serde_json::from_str(&serialized).unwrap();
        decoded.validate().unwrap();
        assert_eq!(decoded.release_body, "Hand-edited body\n\n");
        draft.schema_version = 99;
        assert!(draft.validate().is_err());
        draft.schema_version = 1;
        draft.tag = "HEAD".into();
        assert!(draft.validate().is_err());
    }
}