|
| 1 | +use anyhow::{Result, bail}; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use super::shell::run_git_with_env; |
| 5 | + |
| 6 | +const ACTIONS_BOT_NAME: &str = "github-actions[bot]"; |
| 7 | +const ACTIONS_BOT_EMAIL: &str = "41898282+github-actions[bot]@users.noreply.github.com"; |
| 8 | + |
| 9 | +pub fn on_github_actions() -> bool { |
| 10 | + std::env::var("GITHUB_ACTIONS").is_ok_and(|v| v.trim().eq_ignore_ascii_case("true")) |
| 11 | +} |
| 12 | + |
| 13 | +pub fn commit_identity(workdir: &Path) -> Option<String> { |
| 14 | + commit_identity_with_env(workdir, &[]) |
| 15 | +} |
| 16 | + |
| 17 | +pub fn ensure_commit_identity(workdir: &Path) -> Result<()> { |
| 18 | + ensure_commit_identity_with_env(workdir, on_github_actions(), &[]) |
| 19 | +} |
| 20 | + |
| 21 | +fn commit_identity_with_env(workdir: &Path, env: &[(&str, &str)]) -> Option<String> { |
| 22 | + let author = run_git_with_env(workdir, &["var", "GIT_AUTHOR_IDENT"], env).ok()?; |
| 23 | + run_git_with_env(workdir, &["var", "GIT_COMMITTER_IDENT"], env).ok()?; |
| 24 | + let ident = author.trim(); |
| 25 | + let name_and_email = ident.rsplitn(3, ' ').nth(2).unwrap_or(ident); |
| 26 | + Some(name_and_email.to_string()) |
| 27 | +} |
| 28 | + |
| 29 | +fn ensure_commit_identity_with_env( |
| 30 | + workdir: &Path, |
| 31 | + on_actions: bool, |
| 32 | + env: &[(&str, &str)], |
| 33 | +) -> Result<()> { |
| 34 | + if commit_identity_with_env(workdir, env).is_some() { |
| 35 | + return Ok(()); |
| 36 | + } |
| 37 | + |
| 38 | + if !on_actions { |
| 39 | + bail!( |
| 40 | + "no git identity to author the release commit and tags.\n\ |
| 41 | + Set one before releasing:\n \ |
| 42 | + git config user.name \"Your Name\"\n \ |
| 43 | + git config user.email \"you@example.com\"" |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + tracing::info!("No git identity configured, committing as {ACTIONS_BOT_NAME}."); |
| 48 | + run_git_with_env( |
| 49 | + workdir, |
| 50 | + &["config", "--local", "user.name", ACTIONS_BOT_NAME], |
| 51 | + env, |
| 52 | + )?; |
| 53 | + run_git_with_env( |
| 54 | + workdir, |
| 55 | + &["config", "--local", "user.email", ACTIONS_BOT_EMAIL], |
| 56 | + env, |
| 57 | + )?; |
| 58 | + Ok(()) |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::*; |
| 64 | + |
| 65 | + struct IsolatedRepo { |
| 66 | + dir: tempfile::TempDir, |
| 67 | + global_config: String, |
| 68 | + } |
| 69 | + |
| 70 | + impl IsolatedRepo { |
| 71 | + fn new() -> Self { |
| 72 | + let dir = tempfile::tempdir().unwrap(); |
| 73 | + let global = dir.path().join("empty-global.gitconfig"); |
| 74 | + std::fs::write(&global, "").unwrap(); |
| 75 | + let repo = Self { |
| 76 | + global_config: global.to_string_lossy().into_owned(), |
| 77 | + dir, |
| 78 | + }; |
| 79 | + repo.git(&["init", "-q"]); |
| 80 | + repo.git(&["config", "--local", "user.useConfigOnly", "true"]); |
| 81 | + repo |
| 82 | + } |
| 83 | + |
| 84 | + fn env(&self) -> [(&str, &str); 2] { |
| 85 | + [ |
| 86 | + ("GIT_CONFIG_GLOBAL", self.global_config.as_str()), |
| 87 | + ("GIT_CONFIG_NOSYSTEM", "1"), |
| 88 | + ] |
| 89 | + } |
| 90 | + |
| 91 | + fn git(&self, args: &[&str]) -> String { |
| 92 | + run_git_with_env(self.dir.path(), args, &self.env()).unwrap() |
| 93 | + } |
| 94 | + |
| 95 | + fn local_config(&self, key: &str) -> Option<String> { |
| 96 | + run_git_with_env( |
| 97 | + self.dir.path(), |
| 98 | + &["config", "--local", "--get", key], |
| 99 | + &self.env(), |
| 100 | + ) |
| 101 | + .ok() |
| 102 | + .map(|v| v.trim().to_string()) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn an_unconfigured_repo_has_no_identity() { |
| 108 | + let repo = IsolatedRepo::new(); |
| 109 | + assert_eq!(commit_identity_with_env(repo.dir.path(), &repo.env()), None); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn a_configured_identity_is_reported_without_the_timestamp() { |
| 114 | + let repo = IsolatedRepo::new(); |
| 115 | + repo.git(&["config", "--local", "user.name", "Jane Dev"]); |
| 116 | + repo.git(&["config", "--local", "user.email", "jane@example.com"]); |
| 117 | + assert_eq!( |
| 118 | + commit_identity_with_env(repo.dir.path(), &repo.env()).as_deref(), |
| 119 | + Some("Jane Dev <jane@example.com>") |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn a_name_without_an_email_is_not_an_identity() { |
| 125 | + let repo = IsolatedRepo::new(); |
| 126 | + repo.git(&["config", "--local", "user.name", "Jane Dev"]); |
| 127 | + assert_eq!(commit_identity_with_env(repo.dir.path(), &repo.env()), None); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn on_actions_a_missing_identity_falls_back_to_github_actions_bot() { |
| 132 | + let repo = IsolatedRepo::new(); |
| 133 | + ensure_commit_identity_with_env(repo.dir.path(), true, &repo.env()).unwrap(); |
| 134 | + |
| 135 | + assert_eq!( |
| 136 | + repo.local_config("user.name").as_deref(), |
| 137 | + Some("github-actions[bot]") |
| 138 | + ); |
| 139 | + assert_eq!( |
| 140 | + repo.local_config("user.email").as_deref(), |
| 141 | + Some("41898282+github-actions[bot]@users.noreply.github.com") |
| 142 | + ); |
| 143 | + repo.git(&[ |
| 144 | + "commit", |
| 145 | + "-q", |
| 146 | + "--allow-empty", |
| 147 | + "-m", |
| 148 | + "chore(release): v1.0.0", |
| 149 | + ]); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn on_actions_an_existing_identity_is_left_alone() { |
| 154 | + let repo = IsolatedRepo::new(); |
| 155 | + repo.git(&["config", "--local", "user.name", "ferrflow[bot]"]); |
| 156 | + repo.git(&[ |
| 157 | + "config", |
| 158 | + "--local", |
| 159 | + "user.email", |
| 160 | + "278126555+ferrflow[bot]@users.noreply.github.com", |
| 161 | + ]); |
| 162 | + |
| 163 | + ensure_commit_identity_with_env(repo.dir.path(), true, &repo.env()).unwrap(); |
| 164 | + |
| 165 | + assert_eq!( |
| 166 | + repo.local_config("user.name").as_deref(), |
| 167 | + Some("ferrflow[bot]") |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn off_actions_a_missing_identity_is_an_actionable_error_and_writes_nothing() { |
| 173 | + let repo = IsolatedRepo::new(); |
| 174 | + let err = ensure_commit_identity_with_env(repo.dir.path(), false, &repo.env()) |
| 175 | + .unwrap_err() |
| 176 | + .to_string(); |
| 177 | + |
| 178 | + assert!(err.contains("git config user.email"), "{err}"); |
| 179 | + assert_eq!(repo.local_config("user.name"), None); |
| 180 | + assert_eq!(repo.local_config("user.email"), None); |
| 181 | + } |
| 182 | +} |
0 commit comments