From 39ad36115d368d79d4723b3a6b1cefe4874813a3 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 1 Nov 2023 00:00:00 +0000 Subject: feat(prs-create) add pr tag branch-name and title tag pr event with title, description and the name of the current checkedout branch --- src/git.rs | 31 +++++++++++++++++++++++++++++++ src/sub_commands/prs/create.rs | 20 ++++++++++++++------ 2 files changed, 45 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/git.rs b/src/git.rs index 337444a..f9121f2 100644 --- a/src/git.rs +++ b/src/git.rs @@ -30,6 +30,7 @@ impl Repo { pub trait RepoActions { fn get_local_branch_names(&self) -> Result>; fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)>; + fn get_checked_out_branch_name(&self) -> Result; fn get_tip_of_local_branch(&self, branch_name: &str) -> Result; fn get_root_commit(&self, branch_name: &str) -> Result; fn get_head_commit(&self) -> Result; @@ -83,6 +84,15 @@ impl RepoActions for Repo { Ok(branch_names) } + fn get_checked_out_branch_name(&self) -> Result { + Ok(self + .git_repo + .head()? + .shorthand() + .context("an object without a shorthand is checked out")? + .to_string()) + } + fn get_tip_of_local_branch(&self, branch_name: &str) -> Result { let branch = self .git_repo @@ -378,6 +388,27 @@ mod tests { } } + mod get_checked_out_branch_name { + use super::*; + + #[test] + fn returns_checked_out_branch_name() -> Result<()> { + let test_repo = GitTestRepo::default(); + let _ = test_repo.populate()?; + // create feature branch + test_repo.create_branch("example-feature")?; + test_repo.checkout("example-feature")?; + + let git_repo = Repo::from_path(&test_repo.dir)?; + + assert_eq!( + git_repo.get_checked_out_branch_name()?, + "example-feature".to_string() + ); + Ok(()) + } + } + mod get_commits_ahead_behind { use super::*; mod returns_main { diff --git a/src/sub_commands/prs/create.rs b/src/sub_commands/prs/create.rs index 655978e..ce30c12 100644 --- a/src/sub_commands/prs/create.rs +++ b/src/sub_commands/prs/create.rs @@ -313,15 +313,23 @@ fn generate_pr_and_patch_events( .get_root_commit(to_branch) .context("failed to get root commit of the repository")?; + let mut pr_tags = vec![ + Tag::Reference(format!("r-{root_commit}")), + Tag::Name(title.to_string()), + Tag::Description(description.to_string()), + ]; + + if let Ok(branch_name) = git_repo.get_checked_out_branch_name() { + pr_tags.push(Tag::Generic( + TagKind::Custom("branch-name".to_string()), + vec![branch_name], + )); + } + let pr_event = EventBuilder::new( nostr::event::Kind::Custom(PR_KIND), format!("{title}\r\n\r\n{description}"), - &[Tag::Reference(format!("r-{root_commit}"))], - // TODO: suggested branch name - // Tag::Generic( - // TagKind::Custom("suggested-branch-name".to_string()), - // vec![], - // ), + &pr_tags, // TODO: add Repo event as root // TODO: people tag maintainers // TODO: add relay tags -- cgit v1.2.3