From 450f6d6f0c0714940fe5a7dfeb5ad0b271161049 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 6 Feb 2025 13:14:23 +0100 Subject: [PATCH] Remove usage of `DraftStore` @fintan: The API of the DraftStore needs some improvement and it can be easy to do the wrong thing when trying to publish the changes to the published cob reference Based on this PSA we should apply any changes to the patch revisions directly, instead of using the `DraftStore` --- crates/radicle-tauri/src/commands/cob.rs | 1 - .../radicle-tauri/src/commands/cob/draft.rs | 22 --- .../radicle-tauri/src/commands/cob/patch.rs | 52 ------- crates/radicle-tauri/src/lib.rs | 5 - crates/radicle-types/src/cobs.rs | 1 - crates/radicle-types/src/traits/cobs.rs | 37 +---- crates/radicle-types/src/traits/patch.rs | 137 +----------------- 7 files changed, 2 insertions(+), 253 deletions(-) delete mode 100644 crates/radicle-tauri/src/commands/cob/draft.rs diff --git a/crates/radicle-tauri/src/commands/cob.rs b/crates/radicle-tauri/src/commands/cob.rs index 5f918a035..dcc2fae25 100644 --- a/crates/radicle-tauri/src/commands/cob.rs +++ b/crates/radicle-tauri/src/commands/cob.rs @@ -12,7 +12,6 @@ use tauri_plugin_dialog::DialogExt; use crate::AppState; -pub mod draft; pub mod issue; pub mod patch; diff --git a/crates/radicle-tauri/src/commands/cob/draft.rs b/crates/radicle-tauri/src/commands/cob/draft.rs deleted file mode 100644 index 7c4906fd7..000000000 --- a/crates/radicle-tauri/src/commands/cob/draft.rs +++ /dev/null @@ -1,22 +0,0 @@ -use radicle::cob; -use radicle::git; -use radicle::identity; - -use radicle_types::error::Error; -use radicle_types::traits::cobs::Cobs; - -use crate::AppState; - -/// Puts a draft of a Collaborative Object (COB) out of the draft state by updating the reference to the new object ID (OID). -/// -/// The function updates the reference for the provided `type_name` (e.g., patch, issue) to point to the -/// new object ID (OID) associated with the finalized draft, removing it from the draft store. -#[tauri::command] -pub fn publish_draft( - ctx: tauri::State, - rid: identity::RepoId, - cob_id: git::Oid, - type_name: cob::TypeName, -) -> Result<(), Error> { - ctx.publish_draft(rid, cob_id, type_name) -} diff --git a/crates/radicle-tauri/src/commands/cob/patch.rs b/crates/radicle-tauri/src/commands/cob/patch.rs index 613d0a0a9..afb795072 100644 --- a/crates/radicle-tauri/src/commands/cob/patch.rs +++ b/crates/radicle-tauri/src/commands/cob/patch.rs @@ -1,7 +1,5 @@ -use radicle::cob; use radicle::git; use radicle::identity; -use radicle::patch; use radicle::patch::{Action, TYPENAME}; use radicle_types as types; @@ -81,56 +79,6 @@ pub fn revision_by_patch_and_id( ctx.revision_by_id(rid, id, revision_id) } -#[tauri::command] -pub fn create_draft_review( - ctx: tauri::State, - rid: identity::RepoId, - revision_id: cob::patch::RevisionId, - cob_id: git::Oid, - labels: Vec, -) -> Result { - ctx.create_draft_review(rid, revision_id, cob_id, labels) -} - -/// Creates a new review comment on a draft review for a specific patch. -/// -/// This Tauri command is used to add a comment to an existing draft review in a repository. -/// It allows users to comment on a specific location in the code or leave general feedback -/// on a review that belongs to a specific patch. -#[tauri::command] -pub fn create_draft_review_comment( - ctx: tauri::State, - rid: identity::RepoId, - cob_id: git::Oid, - new: types::cobs::thread::CreateReviewComment, -) -> Result<(), Error> { - ctx.create_draft_review_comment(rid, cob_id, new) -} - -/// Edits a draft review for a specific patch revision in a repository. -/// -/// This Tauri command allows users to edit a draft review for a specific patch review. -/// The draft is associated with the user (signer) and the provided patch revision within the repository. -#[tauri::command] -pub fn edit_draft_review( - ctx: tauri::State, - rid: identity::RepoId, - cob_id: git::Oid, - edit: models::patch::ReviewEdit, -) -> Result<(), Error> { - ctx.edit_draft_review(rid, cob_id, edit) -} - -#[tauri::command] -pub fn get_draft_review( - ctx: tauri::State, - rid: identity::RepoId, - cob_id: git::Oid, - revision_id: patch::RevisionId, -) -> Option { - ctx.get_draft_review(rid, cob_id, revision_id) -} - #[tauri::command] pub fn edit_patch( ctx: tauri::State, diff --git a/crates/radicle-tauri/src/lib.rs b/crates/radicle-tauri/src/lib.rs index bca736053..c9f6adde2 100644 --- a/crates/radicle-tauri/src/lib.rs +++ b/crates/radicle-tauri/src/lib.rs @@ -110,11 +110,6 @@ pub fn run() { cob::patch::edit_patch, cob::patch::revisions_by_patch, cob::patch::revision_by_patch_and_id, - cob::patch::create_draft_review, - cob::patch::create_draft_review_comment, - cob::patch::get_draft_review, - cob::patch::edit_draft_review, - cob::draft::publish_draft, thread::create_issue_comment, thread::create_patch_comment, profile::config, diff --git a/crates/radicle-types/src/cobs.rs b/crates/radicle-types/src/cobs.rs index 1603843b0..91aa7353e 100644 --- a/crates/radicle-types/src/cobs.rs +++ b/crates/radicle-types/src/cobs.rs @@ -7,7 +7,6 @@ use radicle::node::{Alias, AliasStore}; pub mod diff; pub mod issue; -pub mod patch; pub mod stream; pub mod thread; diff --git a/crates/radicle-types/src/traits/cobs.rs b/crates/radicle-types/src/traits/cobs.rs index 5164e8ece..d98674293 100644 --- a/crates/radicle-types/src/traits/cobs.rs +++ b/crates/radicle-types/src/traits/cobs.rs @@ -1,6 +1,4 @@ -use radicle::cob::object::Storage; -use radicle::storage::refs::draft; -use radicle::storage::{self, ReadStorage}; +use radicle::storage::ReadStorage; use radicle::{cob, git, identity}; use serde::de::DeserializeOwned; @@ -39,37 +37,4 @@ pub trait Cobs: Profile { Ok::<_, Error>(ops) } - - fn publish_draft( - &self, - rid: identity::RepoId, - cob_id: git::Oid, - type_name: cob::TypeName, - ) -> Result<(), Error> { - let profile = self.profile(); - let signer = profile.signer()?; - let repo = profile.storage.repository(rid)?; - let draft_oid = repo.backend.refname_to_id(&draft::cob( - signer.public_key(), - &type_name, - &cob_id.into(), - ))?; - repo.update( - signer.public_key(), - &type_name, - &cob_id.into(), - &draft_oid.into(), - )?; - - let mut patches = profile.patches_mut(&repo)?; - patches.write(&cob_id.into())?; - - storage::git::cob::DraftStore::new(&repo, *signer.public_key()).remove( - signer.public_key(), - &type_name, - &cob_id.into(), - )?; - - Ok::<_, Error>(()) - } } diff --git a/crates/radicle-types/src/traits/patch.rs b/crates/radicle-types/src/traits/patch.rs index c3e1ce754..f722f8fa8 100644 --- a/crates/radicle-types/src/traits/patch.rs +++ b/crates/radicle-types/src/traits/patch.rs @@ -1,8 +1,7 @@ use radicle::node::Handle; use radicle::patch::cache::Patches as _; -use radicle::storage; use radicle::storage::ReadStorage; -use radicle::{cob, git, identity, patch, Node}; +use radicle::{git, identity, Node}; use crate::cobs; use crate::domain::patch::models; @@ -253,138 +252,4 @@ pub trait PatchesMut: Profile { Ok::<_, Error>(models::patch::Patch::new(*patch.id(), &patch, &aliases)) } - - /// Gets the draft review of the local user for a specific patch revision in a repository. - /// - /// This Tauri command is used to retrieve a patch review draft for the local user - /// on a given patch revision from a repository. - /// It looks up the repository using the provided repository ID (`rid`) and patch object ID (`cob_id`), - /// and gets the patch review of the local user associated with a specific revision (`revision_id`), if it exists. - fn get_draft_review( - &self, - rid: identity::RepoId, - cob_id: git::Oid, - revision_id: patch::RevisionId, - ) -> Option { - let profile = self.profile(); - let aliases = profile.aliases(); - let repo = profile.storage.repository(rid).ok()?; - let signer = profile.signer().ok()?; - let drafts = storage::git::cob::DraftStore::new(&repo, *signer.public_key()); - let patches = cob::patch::Cache::no_cache(&drafts).ok()?; - - let patch = patches.get(&cob_id.into()).ok()?; - let revision = patch.and_then(|p| p.revision(&revision_id).cloned()); - let review: Option = - revision.and_then(|rev| rev.review_by(signer.public_key()).cloned()); - - review.map(|r| models::patch::Review::new(r, &aliases)) - } - - /// Edits a draft review for a specific patch revision in a repository. - /// - /// This Tauri command allows users to edit a draft review for a specific patch review. - /// The draft is associated with the user (signer) and the provided patch revision within the repository. - fn edit_draft_review( - &self, - rid: identity::RepoId, - cob_id: git::Oid, - edit: models::patch::ReviewEdit, - ) -> Result<(), Error> { - let profile = self.profile(); - let repo = profile.storage.repository(rid)?; - let signer = profile.signer()?; - let drafts = storage::git::cob::DraftStore::new(&repo, *signer.public_key()); - - let mut patches = cob::patch::Cache::no_cache(&drafts)?; - let mut patch = patches.get_mut(&cob_id.into())?; - patch.review_edit( - edit.review_id, - edit.verdict.map(|v| v.into()), - edit.summary, - edit.labels, - &signer, - )?; - - patches.write(&cob_id.into())?; - - Ok::<_, Error>(()) - } - - /// Creates a draft review for a specific patch revision. - /// - /// This Tauri command allows users to create a new draft review for a specific patch revision. - /// The draft is associated with the user (signer) and the provided patch revision within the repository. - fn create_draft_review( - &self, - rid: identity::RepoId, - revision_id: cob::patch::RevisionId, - cob_id: git::Oid, - labels: Vec, - ) -> Result { - let profile = self.profile(); - let repo = profile.storage.repository(rid)?; - let signer = profile.signer()?; - let drafts = storage::git::cob::DraftStore::new(&repo, *signer.public_key()); - - let mut patches = cob::patch::Cache::no_cache(&drafts)?; - let mut patch = patches.get_mut(&cob_id.into())?; - let revision = patch - .revision(&revision_id) - .ok_or_else(|| Error::WithHint { - err: anyhow::anyhow!("patch revision not found"), - hint: "Not able to find the specified patch revision.", - })?; - - let review_id = match revision.review_by(signer.public_key()) { - Some(review) => review.id(), - None => patch.review( - revision.id(), - Some(cob::patch::Verdict::Reject), - None, - labels, - &signer, - )?, - }; - - patches.write(&cob_id.into())?; - - Ok::<_, Error>(review_id) - } - - /// Creates a new review comment on a draft review for a specific patch. - /// - /// This Tauri command is used to add a comment to an existing draft review in a repository. - /// It allows users to comment on a specific location in the code or leave general feedback - /// on a review that belongs to a specific patch. - fn create_draft_review_comment( - &self, - rid: identity::RepoId, - cob_id: git::Oid, - new: cobs::thread::CreateReviewComment, - ) -> Result<(), Error> { - let profile = self.profile(); - let repo = profile.storage.repository(rid)?; - let signer = profile.signer()?; - let drafts = storage::git::cob::DraftStore::new(&repo, *signer.public_key()); - - let mut patches = cob::patch::Cache::no_cache(&drafts)?; - let mut patch = patches.get_mut(&cob_id.into())?; - - patch.transaction("Review comments", &signer, |tx| { - tx.review_comment( - new.review_id, - new.body, - new.location.map(|l| l.into()), - new.reply_to, - new.embeds.into_iter().map(Into::into).collect::>(), - )?; - - Ok(()) - })?; - - patches.write(&cob_id.into())?; - - Ok::<_, Error>(()) - } }