From ed9900b5aca79568dfd9fd8a29ef7fa075eace15 Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Thu, 19 Jan 2023 10:03:03 -0500 Subject: [PATCH] cli: Support patch commenting Allow commenting on a patch as well an issue. Signed-off-by: Slack Coder --- radicle-cli/examples/rad-patch.md | 6 +++++ radicle-cli/src/commands/comment.rs | 28 +++++++++++++++------ radicle/src/cob/patch.rs | 38 ++++++++++++++++++++++++++--- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/radicle-cli/examples/rad-patch.md b/radicle-cli/examples/rad-patch.md index 3492f9f3..b154aa4e 100644 --- a/radicle-cli/examples/rad-patch.md +++ b/radicle-cli/examples/rad-patch.md @@ -107,3 +107,9 @@ ok Analyzing remotes... ok Patch 15141cf1497627e2db54362972dd9533f62d1dcb updated 🌱 ``` + +And lets leave a quick comment for our team: + +``` +$ rad comment 15141cf1497627e2db54362972dd9533f62d1dcb --message 'I cannot wait to get back to the 90s!' +``` diff --git a/radicle-cli/src/commands/comment.rs b/radicle-cli/src/commands/comment.rs index cb79c220..2db69888 100644 --- a/radicle-cli/src/commands/comment.rs +++ b/radicle-cli/src/commands/comment.rs @@ -5,6 +5,7 @@ use anyhow::anyhow; use radicle::cob; use radicle::cob::issue::Issues; +use radicle::cob::patch::Patches; use radicle::cob::store; use radicle::prelude::*; use radicle::storage; @@ -93,15 +94,28 @@ fn comment( } let mut issues = Issues::open(*signer.public_key(), repo)?; - let mut issue = issues.get_mut(&options.id).map_err(|e| match e { - store::Error::NotFound(_, _) => anyhow::anyhow!("Could not find issue {}", options.id), - _ => e.into(), - })?; - let (comment_id, _) = issue.comments().next().expect("root comment always exists"); + match issues.get_mut(&options.id) { + Ok(mut issue) => { + let (comment_id, _) = issue.comments().next().expect("root comment always exists"); + issue.comment(message, *comment_id, &signer)?; + return Ok(()); + } + Err(store::Error::NotFound(_, _)) => {} + Err(e) => return Err(e.into()), + } - issue.comment(message, *comment_id, &signer)?; + let mut patches = Patches::open(*signer.public_key(), repo)?; + match patches.get_mut(&options.id) { + Ok(mut patch) => { + let (revision_id, _) = patch.revisions().last().expect("patch has a revision"); + patch.comment(*revision_id, message, None, &signer)?; + return Ok(()); + } + Err(store::Error::NotFound(_, _)) => {} + Err(e) => return Err(e.into()), + } - Ok(()) + anyhow::bail!("Couldn't find issue or patch {}", options.id) } pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { diff --git a/radicle/src/cob/patch.rs b/radicle/src/cob/patch.rs index 0af441fd..c08cbc4a 100644 --- a/radicle/src/cob/patch.rs +++ b/radicle/src/cob/patch.rs @@ -573,13 +573,13 @@ impl store::Transaction { &mut self, revision: RevisionId, body: S, - reply_to: CommentId, + reply_to: Option, ) -> OpId { self.push(Action::Thread { revision, action: thread::Action::Comment { body: body.to_string(), - reply_to: Some(reply_to), + reply_to, }, }) } @@ -696,7 +696,7 @@ impl<'a, 'g> PatchMut<'a, 'g> { &mut self, revision: RevisionId, body: S, - reply_to: CommentId, + reply_to: Option, signer: &G, ) -> Result { self.transaction("Comment", signer, |tx| tx.comment(revision, body, reply_to)) @@ -1064,6 +1064,38 @@ mod test { assert_eq!(revision.base, base); } + #[test] + fn test_patch_discussion() { + let tmp = tempfile::tempdir().unwrap(); + let (_, signer, project) = test::setup::context(&tmp); + let mut patches = Patches::open(*signer.public_key(), &project).unwrap(); + let patch = patches + .create( + "My first patch", + "Blah blah blah.", + MergeTarget::Delegates, + git::Oid::try_from("cb18e95ada2bb38aadd8e6cef0963ce37a87add3").unwrap(), + git::Oid::try_from("e2a85016a458cd809c0ecee81f8c99613b0b0945").unwrap(), + &[], + &signer, + ) + .unwrap(); + + let id = patch.id; + let mut patch = patches.get_mut(&id).unwrap(); + let (revision_id, _) = patch.revisions().last().unwrap(); + assert!( + patch + .comment(*revision_id, "patch comment", None, &signer) + .is_ok(), + "can comment on patch" + ); + + let (_, revision) = patch.revisions().last().unwrap(); + let (_, comment) = revision.discussion.first().unwrap(); + assert_eq!("patch comment", comment.body(), "comment body untouched"); + } + #[test] fn test_patch_merge() { let tmp = tempfile::tempdir().unwrap();