mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Auto-create feature branch when creating PR from main/master
**New Behavior:** When users try to create a PR while on main/master branch, automatically: - Generate a feature branch name from the PR title - Sanitize title (lowercase, hyphens, remove special chars) - Add timestamp suffix for uniqueness (e.g., "fix-bug-ab3d") - Create and checkout the new branch - Continue with PR creation on the new branch **UX Improvements:** - Alert shown: "You're on the main branch. A feature branch will be automatically created..." - Visual indicator in branch comparison section - Toast notification when branch is created - Clear error if branch creation fails **Benefits:** - Non-technical users don't need to understand branches - Prevents accidental commits to main/master - Follows Git best practices automatically - Smooth workflow: make changes → create PR → done **Example:** PR Title: "Fix login button styling" Generated branch: "fix-login-button-styling-k7m2" This makes contributing to open source as simple as filling out a form, with all Git complexity handled automatically.
This commit is contained in:
1 parent
db22404415
commit
2f6da4fe11
2 files changed
+126
-9
No files matched your search
@@ -251,6 +251,51 @@ export function PullRequestDialog({
|
||||
console.log('Creating PR for:', remoteInfo);
|
||||
console.log('Remote URL:', remoteUrl);
|
||||
|
||||
// Check if user is on main/master - if so, create a feature branch
|
||||
let branchToUse = currentBranch;
|
||||
const isOnDefaultBranch = currentBranch === 'main' || currentBranch === 'master';
|
||||
|
||||
if (isOnDefaultBranch) {
|
||||
console.log(`User is on default branch '${currentBranch}', creating feature branch`);
|
||||
|
||||
// Generate branch name from PR title
|
||||
const sanitizedTitle = title
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9\s-]/g, '') // Remove special chars
|
||||
.replace(/\s+/g, '-') // Replace spaces with hyphens
|
||||
.replace(/-+/g, '-') // Replace multiple hyphens with single
|
||||
.replace(/^-|-$/g, '') // Remove leading/trailing hyphens
|
||||
.substring(0, 50); // Limit length
|
||||
|
||||
const featureBranchName = `${sanitizedTitle}-${Date.now().toString(36).slice(-4)}`;
|
||||
console.log(`Creating feature branch: ${featureBranchName}`);
|
||||
|
||||
try {
|
||||
// Create and checkout the new branch
|
||||
await git.branch({
|
||||
dir: projectPath,
|
||||
ref: featureBranchName,
|
||||
checkout: true,
|
||||
});
|
||||
|
||||
branchToUse = featureBranchName;
|
||||
console.log(`Successfully created and checked out branch: ${featureBranchName}`);
|
||||
|
||||
toast({
|
||||
title: 'Feature branch created',
|
||||
description: `Created branch '${featureBranchName}' for your changes`,
|
||||
});
|
||||
} catch (branchError) {
|
||||
console.error('Failed to create feature branch:', branchError);
|
||||
throw new Error(
|
||||
`Cannot create PR from '${currentBranch}' branch.\n\n` +
|
||||
`Failed to create a feature branch automatically.\n` +
|
||||
`Please create a new branch manually in the Branches tab first.\n\n` +
|
||||
`Error: ${branchError instanceof Error ? branchError.message : 'Unknown error'}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Get credentials
|
||||
const credentials = findCredentialsForRepo(remoteUrl || '', settings.credentials);
|
||||
console.log('Found credentials:', credentials ? 'Yes' : 'No');
|
||||
@@ -274,7 +319,7 @@ export function PullRequestDialog({
|
||||
}
|
||||
|
||||
// If fork is needed, we need to push the branch to the fork first
|
||||
let headRef = currentBranch;
|
||||
let headRef = branchToUse;
|
||||
if (forkInfo.needsFork && forkInfo.forkOwner && forkInfo.forkUrl) {
|
||||
// For cross-fork PRs, head ref format is "owner:branch"
|
||||
headRef = `${forkInfo.forkOwner}:${currentBranch}`;
|
||||
@@ -317,13 +362,13 @@ export function PullRequestDialog({
|
||||
}
|
||||
|
||||
// Push the current branch to the fork
|
||||
console.log(`Pushing branch ${currentBranch} to ${pushRemote} (${forkInfo.forkUrl})`);
|
||||
console.log(`Pushing branch ${branchToUse} to ${pushRemote} (${forkInfo.forkUrl})`);
|
||||
|
||||
try {
|
||||
await git.push({
|
||||
dir: projectPath,
|
||||
remote: pushRemote,
|
||||
ref: currentBranch,
|
||||
ref: branchToUse,
|
||||
onAuth: () => credentials,
|
||||
});
|
||||
console.log('Successfully pushed to fork');
|
||||
@@ -331,7 +376,7 @@ export function PullRequestDialog({
|
||||
console.error('Failed to push to fork:', pushError);
|
||||
const errorMsg = pushError instanceof Error ? pushError.message : 'Unknown error';
|
||||
throw new Error(
|
||||
`Failed to push branch '${currentBranch}' to your fork at ${forkInfo.forkUrl}.\n\n` +
|
||||
`Failed to push branch '${branchToUse}' to your fork at ${forkInfo.forkUrl}.\n\n` +
|
||||
`This usually means:\n` +
|
||||
`1. The branch hasn't been committed yet\n` +
|
||||
`2. Network/authentication issues\n` +
|
||||
@@ -689,6 +734,15 @@ export function PullRequestDialog({
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{/* Auto-branch creation notice */}
|
||||
{currentBranch && (currentBranch === 'main' || currentBranch === 'master') && (
|
||||
<Alert>
|
||||
<AlertDescription>
|
||||
You're on the <strong>{currentBranch}</strong> branch. A feature branch will be automatically created from your PR title for this contribution.
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{/* Branch comparison */}
|
||||
{currentBranch && (
|
||||
<div className="bg-muted/30 rounded-lg p-4">
|
||||
@@ -713,6 +767,11 @@ export function PullRequestDialog({
|
||||
: `Merge ${currentBranch} into ${targetBranch}`
|
||||
}
|
||||
</p>
|
||||
{(currentBranch === 'main' || currentBranch === 'master') && (
|
||||
<p className="text-xs text-blue-600 dark:text-blue-400 mt-1">
|
||||
Note: A new branch will be created automatically
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
@@ -185,13 +185,57 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq
|
||||
throw new Error('No credentials found. Please add credentials in Settings > Git');
|
||||
}
|
||||
|
||||
// Check if user is on main/master - if so, create a feature branch
|
||||
let branchToUse = currentBranch;
|
||||
const isOnDefaultBranch = currentBranch === 'main' || currentBranch === 'master';
|
||||
|
||||
if (isOnDefaultBranch) {
|
||||
console.log(`User is on default branch '${currentBranch}', creating feature branch`);
|
||||
|
||||
// Generate branch name from PR title
|
||||
const sanitizedTitle = title
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9\s-]/g, '')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.replace(/^-|-$/g, '')
|
||||
.substring(0, 50);
|
||||
|
||||
const featureBranchName = `${sanitizedTitle}-${Date.now().toString(36).slice(-4)}`;
|
||||
console.log(`Creating feature branch: ${featureBranchName}`);
|
||||
|
||||
try {
|
||||
await git.branch({
|
||||
dir: projectPath,
|
||||
ref: featureBranchName,
|
||||
checkout: true,
|
||||
});
|
||||
|
||||
branchToUse = featureBranchName;
|
||||
console.log(`Successfully created and checked out branch: ${featureBranchName}`);
|
||||
|
||||
toast({
|
||||
title: 'Feature branch created',
|
||||
description: `Created branch '${featureBranchName}' for your changes`,
|
||||
});
|
||||
} catch (branchError) {
|
||||
console.error('Failed to create feature branch:', branchError);
|
||||
throw new Error(
|
||||
`Cannot create PR from '${currentBranch}' branch.\n\n` +
|
||||
`Failed to create a feature branch automatically.\n` +
|
||||
`Please create a new branch manually in the Branches tab first.\n\n` +
|
||||
`Error: ${branchError instanceof Error ? branchError.message : 'Unknown error'}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// If fork is needed, push the branch first
|
||||
const headRef = forkInfo.needsFork && forkInfo.forkOwner
|
||||
? `${forkInfo.forkOwner}:${currentBranch}`
|
||||
: currentBranch;
|
||||
? `${forkInfo.forkOwner}:${branchToUse}`
|
||||
: branchToUse;
|
||||
|
||||
if (forkInfo.needsFork && forkInfo.forkUrl) {
|
||||
console.log(`Pushing branch ${currentBranch} to fork ${forkInfo.forkUrl}`);
|
||||
console.log(`Pushing branch ${branchToUse} to fork ${forkInfo.forkUrl}`);
|
||||
|
||||
// Check current remotes
|
||||
const remotes = await git.listRemotes({ dir: projectPath });
|
||||
@@ -217,14 +261,14 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq
|
||||
await git.push({
|
||||
dir: projectPath,
|
||||
remote: pushRemote,
|
||||
ref: currentBranch,
|
||||
ref: branchToUse,
|
||||
onAuth: () => credentials,
|
||||
});
|
||||
console.log('Successfully pushed to fork');
|
||||
} catch (pushError) {
|
||||
const errorMsg = pushError instanceof Error ? pushError.message : 'Unknown error';
|
||||
throw new Error(
|
||||
`Failed to push branch '${currentBranch}' to your fork.\n\n` +
|
||||
`Failed to push branch '${branchToUse}' to your fork.\n\n` +
|
||||
`This usually means:\n` +
|
||||
`1. The branch hasn't been committed yet\n` +
|
||||
`2. Network/authentication issues\n\n` +
|
||||
@@ -360,6 +404,15 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{/* Auto-branch creation notice */}
|
||||
{currentBranch && (currentBranch === 'main' || currentBranch === 'master') && (
|
||||
<Alert>
|
||||
<AlertDescription>
|
||||
You're on the <strong>{currentBranch}</strong> branch. A feature branch will be automatically created from your PR title for this contribution.
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{/* Branch comparison */}
|
||||
<div className="bg-muted/30 rounded-lg p-4">
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
@@ -374,6 +427,11 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq
|
||||
<p className="text-xs text-muted-foreground mt-2">
|
||||
Merge {currentBranch} into {targetBranch}
|
||||
</p>
|
||||
{(currentBranch === 'main' || currentBranch === 'master') && (
|
||||
<p className="text-xs text-blue-600 dark:text-blue-400 mt-1">
|
||||
Note: A new branch will be created automatically
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Error alert */}
|
||||
|
||||
Reference in new issue
Block a user