Skip to content

Commit

Permalink
feat: allow signing commits
Browse files Browse the repository at this point in the history
closes #73
  • Loading branch information
its-danny committed Nov 24, 2023
1 parent 05d4b1a commit 66b9d9e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
27 changes: 24 additions & 3 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -53,6 +53,12 @@ struct Args {
help = "Enables issue prompt, which will append a reference to an issue in the commit body"
)]
issues: Option<bool>,

#[arg(
long,
help = "Sign the commit using the user's GPG key, if one is configured"
)]
sign: Option<bool>,
}

fn main() -> Result<()> {
Expand All @@ -64,6 +70,7 @@ fn main() -> Result<()> {
emoji,
hook,
issues,
sign,
} = Args::parse();

// Find repo
Expand All @@ -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)?;
Expand All @@ -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(())
Expand Down
8 changes: 4 additions & 4 deletions src/lib/answers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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()));
Expand All @@ -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()));
Expand Down Expand Up @@ -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!(
Expand Down
3 changes: 2 additions & 1 deletion src/lib/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn commit(
summary: String,
body: Option<String>,
is_breaking_change: bool,
sign: bool,
) -> Result<()> {
let cocogitto = CocoGitto::get()?;

Expand All @@ -47,7 +48,7 @@ pub fn commit(
body,
None,
is_breaking_change,
false,
sign,
)?;

Ok(())
Expand Down
48 changes: 36 additions & 12 deletions src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Config {
pub commit_types: IndexMap<String, CommitType>,
pub emoji: bool,
pub issues: bool,
pub sign: bool,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
Expand All @@ -27,17 +28,31 @@ struct ConfigTOML {
commit_types: Vec<CommitType>,
pub emoji: Option<bool>,
pub issues: Option<bool>,
pub sign: Option<bool>,
}

#[derive(Default)]
pub struct ConfigArgs {
pub path: Option<String>,
pub autocomplete: Option<bool>,
pub breaking_changes: Option<bool>,
pub emoji: Option<bool>,
pub issues: Option<bool>,
pub sign: Option<bool>,
}

impl Config {
/// Find a config and load it
pub fn new(
path: Option<String>,
autocomplete: Option<bool>,
breaking_changes: Option<bool>,
emoji: Option<bool>,
issues: Option<bool>,
) -> Result<Self> {
pub fn new(args: Option<ConfigArgs>) -> Result<Self> {
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 =
Expand Down Expand Up @@ -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)),
})
}
}
Expand All @@ -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!(
Expand Down
4 changes: 2 additions & 2 deletions src/lib/questions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 =
Expand Down

0 comments on commit 66b9d9e

Please sign in to comment.