upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2023-11-01 00:00:00 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2023-11-01 00:00:00 +0000
commit39ad36115d368d79d4723b3a6b1cefe4874813a3 (patch)
treea74ed0c389c46f7be90877d20bc705e0180a0686
parent6672f54dc166d7447e5c799f1809ecbb3d9ec04c (diff)
feat(prs-create) add pr tag branch-name and title
tag pr event with title, description and the name of the current checkedout branch
-rw-r--r--src/git.rs31
-rw-r--r--src/sub_commands/prs/create.rs20
-rw-r--r--tests/prs_create.rs77
3 files changed, 120 insertions, 8 deletions
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 {
30pub trait RepoActions { 30pub trait RepoActions {
31 fn get_local_branch_names(&self) -> Result<Vec<String>>; 31 fn get_local_branch_names(&self) -> Result<Vec<String>>;
32 fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)>; 32 fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)>;
33 fn get_checked_out_branch_name(&self) -> Result<String>;
33 fn get_tip_of_local_branch(&self, branch_name: &str) -> Result<Sha1Hash>; 34 fn get_tip_of_local_branch(&self, branch_name: &str) -> Result<Sha1Hash>;
34 fn get_root_commit(&self, branch_name: &str) -> Result<Sha1Hash>; 35 fn get_root_commit(&self, branch_name: &str) -> Result<Sha1Hash>;
35 fn get_head_commit(&self) -> Result<Sha1Hash>; 36 fn get_head_commit(&self) -> Result<Sha1Hash>;
@@ -83,6 +84,15 @@ impl RepoActions for Repo {
83 Ok(branch_names) 84 Ok(branch_names)
84 } 85 }
85 86
87 fn get_checked_out_branch_name(&self) -> Result<String> {
88 Ok(self
89 .git_repo
90 .head()?
91 .shorthand()
92 .context("an object without a shorthand is checked out")?
93 .to_string())
94 }
95
86 fn get_tip_of_local_branch(&self, branch_name: &str) -> Result<Sha1Hash> { 96 fn get_tip_of_local_branch(&self, branch_name: &str) -> Result<Sha1Hash> {
87 let branch = self 97 let branch = self
88 .git_repo 98 .git_repo
@@ -378,6 +388,27 @@ mod tests {
378 } 388 }
379 } 389 }
380 390
391 mod get_checked_out_branch_name {
392 use super::*;
393
394 #[test]
395 fn returns_checked_out_branch_name() -> Result<()> {
396 let test_repo = GitTestRepo::default();
397 let _ = test_repo.populate()?;
398 // create feature branch
399 test_repo.create_branch("example-feature")?;
400 test_repo.checkout("example-feature")?;
401
402 let git_repo = Repo::from_path(&test_repo.dir)?;
403
404 assert_eq!(
405 git_repo.get_checked_out_branch_name()?,
406 "example-feature".to_string()
407 );
408 Ok(())
409 }
410 }
411
381 mod get_commits_ahead_behind { 412 mod get_commits_ahead_behind {
382 use super::*; 413 use super::*;
383 mod returns_main { 414 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(
313 .get_root_commit(to_branch) 313 .get_root_commit(to_branch)
314 .context("failed to get root commit of the repository")?; 314 .context("failed to get root commit of the repository")?;
315 315
316 let mut pr_tags = vec![
317 Tag::Reference(format!("r-{root_commit}")),
318 Tag::Name(title.to_string()),
319 Tag::Description(description.to_string()),
320 ];
321
322 if let Ok(branch_name) = git_repo.get_checked_out_branch_name() {
323 pr_tags.push(Tag::Generic(
324 TagKind::Custom("branch-name".to_string()),
325 vec![branch_name],
326 ));
327 }
328
316 let pr_event = EventBuilder::new( 329 let pr_event = EventBuilder::new(
317 nostr::event::Kind::Custom(PR_KIND), 330 nostr::event::Kind::Custom(PR_KIND),
318 format!("{title}\r\n\r\n{description}"), 331 format!("{title}\r\n\r\n{description}"),
319 &[Tag::Reference(format!("r-{root_commit}"))], 332 &pr_tags,
320 // TODO: suggested branch name
321 // Tag::Generic(
322 // TagKind::Custom("suggested-branch-name".to_string()),
323 // vec![],
324 // ),
325 // TODO: add Repo event as root 333 // TODO: add Repo event as root
326 // TODO: people tag maintainers 334 // TODO: people tag maintainers
327 // TODO: add relay tags 335 // TODO: add relay tags
diff --git a/tests/prs_create.rs b/tests/prs_create.rs
index 72a85c8..dc6eec0 100644
--- a/tests/prs_create.rs
+++ b/tests/prs_create.rs
@@ -175,9 +175,9 @@ mod sends_pr_and_2_patches_to_3_relays {
175 "prs", 175 "prs",
176 "create", 176 "create",
177 "--title", 177 "--title",
178 "example", 178 "exampletitle",
179 "--description", 179 "--description",
180 "example", 180 "exampledescription",
181 ], 181 ],
182 ) 182 )
183 } 183 }
@@ -426,6 +426,79 @@ mod sends_pr_and_2_patches_to_3_relays {
426 } 426 }
427 Ok(()) 427 Ok(())
428 } 428 }
429
430 #[test]
431 #[serial]
432 fn pr_tags_title_as_name() -> Result<()> {
433 let (_, _, r53, r55, r56) = futures::executor::block_on(prep_run_create_pr())?;
434 for relay in [&r53, &r55, &r56] {
435 let pr_event: &nostr::Event = relay
436 .events
437 .iter()
438 .find(|e| e.kind.as_u64().eq(&PR_KIND))
439 .unwrap();
440
441 assert_eq!(
442 pr_event
443 .tags
444 .iter()
445 .find(|t| t.as_vec()[0].eq("name"))
446 .unwrap()
447 .as_vec()[1],
448 "exampletitle"
449 );
450 }
451 Ok(())
452 }
453
454 #[test]
455 #[serial]
456 fn pr_tags_description() -> Result<()> {
457 let (_, _, r53, r55, r56) = futures::executor::block_on(prep_run_create_pr())?;
458 for relay in [&r53, &r55, &r56] {
459 let pr_event: &nostr::Event = relay
460 .events
461 .iter()
462 .find(|e| e.kind.as_u64().eq(&PR_KIND))
463 .unwrap();
464
465 assert_eq!(
466 pr_event
467 .tags
468 .iter()
469 .find(|t| t.as_vec()[0].eq("description"))
470 .unwrap()
471 .as_vec()[1],
472 "exampledescription"
473 );
474 }
475 Ok(())
476 }
477
478 #[test]
479 #[serial]
480 fn pr_tags_branch_name() -> Result<()> {
481 let (_, _, r53, r55, r56) = futures::executor::block_on(prep_run_create_pr())?;
482 for relay in [&r53, &r55, &r56] {
483 let pr_event: &nostr::Event = relay
484 .events
485 .iter()
486 .find(|e| e.kind.as_u64().eq(&PR_KIND))
487 .unwrap();
488
489 // branch-name tag
490 assert_eq!(
491 pr_event
492 .tags
493 .iter()
494 .find(|t| t.as_vec()[0].eq("branch-name"))
495 .unwrap()
496 .as_vec()[1],
497 "feature"
498 );
499 }
500 Ok(())
501 }
429 } 502 }
430 503
431 mod patch_tags { 504 mod patch_tags {