diff --git a/apps/desktop/src/lib/vbranches/branchController.ts b/apps/desktop/src/lib/vbranches/branchController.ts index 1cda10b718..4ad432b2c7 100644 --- a/apps/desktop/src/lib/vbranches/branchController.ts +++ b/apps/desktop/src/lib/vbranches/branchController.ts @@ -4,7 +4,7 @@ import * as toasts from '$lib/utils/toasts'; import posthog from 'posthog-js'; import type { BaseBranchService } from '$lib/baseBranch/baseBranchService'; import type { RemoteBranchService } from '$lib/stores/remoteBranches'; -import type { BranchPushResult, Hunk, LocalFile } from './types'; +import type { BranchPushResult, ForgeIdentifier, Hunk, LocalFile } from './types'; import type { VirtualBranchService } from './virtualBranch'; export type CommitIdOrChangeId = { CommitId: string } | { ChangeId: string }; @@ -154,6 +154,26 @@ export class BranchController { } } + /** + * Updates the forge identifierd for a branch/series. + * This is useful for storing for example the Pull Request Number for a branch. + * @param stackId The stack ID to update. + * @param headName The branch name to update. + * @param forgeIds New forge ids to be set for the branch (overrides current state). + */ + async updateSeriesForgeIds(stackId: string, headName: string, forgeIds: ForgeIdentifier[]) { + try { + await invoke('update_series_forge_ids', { + projectId: this.projectId, + stackId, + headName, + forgeIds + }); + } catch (err) { + showError('Failed to update branch forge ids', err); + } + } + /* * Creates a new GitButler change reference associated with a branch. * @param branchId diff --git a/crates/gitbutler-branch-actions/src/stack.rs b/crates/gitbutler-branch-actions/src/stack.rs index 2aaa470d58..e6d3a000e9 100644 --- a/crates/gitbutler-branch-actions/src/stack.rs +++ b/crates/gitbutler-branch-actions/src/stack.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use anyhow::{Context, Result}; use gitbutler_command_context::CommandContext; use gitbutler_commit::commit_ext::CommitExt; -use gitbutler_patch_reference::{CommitOrChangeId, PatchReference}; +use gitbutler_patch_reference::{CommitOrChangeId, ForgeIdentifier, PatchReference}; use gitbutler_project::Project; use gitbutler_repo_actions::RepoActionsExt; use gitbutler_stack::{PatchReferenceUpdate, Series}; @@ -122,6 +122,27 @@ pub fn update_series_description( ) } +/// Sets the forge identifiers for a given series/branch. Existing values are overwritten. +/// +/// # Errors +/// This method will return an error if: +/// - The series does not exist +/// - The stack cant be found +/// - The stack has not been initialized +/// - The project is not in workspace mode +/// - Persisting the changes failed +pub fn update_series_forge_ids( + project: &Project, + stack_id: StackId, + head_name: String, + forge_ids: Vec, +) -> Result<()> { + let ctx = &open_with_verify(project)?; + assure_open_workspace_mode(ctx).context("Requires an open workspace mode")?; + let mut stack = ctx.project().virtual_branches().get_branch(stack_id)?; + stack.set_forge_ids(ctx, head_name, forge_ids) +} + /// Pushes all series in the stack to the remote. /// This operation will error out if the target has no push remote configured. pub fn push_stack(project: &Project, branch_id: StackId, with_force: bool) -> Result<()> { diff --git a/crates/gitbutler-tauri/src/main.rs b/crates/gitbutler-tauri/src/main.rs index 956932a5a4..c35fcdf0ad 100644 --- a/crates/gitbutler-tauri/src/main.rs +++ b/crates/gitbutler-tauri/src/main.rs @@ -195,6 +195,7 @@ fn main() { stack::remove_series, stack::update_series_name, stack::update_series_description, + stack::update_series_forge_ids, stack::push_stack, secret::secret_get_global, secret::secret_set_global, diff --git a/crates/gitbutler-tauri/src/stack.rs b/crates/gitbutler-tauri/src/stack.rs index a28a576408..f1389f5555 100644 --- a/crates/gitbutler-tauri/src/stack.rs +++ b/crates/gitbutler-tauri/src/stack.rs @@ -1,4 +1,5 @@ use gitbutler_branch_actions::stack::CreateSeriesRequest; +use gitbutler_patch_reference::ForgeIdentifier; use gitbutler_project as projects; use gitbutler_project::ProjectId; use gitbutler_stack::StackId; @@ -80,6 +81,24 @@ pub fn update_series_description( Ok(()) } +#[tauri::command(async)] +#[instrument(skip(projects, windows), err(Debug))] +pub fn update_series_forge_ids( + windows: State<'_, WindowState>, + projects: State<'_, projects::Controller>, + project_id: ProjectId, + stack_id: StackId, + head_name: String, + forge_ids: Vec, +) -> Result<(), Error> { + let project = projects.get(project_id)?; + gitbutler_branch_actions::stack::update_series_forge_ids( + &project, stack_id, head_name, forge_ids, + )?; + emit_vbranches(&windows, project_id); + Ok(()) +} + #[tauri::command(async)] #[instrument(skip(projects, windows), err(Debug))] pub fn push_stack(