Skip to content

Commit e5fec67

Browse files
jfrench9claude
andauthored
Fix create-pr workflow: sanitize output and prevent duplicate PRs (#9)
## Summary This PR enhances the robustness and reliability of the automated PR creation workflow by implementing proper output sanitization and duplicate prevention mechanisms. ## Key Accomplishments - **Output Sanitization**: Added proper environment variable handling to safely process Claude-generated PR titles and descriptions, preventing potential injection issues or formatting problems - **Duplicate Prevention**: Implemented validation logic to check for existing PRs before creating new ones, avoiding unnecessary duplicate pull requests - **Improved Error Handling**: Enhanced the workflow's resilience by adding safeguards around the PR creation process - **Better Variable Management**: Structured environment variable usage for cleaner and more maintainable workflow execution ## Breaking Changes None. This is a backward-compatible enhancement that improves existing functionality without changing the workflow's interface or expected behavior. ## Testing Notes - Verify that PR creation works correctly with various title and description formats - Test duplicate detection by attempting to create PRs for branches with existing pull requests - Confirm that environment variable sanitization properly handles special characters and multi-line content - Validate that the workflow fails gracefully when encountering edge cases ## Infrastructure Considerations This update affects the automated PR creation pipeline and should improve overall workflow reliability. The changes focus on defensive programming practices and should reduce potential failures in the CI/CD process. Monitor initial runs to ensure the enhanced validation logic performs as expected in the production environment. --- 🤖 Generated with [Claude Code](https://claude.ai/code) **Branch Info:** - Source: `bugfix/claude-pr-escape` - Target: `main` - Type: bugfix Co-Authored-By: Claude <noreply@anthropic.com>
2 parents 3fa8c5a + c41229d commit e5fec67

1 file changed

Lines changed: 32 additions & 16 deletions

File tree

‎.github/workflows/create-pr.yml‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ jobs:
9898
COMMIT_RANGE="origin/$TARGET_BRANCH...$SOURCE_BRANCH"
9999
100100
# Get diff stats (limit output to prevent memory issues)
101-
DIFF_STATS=$(git diff --stat --no-color $COMMIT_RANGE | head -50) # Limit to 50 lines
101+
DIFF_STATS=$(git diff --stat --no-color $COMMIT_RANGE | head -50 | sed 's/`//g') # Limit to 50 lines
102102
FILES_CHANGED=$(git diff --name-only $COMMIT_RANGE | wc -l)
103103
COMMITS_COUNT=$(git rev-list --count $COMMIT_RANGE)
104104
105105
# Get commit messages (limit to recent commits)
106-
COMMIT_MESSAGES=$(git log --oneline --no-color $COMMIT_RANGE | head -20) # Limit to 20 commits
106+
COMMIT_MESSAGES=$(git log --oneline --no-color $COMMIT_RANGE | head -20 | sed 's/`//g') # Limit to 20 commits
107107
108108
# Get detailed changes (limit to prevent memory issues)
109-
DETAILED_CHANGES=$(git diff $COMMIT_RANGE --name-status --no-color | head -100) # Limit to 100 files
109+
DETAILED_CHANGES=$(git diff $COMMIT_RANGE --name-status --no-color | head -100 | sed 's/`//g') # Limit to 100 files
110110
111111
# Create analysis prompt for Claude
112112
cat << 'EOF' > /tmp/pr_analysis_prompt.txt
@@ -297,14 +297,28 @@ jobs:
297297
298298
- name: Create Pull Request
299299
id: create-pr
300+
env:
301+
CLAUDE_PR_TITLE: ${{ steps.claude.outputs.pr_title }}
302+
CLAUDE_PR_DESCRIPTION: ${{ steps.claude.outputs.pr_description }}
300303
run: |
301304
SOURCE_BRANCH="${{ steps.source-branch.outputs.source_branch }}"
302305
TARGET_BRANCH="${{ inputs.target_branch }}"
303-
PR_TITLE="${{ steps.claude.outputs.pr_title }}"
304306
305-
# Create PR description with Claude analysis + footer
306-
cat > /tmp/pr_description.txt << EOF
307-
${{ steps.claude.outputs.pr_description }}
307+
# Use environment variables to safely handle Claude's output
308+
PR_TITLE="$CLAUDE_PR_TITLE"
309+
PR_DESCRIPTION="$CLAUDE_PR_DESCRIPTION"
310+
311+
# Check if PR already exists
312+
EXISTING_PR=$(gh pr list --head "$SOURCE_BRANCH" --base "$TARGET_BRANCH" --json url --jq '.[0].url' 2>/dev/null || echo "")
313+
314+
if [ -n "$EXISTING_PR" ] && [ "$EXISTING_PR" != "null" ]; then
315+
echo "✅ PR already exists: $EXISTING_PR"
316+
echo "pr_url=$EXISTING_PR" >> $GITHUB_OUTPUT
317+
PR_URL="$EXISTING_PR"
318+
else
319+
# Create PR description with Claude analysis + footer
320+
cat > /tmp/pr_description.txt << EOF
321+
$PR_DESCRIPTION
308322
309323
---
310324
🤖 Generated with [Claude Code](https://claude.ai/code)
@@ -317,17 +331,19 @@ jobs:
317331
Co-Authored-By: Claude <noreply@anthropic.com>
318332
EOF
319333
320-
PR_DESCRIPTION=$(cat /tmp/pr_description.txt)
334+
FINAL_PR_DESCRIPTION=$(cat /tmp/pr_description.txt)
321335
322-
# Create the PR
323-
PR_URL=$(gh pr create \
324-
--title "$PR_TITLE" \
325-
--body "$PR_DESCRIPTION" \
326-
--base "$TARGET_BRANCH" \
327-
--head "$SOURCE_BRANCH")
336+
# Create the PR
337+
PR_URL=$(gh pr create \
338+
--title "$PR_TITLE" \
339+
--body "$FINAL_PR_DESCRIPTION" \
340+
--base "$TARGET_BRANCH" \
341+
--head "$SOURCE_BRANCH")
342+
343+
echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
344+
echo "✅ Pull Request created: $PR_URL"
345+
fi
328346
329-
echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
330-
echo "✅ Pull Request created: $PR_URL"
331347
332348
- name: Request Claude review
333349
if: ${{ inputs.claude_review }}

0 commit comments

Comments
 (0)