feat: with git username and email in config
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
1e15e41354
commit
5229d30758
@ -89,7 +89,7 @@ impl Command {
|
||||
|
||||
pub fn execute(self, current_dir: Option<&Path>) -> anyhow::Result<()> {
|
||||
let config = self.build_config(current_dir)?;
|
||||
let git_client = self.get_git(config.get_source())?;
|
||||
let git_client = self.get_git(&config)?;
|
||||
let gitea_client = self.get_gitea_client(&config);
|
||||
|
||||
match &self.commands {
|
||||
@ -131,11 +131,15 @@ impl Command {
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
fn get_git(&self, current_dir: &Path) -> anyhow::Result<VcsClient> {
|
||||
fn get_git(&self, config: &PleaseConfig) -> anyhow::Result<VcsClient> {
|
||||
if self.global.no_vcs {
|
||||
Ok(VcsClient::new_noop())
|
||||
} else {
|
||||
VcsClient::new_git(current_dir)
|
||||
VcsClient::new_git(
|
||||
config.get_source(),
|
||||
config.settings.git_username.clone(),
|
||||
config.settings.git_email.clone(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,6 +52,24 @@ pub struct ConfigArgs {
|
||||
help_heading = "Config"
|
||||
)]
|
||||
pub branch: Option<String>,
|
||||
|
||||
/// which git username to use for commits
|
||||
#[arg(
|
||||
env = "CUDDLE_PLEASE_GIT_USERNAME",
|
||||
long,
|
||||
global = true,
|
||||
help_heading = "Config"
|
||||
)]
|
||||
pub git_username: Option<String>,
|
||||
|
||||
/// which git email to use for commits
|
||||
#[arg(
|
||||
env = "CUDDLE_PLEASE_GIT_EMAIL",
|
||||
long,
|
||||
global = true,
|
||||
help_heading = "Config"
|
||||
)]
|
||||
pub git_email: Option<String>,
|
||||
}
|
||||
|
||||
impl From<ConfigArgs> for PleaseConfigBuilder {
|
||||
@ -65,6 +83,8 @@ impl From<ConfigArgs> for PleaseConfigBuilder {
|
||||
}),
|
||||
settings: Some(PleaseSettingsConfigBuilder {
|
||||
api_url: value.api_url,
|
||||
git_username: value.git_username,
|
||||
git_email: value.git_email,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ pub struct PleaseProjectConfig {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PleaseSettingsConfig {
|
||||
pub api_url: String,
|
||||
pub git_username: Option<String>,
|
||||
pub git_email: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -43,6 +45,12 @@ impl PleaseConfig {
|
||||
pub fn get_api_url(&self) -> &str {
|
||||
&self.settings.api_url
|
||||
}
|
||||
pub fn get_git_username(&self) -> Option<String> {
|
||||
self.settings.git_username.clone()
|
||||
}
|
||||
pub fn get_git_email(&self) -> Option<String> {
|
||||
self.settings.git_email.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for PleaseConfig {
|
||||
@ -52,6 +60,12 @@ impl Display for PleaseConfig {
|
||||
writeln!(f, " repository: {}", self.get_repository())?;
|
||||
writeln!(f, " branch: {}", self.get_branch())?;
|
||||
writeln!(f, " api_url: {}", self.get_api_url())?;
|
||||
if let Some(git_username) = self.get_git_username() {
|
||||
writeln!(f, " git_username: {}", git_username)?;
|
||||
}
|
||||
if let Some(git_email) = self.get_git_email() {
|
||||
writeln!(f, " git_email: {}", git_email)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -15,6 +15,8 @@ pub struct PleaseProjectConfigBuilder {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct PleaseSettingsConfigBuilder {
|
||||
pub api_url: Option<String>,
|
||||
pub git_username: Option<String>,
|
||||
pub git_email: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
@ -56,6 +58,13 @@ impl PleaseConfigBuilder {
|
||||
fsettings.api_url = Some(api_url);
|
||||
}
|
||||
|
||||
if let Some(git_username) = settings.git_username {
|
||||
fsettings.git_username = Some(git_username);
|
||||
}
|
||||
if let Some(git_email) = settings.git_email {
|
||||
fsettings.git_email = Some(git_email);
|
||||
}
|
||||
|
||||
self.settings = Some(fsettings);
|
||||
}
|
||||
|
||||
@ -99,6 +108,8 @@ impl TryFrom<PleaseSettingsConfigBuilder> for PleaseSettingsConfig {
|
||||
fn try_from(value: PleaseSettingsConfigBuilder) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
api_url: value.api_url.ok_or(value_is_missing("api_url"))?,
|
||||
git_username: value.git_username.clone(),
|
||||
git_email: value.git_username.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,11 @@ use std::path::{Path, PathBuf};
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum VcsClient {
|
||||
Noop {},
|
||||
Git { source: PathBuf },
|
||||
Git {
|
||||
source: PathBuf,
|
||||
username: String,
|
||||
email: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl VcsClient {
|
||||
@ -11,13 +15,23 @@ impl VcsClient {
|
||||
Self::Noop {}
|
||||
}
|
||||
|
||||
pub fn new_git(path: &Path) -> anyhow::Result<VcsClient> {
|
||||
pub fn new_git(
|
||||
path: &Path,
|
||||
git_username: Option<impl Into<String>>,
|
||||
git_email: Option<impl Into<String>>,
|
||||
) -> anyhow::Result<VcsClient> {
|
||||
if !path.to_path_buf().join(".git").exists() {
|
||||
anyhow::bail!("git directory not found in: {}", path.display().to_string())
|
||||
}
|
||||
|
||||
Ok(Self::Git {
|
||||
source: path.to_path_buf(),
|
||||
username: git_username
|
||||
.map(|u| u.into())
|
||||
.unwrap_or("cuddle-please".to_string()),
|
||||
email: git_email
|
||||
.map(|e| e.into())
|
||||
.unwrap_or("bot@cuddle.sh".to_string()),
|
||||
})
|
||||
}
|
||||
|
||||
@ -38,7 +52,7 @@ impl VcsClient {
|
||||
fn exec_git(&self, args: &[&str]) -> anyhow::Result<()> {
|
||||
match self {
|
||||
VcsClient::Noop {} => {}
|
||||
VcsClient::Git { source } => {
|
||||
VcsClient::Git { source, .. } => {
|
||||
let checkout_branch = std::process::Command::new("git")
|
||||
.current_dir(source.as_path())
|
||||
.args(args)
|
||||
|
@ -17,7 +17,7 @@ fn exec_git_into_branch() {
|
||||
add_commit(tempdir.path(), "second").unwrap();
|
||||
add_tag(tempdir.path(), "1.0.1").unwrap();
|
||||
|
||||
let vcs = VcsClient::new_git(tempdir.path()).unwrap();
|
||||
let vcs = VcsClient::new_git(tempdir.path(), None::<String>, None::<String>).unwrap();
|
||||
vcs.checkout_branch().unwrap();
|
||||
|
||||
let output = std::process::Command::new("git")
|
||||
@ -48,7 +48,7 @@ fn add_files_to_commit() {
|
||||
add_commit(tempdir.path(), "first").unwrap();
|
||||
add_tag(tempdir.path(), "1.0.0").unwrap();
|
||||
|
||||
let vcs = VcsClient::new_git(tempdir.path()).unwrap();
|
||||
let vcs = VcsClient::new_git(tempdir.path(), None::<String>, None::<String>).unwrap();
|
||||
vcs.checkout_branch().unwrap();
|
||||
|
||||
std::fs::File::create(tempdir.path().join("changelog")).unwrap();
|
||||
@ -76,7 +76,7 @@ fn reset_branch() {
|
||||
add_commit(tempdir.path(), "first").unwrap();
|
||||
add_tag(tempdir.path(), "1.0.0").unwrap();
|
||||
|
||||
let vcs = VcsClient::new_git(tempdir.path()).unwrap();
|
||||
let vcs = VcsClient::new_git(tempdir.path(), None::<String>, None::<String>).unwrap();
|
||||
vcs.checkout_branch().unwrap();
|
||||
|
||||
std::fs::File::create(tempdir.path().join("changelog-first")).unwrap();
|
||||
|
@ -16,6 +16,13 @@ fn test_vcs_get_noop() {
|
||||
#[traced_test]
|
||||
fn test_vcs_get_git_found() {
|
||||
let testdata = get_test_data_path("git-found");
|
||||
let git = VcsClient::new_git(&testdata).unwrap();
|
||||
assert_eq!(git, VcsClient::Git { source: testdata })
|
||||
let git = VcsClient::new_git(&testdata, None::<String>, None::<String>).unwrap();
|
||||
assert_eq!(
|
||||
git,
|
||||
VcsClient::Git {
|
||||
source: testdata,
|
||||
username: "cuddle-please".into(),
|
||||
email: "bot@cuddle.sh".into()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user