From 66b9d9e42e8b44b895e535a8ceaf2d399f2fbbee Mon Sep 17 00:00:00 2001 From: Danny Tatom Date: Fri, 24 Nov 2023 11:04:58 -0800 Subject: [PATCH] feat: allow signing commits closes #73 --- src/bin/main.rs | 27 ++++++++++++++++++++++--- src/lib/answers.rs | 8 ++++---- src/lib/commit.rs | 3 ++- src/lib/config.rs | 48 +++++++++++++++++++++++++++++++++----------- src/lib/questions.rs | 4 ++-- 5 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index f4b326f..a90b691 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -7,7 +7,7 @@ use conventional_commit_parser::parse; use git2::Repository; use koji::answers::{get_extracted_answers, ExtractedAnswers}; use koji::commit::{commit, write_commit_msg}; -use koji::config::Config; +use koji::config::{Config, ConfigArgs}; use koji::questions::create_prompt; #[derive(Parser, Debug)] @@ -53,6 +53,12 @@ struct Args { help = "Enables issue prompt, which will append a reference to an issue in the commit body" )] issues: Option, + + #[arg( + long, + help = "Sign the commit using the user's GPG key, if one is configured" + )] + sign: Option, } fn main() -> Result<()> { @@ -64,6 +70,7 @@ fn main() -> Result<()> { emoji, hook, issues, + sign, } = Args::parse(); // Find repo @@ -84,7 +91,14 @@ fn main() -> Result<()> { } // Load config - let config = Config::new(config, autocomplete, breaking_changes, emoji, issues)?; + let config = Config::new(Some(ConfigArgs { + autocomplete, + breaking_changes, + emoji, + issues, + path: config, + sign, + }))?; // Get answers from interactive prompt let answers = create_prompt(&repo, commit_message, &config)?; @@ -102,7 +116,14 @@ fn main() -> Result<()> { if hook { write_commit_msg(repo, commit_type, scope, summary, body, is_breaking_change)?; } else { - commit(commit_type, scope, summary, body, is_breaking_change)?; + commit( + commit_type, + scope, + summary, + body, + is_breaking_change, + config.sign, + )?; } Ok(()) diff --git a/src/lib/answers.rs b/src/lib/answers.rs index 3b2e21e..669f87c 100644 --- a/src/lib/answers.rs +++ b/src/lib/answers.rs @@ -203,7 +203,7 @@ mod tests { #[test] fn test_get_summary() { - let config = Config::new(None, None, None, None, None).unwrap(); + let config = Config::new(None).unwrap(); let commit_types = config.commit_types; let answer = Some(Answer::String("needed more badges".into())); @@ -216,7 +216,7 @@ mod tests { #[test] fn test_get_summary_with_emoji() { - let config = Config::new(None, None, None, Some(true), None).unwrap(); + let config = Config::new(None).unwrap(); let commit_types = config.commit_types; let answer = Some(Answer::String("needed more badges".into())); @@ -229,7 +229,7 @@ mod tests { #[test] fn test_get_summary_with_shortcode() { - let config = Config::new(None, None, None, None, None).unwrap(); + let config = Config::new(None).unwrap(); let commit_types = config.commit_types; let answer = Some(Answer::String("needed more badges :badger:".into())); @@ -341,7 +341,7 @@ mod tests { ), ])); - let config = Config::new(None, None, None, None, None).unwrap(); + let config = Config::new(None).unwrap(); let extracted_answers = get_extracted_answers(&answers, &config).unwrap(); assert_eq!( diff --git a/src/lib/commit.rs b/src/lib/commit.rs index 3702166..2596906 100644 --- a/src/lib/commit.rs +++ b/src/lib/commit.rs @@ -37,6 +37,7 @@ pub fn commit( summary: String, body: Option, is_breaking_change: bool, + sign: bool, ) -> Result<()> { let cocogitto = CocoGitto::get()?; @@ -47,7 +48,7 @@ pub fn commit( body, None, is_breaking_change, - false, + sign, )?; Ok(()) diff --git a/src/lib/config.rs b/src/lib/config.rs index a582bb2..602771f 100644 --- a/src/lib/config.rs +++ b/src/lib/config.rs @@ -10,6 +10,7 @@ pub struct Config { pub commit_types: IndexMap, pub emoji: bool, pub issues: bool, + pub sign: bool, } #[derive(Clone, Debug, Deserialize, PartialEq, Eq)] @@ -27,17 +28,31 @@ struct ConfigTOML { commit_types: Vec, pub emoji: Option, pub issues: Option, + pub sign: Option, +} + +#[derive(Default)] +pub struct ConfigArgs { + pub path: Option, + pub autocomplete: Option, + pub breaking_changes: Option, + pub emoji: Option, + pub issues: Option, + pub sign: Option, } impl Config { /// Find a config and load it - pub fn new( - path: Option, - autocomplete: Option, - breaking_changes: Option, - emoji: Option, - issues: Option, - ) -> Result { + pub fn new(args: Option) -> Result { + let ConfigArgs { + path, + autocomplete, + breaking_changes, + emoji, + issues, + sign, + } = args.unwrap_or_default(); + // Get the default config let default_str = include_str!("../../meta/config/default.toml"); let default_config: ConfigTOML = @@ -92,6 +107,7 @@ impl Config { commit_types, emoji: emoji.unwrap_or(config.emoji.unwrap_or(false)), issues: issues.unwrap_or(config.issues.unwrap_or(true)), + sign: sign.unwrap_or(config.sign.unwrap_or(false)), }) } } @@ -102,25 +118,33 @@ mod tests { #[test] fn test_breaking_changes() { - let config = Config::new(None, None, None, None, None).unwrap(); + let config = Config::new(None).unwrap(); assert_eq!(config.breaking_changes, true); - let config = Config::new(None, None, Some(false), None, None).unwrap(); + let config = Config::new(Some(ConfigArgs { + breaking_changes: Some(false), + ..Default::default() + })) + .unwrap(); assert_eq!(config.breaking_changes, false); } #[test] fn test_issues() { - let config = Config::new(None, None, None, None, None).unwrap(); + let config = Config::new(None).unwrap(); assert_eq!(config.issues, true); - let config = Config::new(None, None, None, None, Some(false)).unwrap(); + let config = Config::new(Some(ConfigArgs { + issues: Some(false), + ..Default::default() + })) + .unwrap(); assert_eq!(config.issues, false); } #[test] fn test_commit_types() { - let config = Config::new(None, None, None, Some(true), None).unwrap(); + let config = Config::new(None).unwrap(); let commit_types = config.commit_types; assert_eq!( diff --git a/src/lib/questions.rs b/src/lib/questions.rs index aa1c4a6..00b9c73 100644 --- a/src/lib/questions.rs +++ b/src/lib/questions.rs @@ -199,7 +199,7 @@ mod tests { #[test] fn test_format_commit_type_choice() { - let config = Config::new(None, None, None, None, None).unwrap(); + let config = Config::new(None).unwrap(); let commit_types = config.commit_types; let choice = @@ -213,7 +213,7 @@ mod tests { #[test] fn test_render_commit_type_choice_with_emoji() { - let config = Config::new(None, None, None, None, None).unwrap(); + let config = Config::new(None).unwrap(); let commit_types = config.commit_types; let choice =