Skip to content

Commit

Permalink
Add API endpoint for setting the forge ids
Browse files Browse the repository at this point in the history
Plus frontend medthod in branchController for calling it
  • Loading branch information
krlvi committed Oct 29, 2024
1 parent 4bba0dd commit 88729a8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
22 changes: 21 additions & 1 deletion apps/desktop/src/lib/vbranches/branchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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<void>('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
Expand Down
23 changes: 22 additions & 1 deletion crates/gitbutler-branch-actions/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<ForgeIdentifier>,
) -> 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<()> {
Expand Down
1 change: 1 addition & 0 deletions crates/gitbutler-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions crates/gitbutler-tauri/src/stack.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ForgeIdentifier>,
) -> 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(
Expand Down

0 comments on commit 88729a8

Please sign in to comment.