|
| 1 | +use anyhow::{bail, Context, Result}; |
| 2 | +use serde::Deserialize; |
| 3 | +use std::collections::HashMap; |
| 4 | +use std::fs; |
| 5 | +use std::io; |
| 6 | +use std::io::Write; |
| 7 | +use structopt::StructOpt; |
| 8 | + |
| 9 | +fn main() -> Result<()> { |
| 10 | + Cli::from_args().run() |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(StructOpt)] |
| 14 | +pub struct Cli { |
| 15 | + /// From commit. |
| 16 | + from: String, |
| 17 | + |
| 18 | + /// To commit. |
| 19 | + #[structopt(default_value = "HEAD")] |
| 20 | + to: String, |
| 21 | + |
| 22 | + #[structopt(skip = Self::open_repository())] |
| 23 | + repo: git2::Repository, |
| 24 | + |
| 25 | + #[structopt(skip)] |
| 26 | + github_users: GitHubUsers, |
| 27 | + |
| 28 | + #[structopt(skip = regex::Regex::new(r"\(#(\d+)\)").unwrap())] |
| 29 | + re_issue: regex::Regex, |
| 30 | +} |
| 31 | + |
| 32 | +impl Cli { |
| 33 | + fn open_repository() -> git2::Repository { |
| 34 | + match git2::Repository::open(".") { |
| 35 | + Err(err) => { |
| 36 | + eprintln!("Error: could not open repository: {}", err); |
| 37 | + std::process::exit(1); |
| 38 | + } |
| 39 | + Ok(repo) => repo, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + fn run(&mut self) -> Result<()> { |
| 44 | + let mut old_changelog = |
| 45 | + fs::File::open("CHANGELOG.md").context("could not open CHANGELOG.md for reading")?; |
| 46 | + let mut f = fs::OpenOptions::new() |
| 47 | + .write(true) |
| 48 | + .create(true) |
| 49 | + .truncate(true) |
| 50 | + .open("CHANGELOG.md.new") |
| 51 | + .context("could not open CHANGELOG.md.new for writing")?; |
| 52 | + |
| 53 | + let mut revwalk = self.repo.revwalk()?; |
| 54 | + revwalk.set_sorting(git2::Sort::TOPOLOGICAL)?; |
| 55 | + |
| 56 | + let from_object = self |
| 57 | + .repo |
| 58 | + .revparse_single(&self.from) |
| 59 | + .context("Could not find `from` revision")?; |
| 60 | + let to_object = self |
| 61 | + .repo |
| 62 | + .revparse_single(&self.to) |
| 63 | + .context("Could not find `to` revision")?; |
| 64 | + revwalk.hide(from_object.id())?; |
| 65 | + revwalk.push(to_object.id())?; |
| 66 | + |
| 67 | + let mut logs = Vec::new(); |
| 68 | + for oid in revwalk { |
| 69 | + let oid = oid?; |
| 70 | + let commit = self.repo.find_commit(oid)?; |
| 71 | + let first_line = commit |
| 72 | + .message() |
| 73 | + .context("Invalid UTF-8 in commit message")? |
| 74 | + .lines() |
| 75 | + .next() |
| 76 | + .context("Missing commit message")?; |
| 77 | + let author = commit.author(); |
| 78 | + let email = author.email().context("Missing author's email")?; |
| 79 | + |
| 80 | + if email.contains("dependabot") { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + let issue = |
| 85 | + if let Some(issue) = self.re_issue.captures(first_line).map(|x| x[1].to_string()) { |
| 86 | + issue |
| 87 | + } else { |
| 88 | + eprintln!("Missing issue for commit: {}", oid); |
| 89 | + continue; |
| 90 | + }; |
| 91 | + |
| 92 | + let user = self |
| 93 | + .github_users |
| 94 | + .find_user_by_commit_author(email, oid.to_string()) |
| 95 | + .with_context(|| format!("Could not find GitHub user for commit: {}", oid))?; |
| 96 | + |
| 97 | + logs.push((first_line.to_owned(), user.to_owned(), issue.to_owned())); |
| 98 | + } |
| 99 | + |
| 100 | + let (features, fixes): (Vec<_>, Vec<_>) = logs |
| 101 | + .into_iter() |
| 102 | + .partition(|(msg, _, _)| msg.to_lowercase().contains("fix")); |
| 103 | + |
| 104 | + writeln!( |
| 105 | + f, |
| 106 | + "## ✨ **x.y.z** *({})*", |
| 107 | + chrono::Utc::now().format("%Y-%m-%d") |
| 108 | + )?; |
| 109 | + writeln!(f)?; |
| 110 | + writeln!(f, "#### Changelog")?; |
| 111 | + writeln!(f)?; |
| 112 | + |
| 113 | + writeln!(f, "- #### 🛠 Fixes")?; |
| 114 | + writeln!(f)?; |
| 115 | + for (msg, user, issue) in fixes { |
| 116 | + writeln!( |
| 117 | + f, |
| 118 | + " - {msg}. [[@{user}] [#{issue}]](https://github.com/yewstack/yew/pull/{issue})", |
| 119 | + msg = msg, |
| 120 | + user = user, |
| 121 | + issue = issue |
| 122 | + )?; |
| 123 | + } |
| 124 | + |
| 125 | + writeln!(f, "- #### ⚡️ Features")?; |
| 126 | + writeln!(f)?; |
| 127 | + for (msg, user, issue) in features { |
| 128 | + writeln!( |
| 129 | + f, |
| 130 | + " - {msg}. [[@{user}] [#{issue}]](https://github.com/yewstack/yew/pull/{issue})", |
| 131 | + msg = msg, |
| 132 | + user = user, |
| 133 | + issue = issue |
| 134 | + )?; |
| 135 | + } |
| 136 | + |
| 137 | + writeln!(f)?; |
| 138 | + io::copy(&mut old_changelog, &mut f)?; |
| 139 | + |
| 140 | + drop(old_changelog); |
| 141 | + drop(f); |
| 142 | + |
| 143 | + fs::remove_file("CHANGELOG.md").context("Could not delete CHANGELOG.md")?; |
| 144 | + fs::rename("CHANGELOG.md.new", "CHANGELOG.md") |
| 145 | + .context("Could not replace CHANGELOG.md with CHANGELOG.md.new")?; |
| 146 | + |
| 147 | + Ok(()) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +#[derive(Debug, Default)] |
| 152 | +pub struct GitHubUsers { |
| 153 | + cache: HashMap<String, Option<String>>, |
| 154 | +} |
| 155 | + |
| 156 | +impl GitHubUsers { |
| 157 | + pub fn find_user_by_commit_author( |
| 158 | + &mut self, |
| 159 | + key: impl Into<String>, |
| 160 | + commit: impl AsRef<str>, |
| 161 | + ) -> Option<&str> { |
| 162 | + self.cache |
| 163 | + .entry(key.into()) |
| 164 | + .or_insert_with(|| match Self::query_commit(commit) { |
| 165 | + Ok(value) => value, |
| 166 | + Err(err) => { |
| 167 | + eprintln!("Error: {}", err); |
| 168 | + None |
| 169 | + } |
| 170 | + }) |
| 171 | + .as_deref() |
| 172 | + } |
| 173 | + |
| 174 | + fn query_commit(q: impl AsRef<str>) -> Result<Option<String>> { |
| 175 | + std::thread::sleep(std::time::Duration::from_secs(1)); |
| 176 | + let client = reqwest::blocking::Client::new(); |
| 177 | + let resp = client |
| 178 | + .get(format!( |
| 179 | + "https://api.github.com/repos/yewstack/yew/commits/{}", |
| 180 | + q.as_ref(), |
| 181 | + )) |
| 182 | + .header("user-agent", "reqwest") |
| 183 | + .header("accept", "application/vnd.github.v3+json") |
| 184 | + .send()?; |
| 185 | + let status = resp.status(); |
| 186 | + if !status.is_success() { |
| 187 | + if let Some(remaining) = resp.headers().get("x-ratelimit-remaining") { |
| 188 | + if remaining == "0" { |
| 189 | + bail!("GitHub API limit reached."); |
| 190 | + } |
| 191 | + } |
| 192 | + bail!("GitHub API request error: {}", status); |
| 193 | + } |
| 194 | + let body = resp.json::<GitHubCommitApi>()?; |
| 195 | + |
| 196 | + Ok(Some(body.author.login)) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +#[derive(Deserialize, Debug)] |
| 201 | +pub struct GitHubCommitApi { |
| 202 | + author: GitHubCommitAuthorApi, |
| 203 | +} |
| 204 | + |
| 205 | +#[derive(Deserialize, Debug)] |
| 206 | +pub struct GitHubCommitAuthorApi { |
| 207 | + login: String, |
| 208 | +} |
0 commit comments