cli: Support patch commenting

Allow commenting on a patch as well an issue.

Signed-off-by: Slack Coder <slackcoder@server.ky>
This commit is contained in:
Slack Coder 2023-01-19 10:03:03 -05:00 committed by Alexis Sellier
parent 2c3e7cc2c9
commit ed9900b5ac
No known key found for this signature in database
3 changed files with 62 additions and 10 deletions

View File

@ -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!'
```

View File

@ -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<()> {

View File

@ -573,13 +573,13 @@ impl store::Transaction<Patch> {
&mut self,
revision: RevisionId,
body: S,
reply_to: CommentId,
reply_to: Option<CommentId>,
) -> 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<CommentId>,
signer: &G,
) -> Result<CommentId, Error> {
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();