Skip to content

Commit

Permalink
Add option for creating new pr
Browse files Browse the repository at this point in the history
- we automatically re-attach open prs, so we only allow it when pr is closed
  • Loading branch information
mtsgrd committed Nov 19, 2024
1 parent 42b855f commit def6d9a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
24 changes: 23 additions & 1 deletion apps/desktop/src/lib/branch/SeriesHeader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@
branchController.updateSeriesName(branch.id, currentSeries.name, message);
}
}
async function onCreateNewPr() {
// Make sure the listing result is up-to-date so that we don't
// automatically set it back to what it was. If a branch has no
// pr attached we look for any open prs with a matching branch
// name, and save it to the branch.
await $forgeListing?.refresh();
if (!currentSeries.prNumber) {
throw new Error('Failed to discard pr, try reloading the app.');
}
// Delete the reference stored on disk.
branchController.updateBranchPrNumber(branch.id, currentSeries.name, null);
kebabContextMenu?.close();
// Display create pr modal after a slight delay, this prevents
// interference with the closing context menu. It also feels nice
// that these two things are not happening at the same time.
setTimeout(() => handleOpenPR(), 250);
}
</script>

<AddSeriesModal bind:this={stackingAddSeriesModal} parentSeriesName={currentSeries.name} />
Expand All @@ -251,14 +272,15 @@
if (url) openExternalUrl(url);
}}
hasForgeBranch={!!forgeBranch}
prUrl={$pr?.htmlUrl}
pr={$pr}
openPrDetailsModal={handleOpenPR}
{branchType}
onMenuToggle={(isOpen, isLeftClick) => {
if (isLeftClick) {
contextMenuOpened = isOpen;
}
}}
{onCreateNewPr}
/>

<div
Expand Down
23 changes: 18 additions & 5 deletions apps/desktop/src/lib/branch/SeriesHeaderContextMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import Button from '@gitbutler/ui/Button.svelte';
import Modal from '@gitbutler/ui/Modal.svelte';
import Textbox from '@gitbutler/ui/Textbox.svelte';
import type { DetailedPullRequest } from '$lib/forge/interface/types';
interface Props {
contextMenuEl?: ReturnType<typeof ContextMenu>;
Expand All @@ -22,13 +23,14 @@
seriesCount: number;
isTopSeries: boolean;
hasForgeBranch: boolean;
prUrl?: string;
pr?: DetailedPullRequest;
branchType: CommitStatus;
description: string;
toggleDescription: () => Promise<void>;
onGenerateBranchName: () => void;
openPrDetailsModal: () => void;
onAddDependentSeries?: () => void;
onCreateNewPr?: () => Promise<void>;
onOpenInBrowser?: () => void;
onMenuToggle?: (isOpen: boolean, isLeftClick: boolean) => void;
}
Expand All @@ -41,12 +43,13 @@
seriesCount,
hasForgeBranch,
headName,
prUrl,
pr,
branchType,
description,
toggleDescription,
onGenerateBranchName,
openPrDetailsModal,
onCreateNewPr,
onAddDependentSeries,
onOpenInBrowser,
onMenuToggle
Expand Down Expand Up @@ -160,19 +163,19 @@
/>
{/if}
</ContextMenuSection>
{#if prUrl}
{#if pr?.htmlUrl}
<ContextMenuSection>
<ContextMenuItem
label="Open PR in browser"
onclick={() => {
openExternalUrl(prUrl);
openExternalUrl(pr.htmlUrl);
contextMenuEl?.close();
}}
/>
<ContextMenuItem
label="Copy PR link"
onclick={() => {
copyToClipboard(prUrl);
copyToClipboard(pr.htmlUrl);
contextMenuEl?.close();
}}
/>
Expand All @@ -185,6 +188,16 @@
/>
</ContextMenuSection>
{/if}
{#if onCreateNewPr && pr?.state === 'closed'}
<ContextMenuSection>
<ContextMenuItem
label="Create new PR"
onclick={async () => {
await onCreateNewPr();
}}
/>
</ContextMenuSection>
{/if}
</ContextMenu>

<Modal
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/lib/forge/github/githubListingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class GitHubListingService implements ForgeListingService {

async refresh(): Promise<void> {
try {
this.fetch();
await this.fetch();
} catch (e) {
this.error.set(e);
console.error(e);
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop/src/lib/vbranches/branchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class BranchController {
* @param headName The branch name to update.
* @param prNumber New pull request number to be set for the branch.
*/
async updateBranchPrNumber(stackId: string, headName: string, prNumber: number | undefined) {
async updateBranchPrNumber(stackId: string, headName: string, prNumber: number | null) {
try {
await invoke<void>('update_series_pr_number', {
projectId: this.projectId,
Expand All @@ -170,7 +170,7 @@ export class BranchController {
prNumber
});
} catch (err) {
showError('Failed to update branch forge ids', err);
showError('Failed to update pr number', err);
}
}

Expand Down

0 comments on commit def6d9a

Please sign in to comment.