Skip to content

Commit

Permalink
fix: ensure branch name is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
gmickel committed Jul 29, 2024
1 parent 1825be8 commit 82ced3c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/ai/parse-ai-codegen-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export function parseAICodegenResponse(response: string): AIParsedResponse {
}

// Parse other fields
result.gitBranchName = parseField(response, 'git_branch_name');
result.gitBranchName =
parseField(response, 'git_branch_name') ||
`feature/ai-task-${Date.now()}`;
result.gitCommitMessage = parseField(response, 'git_commit_message');
result.summary = parseField(response, 'summary');
result.potentialIssues = parseField(response, 'potential_issues');
Expand Down
6 changes: 5 additions & 1 deletion src/ai/task-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { applyChanges } from '../git/apply-changes';
import { selectFilesPrompt } from '../interactive/select-files-prompt';
import type { AiAssistedTaskOptions, MarkdownOptions } from '../types';
import { DEFAULT_CACHE_PATH } from '../utils/cache-utils';
import { ensureBranch } from '../utils/git-tools';
import { ensureBranch, ensureValidBranchName } from '../utils/git-tools';
import {
collectVariables,
extractTemplateVariables,
Expand Down Expand Up @@ -196,6 +196,10 @@ export async function runAIAssistedTask(options: AiAssistedTaskOptions) {

const parsedResponse = parseAICodegenResponse(generatedCode);

parsedResponse.gitBranchName = ensureValidBranchName(
parsedResponse.gitBranchName,
);

if (options.dryRun) {
spinner.info(
chalk.yellow(
Expand Down
15 changes: 15 additions & 0 deletions src/utils/git-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ export async function prReview(prNumber: string): Promise<FileInfo[]> {
return [];
}

export function ensureValidBranchName(branchName: string): string {
// Remove any characters that are not allowed in Git branch names
let sanitizedName = branchName.replace(/[^a-zA-Z0-9-_./]/g, '-');

// Ensure the branch name doesn't start or end with a slash
sanitizedName = sanitizedName.replace(/^\/+|\/+$/g, '');

// If the branch name is empty after sanitization, provide a default
if (!sanitizedName) {
sanitizedName = `feature/ai-task-${Date.now()}`;
}

return sanitizedName;
}

export async function ensureBranch(
basePath: string,
initialBranchName: string,
Expand Down

0 comments on commit 82ced3c

Please sign in to comment.