|
| 1 | +mod notes; |
| 2 | + |
| 3 | +use crate::flags; |
| 4 | +use anyhow::{anyhow, bail, Result}; |
| 5 | +use std::env; |
| 6 | +use xshell::{cmd, Shell}; |
| 7 | + |
| 8 | +impl flags::PublishReleaseNotes { |
| 9 | + pub(crate) fn run(self, sh: &Shell) -> Result<()> { |
| 10 | + let asciidoc = sh.read_file(&self.changelog)?; |
| 11 | + let mut markdown = notes::convert_asciidoc_to_markdown(std::io::Cursor::new(&asciidoc))?; |
| 12 | + let file_name = check_file_name(self.changelog)?; |
| 13 | + let tag_name = &file_name[0..10]; |
| 14 | + let original_changelog_url = create_original_changelog_url(&file_name); |
| 15 | + let additional_paragraph = |
| 16 | + format!("\nSee also [original changelog]({original_changelog_url})."); |
| 17 | + markdown.push_str(&additional_paragraph); |
| 18 | + if self.dry_run { |
| 19 | + println!("{markdown}"); |
| 20 | + } else { |
| 21 | + update_release(sh, tag_name, &markdown)?; |
| 22 | + } |
| 23 | + Ok(()) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn check_file_name<P: AsRef<std::path::Path>>(path: P) -> Result<String> { |
| 28 | + let file_name = path |
| 29 | + .as_ref() |
| 30 | + .file_name() |
| 31 | + .ok_or_else(|| anyhow!("file name is not specified as `changelog`"))? |
| 32 | + .to_string_lossy(); |
| 33 | + |
| 34 | + let mut chars = file_name.chars(); |
| 35 | + if file_name.len() >= 10 |
| 36 | + && chars.next().unwrap().is_ascii_digit() |
| 37 | + && chars.next().unwrap().is_ascii_digit() |
| 38 | + && chars.next().unwrap().is_ascii_digit() |
| 39 | + && chars.next().unwrap().is_ascii_digit() |
| 40 | + && chars.next().unwrap() == '-' |
| 41 | + && chars.next().unwrap().is_ascii_digit() |
| 42 | + && chars.next().unwrap().is_ascii_digit() |
| 43 | + && chars.next().unwrap() == '-' |
| 44 | + && chars.next().unwrap().is_ascii_digit() |
| 45 | + && chars.next().unwrap().is_ascii_digit() |
| 46 | + { |
| 47 | + Ok(file_name.to_string()) |
| 48 | + } else { |
| 49 | + bail!("unexpected file name format; no date information prefixed") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn create_original_changelog_url(file_name: &str) -> String { |
| 54 | + let year = &file_name[0..4]; |
| 55 | + let month = &file_name[5..7]; |
| 56 | + let day = &file_name[8..10]; |
| 57 | + let mut stem = &file_name[11..]; |
| 58 | + if let Some(stripped) = stem.strip_suffix(".adoc") { |
| 59 | + stem = stripped; |
| 60 | + } |
| 61 | + format!("https://rust-analyzer.github.io/thisweek/{year}/{month}/{day}/{stem}.html") |
| 62 | +} |
| 63 | + |
| 64 | +fn update_release(sh: &Shell, tag_name: &str, release_notes: &str) -> Result<()> { |
| 65 | + let token = match env::var("GITHUB_TOKEN") { |
| 66 | + Ok(token) => token, |
| 67 | + Err(_) => bail!("Please obtain a personal access token from https://github.com/settings/tokens and set the `GITHUB_TOKEN` environment variable."), |
| 68 | + }; |
| 69 | + let accept = "Accept: application/vnd.github+json"; |
| 70 | + let authorization = format!("Authorization: Bearer {token}"); |
| 71 | + let api_version = "X-GitHub-Api-Version: 2022-11-28"; |
| 72 | + let release_url = "https://api.github.com/repos/rust-lang/rust-analyzer/releases"; |
| 73 | + |
| 74 | + let release_json = cmd!( |
| 75 | + sh, |
| 76 | + "curl -sf -H {accept} -H {authorization} -H {api_version} {release_url}/tags/{tag_name}" |
| 77 | + ) |
| 78 | + .read()?; |
| 79 | + let release_id = cmd!(sh, "jq .id").stdin(release_json).read()?; |
| 80 | + |
| 81 | + let mut patch = String::new(); |
| 82 | + write_json::object(&mut patch) |
| 83 | + .string("tag_name", tag_name) |
| 84 | + .string("target_commitish", "master") |
| 85 | + .string("name", tag_name) |
| 86 | + .string("body", release_notes) |
| 87 | + .bool("draft", false) |
| 88 | + .bool("prerelease", false); |
| 89 | + let _ = cmd!( |
| 90 | + sh, |
| 91 | + "curl -sf -X PATCH -H {accept} -H {authorization} -H {api_version} {release_url}/{release_id} -d {patch}" |
| 92 | + ) |
| 93 | + .read()?; |
| 94 | + |
| 95 | + Ok(()) |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use super::*; |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn original_changelog_url_creation() { |
| 104 | + let input = "2019-07-24-changelog-0.adoc"; |
| 105 | + let actual = create_original_changelog_url(input); |
| 106 | + let expected = "https://rust-analyzer.github.io/thisweek/2019/07/24/changelog-0.html"; |
| 107 | + assert_eq!(actual, expected); |
| 108 | + } |
| 109 | +} |
0 commit comments