Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable "push and create PR" #5278

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/desktop/src/lib/branch/BranchHeader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
}

function handleOpenPR() {
prDetailsModal?.show();
prDetailsModal?.show(false);
}
</script>

Expand Down
23 changes: 11 additions & 12 deletions apps/desktop/src/lib/branch/StackingSeriesHeader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
const topPatch = $derived(currentSeries?.patches[0]);
const branchType = $derived<CommitStatus>(topPatch?.status ?? 'local');
const lineColor = $derived(getColorFromBranchType(branchType));
const hasNoCommits = $derived(
currentSeries.upstreamPatches.length === 0 && currentSeries.patches.length === 0
);

// Pretty cumbersome way of getting the PR number, would be great if we can
// make it more concise somehow.
Expand All @@ -77,8 +80,8 @@
await Promise.allSettled([prMonitor?.refresh(), checksMonitor?.update()]);
}

function handleOpenPR() {
prDetailsModal?.show();
function handleOpenPR(pushBeforeCreate: boolean = false) {
prDetailsModal?.show(pushBeforeCreate);
}

function editTitle(title: string) {
Expand Down Expand Up @@ -192,7 +195,7 @@
/>
</div>
{/if}
{#if gitHostBranch && $prService}
{#if $prService && !hasNoCommits}
<div class="branch-action">
<div class="branch-action__line" style:--bg-color={lineColor}></div>
<div class="branch-action__body">
Expand All @@ -204,13 +207,15 @@
wide
outline
disabled={currentSeries.patches.length === 0 || !$gitHost || !$prService}
onclick={handleOpenPR}>Create pull request</Button
onclick={() => handleOpenPR(!gitHostBranch)}
>
Create pull request
</Button>
{/if}
</div>
</div>
{/if}
{#if currentSeries.upstreamPatches.length === 0 && currentSeries.patches.length === 0}
{#if hasNoCommits}
<div class="branch-emptystate">
<EmptyStatePlaceholder bottomMargin={10}>
{#snippet title()}
Expand All @@ -226,13 +231,7 @@
{#if $pr}
<PrDetailsModal bind:this={prDetailsModal} type="display" pr={$pr} />
{:else}
<PrDetailsModal
bind:this={prDetailsModal}
type="preview-series"
{upstreamName}
name={currentSeries.name}
commits={currentSeries.patches}
/>
<PrDetailsModal bind:this={prDetailsModal} type="preview-series" {currentSeries} />
{/if}
</div>

Expand Down
29 changes: 19 additions & 10 deletions apps/desktop/src/lib/pr/PrDetailsModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import { error } from '$lib/utils/toasts';
import { openExternalUrl } from '$lib/utils/url';
import { BranchController } from '$lib/vbranches/branchController';
import { DetailedCommit, VirtualBranch } from '$lib/vbranches/types';
import { PatchSeries, VirtualBranch } from '$lib/vbranches/types';
import { getContext, getContextStore } from '@gitbutler/shared/context';
import BorderlessTextarea from '@gitbutler/ui/BorderlessTextarea.svelte';
import Button from '@gitbutler/ui/Button.svelte';
Expand All @@ -57,9 +57,8 @@

interface PreviewSeriesProps {
type: 'preview-series';
name: string;
upstreamName?: string;
commits: DetailedCommit[];
currentSeries: PatchSeries;
}

type Props = DisplayProps | PreviewProps | PreviewSeriesProps;
Expand All @@ -79,10 +78,14 @@
const preferredPRAction = getPreferredPRAction();

const branch = $derived($branchStore);
const branchName = $derived(props.type === 'preview-series' ? props.name : branch.name);
const commits = $derived(props.type === 'preview-series' ? props.commits : branch.commits);
const branchName = $derived(
props.type === 'preview-series' ? props.currentSeries.name : branch.name
);
const commits = $derived(
props.type === 'preview-series' ? props.currentSeries.patches : branch.commits
);
const upstreamName = $derived(
props.type === 'preview-series' ? props.upstreamName : branch.upstreamName
props.type === 'preview-series' ? props.currentSeries.name : branch.upstreamName
);
const baseBranchName = $derived($baseBranch.shortName);

Expand All @@ -98,6 +101,7 @@
let aiConfigurationValid = $state<boolean>(false);
let aiDescriptionDirective = $state<string | undefined>(undefined);
let showAiBox = $state<boolean>(false);
let pushBeforeCreate = $state(false);

async function handleToggleUseTemplate() {
if (!templateSelector) return;
Expand Down Expand Up @@ -144,7 +148,7 @@
}
});

async function createPr(params: CreatePrParams): Promise<PullRequest | undefined> {
export async function createPr(params: CreatePrParams): Promise<PullRequest | undefined> {
if (!$gitHost) {
error('Pull request service not available');
return;
Expand All @@ -154,7 +158,7 @@
try {
let upstreamBranchName = upstreamName;

if (commits.some((c) => !c.isRemote)) {
if (pushBeforeCreate || commits.some((c) => !c.isRemote)) {
const firstPush = !branch.upstream;
const pushResult = await branchController.pushBranch(
branch.id,
Expand Down Expand Up @@ -302,7 +306,11 @@
}, 2000);
}

export function show() {
/**
* @param {boolean} pushAndCreate - Whether or not the commits need pushed before opening a PR
*/
export function show(pushAndCreate = false) {
pushBeforeCreate = pushAndCreate;
modal?.show();
}

Expand Down Expand Up @@ -438,7 +446,8 @@
type="submit"
onclick={async () => await handleCreatePR(close)}
>
{isDraft ? 'Create pull request draft' : 'Create pull request'}
{pushBeforeCreate ? 'Push and ' : ''}
{isDraft ? 'Create pull request draft' : `Create pull request`}

{#snippet contextMenuSlot()}
<ContextMenuSection>
Expand Down