Skip to content

Commit c41229d

Browse files
committed
Enhance create-pr workflow by sanitizing output and improving PR creation logic. Added environment variables for Claude's title and description, and implemented a check for existing PRs to prevent duplicates.
1 parent 3fa8c5a commit c41229d

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)