Skip to content

Commit 691795c

Browse files
committed
Enhance GitHub workflow PR creation to handle existing pull requests and add update notifications
1 parent 557d78c commit 691795c

1 file changed

Lines changed: 90 additions & 42 deletions

File tree

.github/workflows/claude.yml

Lines changed: 90 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -710,72 +710,120 @@ jobs:
710710
echo "📊 Change statistics:"
711711
git diff --stat $BRANCH_RANGE
712712
fi
713-
714-
- name: Create Pull Request
713+
#########################################################
714+
# IF we have changes, create or update a pull request
715+
#########################################################
716+
- name: Create or Update Pull Request
715717
id: auto-pr
716-
if: steps.claude.outcome == 'success' && steps.context-info.outputs.is-pr == 'false' && steps.claude.outputs.claude_branch_name && steps.check-changes.outputs.has-changes == 'true'
718+
# The 'if' condition is now correctly chained.
719+
if: |
720+
steps.claude.outcome == 'success'
721+
&& steps.context-info.outputs.is-pr == 'false'
722+
&& steps.claude.outputs.claude_branch_name
723+
&& steps.check-changes.outputs.has-changes == 'true'
717724
uses: actions/github-script@v7
718725
with:
719726
github-token: ${{ secrets.GITHUB_TOKEN }}
720727
script: |
721-
const issueNumber = ${{ steps.context-info.outputs.issue-number }};
722-
const branchName = '${{ steps.claude.outputs.claude_branch_name }}';
723-
const defaultBranch = '${{ github.event.repository.default_branch }}';
728+
const issueNumber = ${{ steps.context-info.outputs.issue-number }}
729+
const branchName = '${{ steps.claude.outputs.claude_branch_name }}'
730+
const defaultBranch = '${{ github.event.repository.default_branch }}'
731+
const owner = context.repo.owner
732+
const repo = context.repo.repo
724733
725-
console.log(`Attempting to create PR for branch: ${branchName}`);
726-
727734
try {
728-
const { data: issue } = await github.rest.issues.get({
729-
owner: context.repo.owner,
730-
repo: context.repo.repo,
731-
issue_number: issueNumber
732-
});
735+
// 1. Check for an existing PR for this branch
736+
console.log(`Checking for existing PRs for branch: ${branchName}`)
733737
734-
const prTitle = `🤖 Claude Code: ${issue.title}`;
735-
const prBody = `## 🤖 Automated fix by Claude Code
738+
let existingPr = null
739+
try {
740+
const { data: pulls } = await github.rest.pulls.list({
741+
owner,
742+
repo,
743+
head: `${owner}:${branchName}`,
744+
state: 'open',
745+
per_page: 1
746+
})
747+
existingPr = pulls.length > 0 ? pulls[0] : null
748+
} catch (error) {
749+
existingPr = null
750+
}
751+
752+
let pr
753+
754+
if (existingPr) {
755+
// 2. If PR exists, use it
756+
pr = existingPr
757+
console.log(`✅ Found existing PR: #${pr.number}. No new PR will be created.`)
758+
759+
// Optional: Post a comment to the existing PR to notify of the update
760+
const updateComment = `🔄 **Claude Code has updated this branch** with new changes.\n\n[View the latest workflow run](https://github.com/${owner}/${repo}/actions/runs/${{ github.run_id }})`
761+
await github.rest.issues.createComment({
762+
owner,
763+
repo,
764+
issue_number: pr.number,
765+
body: updateComment,
766+
})
767+
768+
} else {
769+
// 3. If no PR exists, create one
770+
console.log(`No existing PR found. Attempting to create a new PR for branch: ${branchName}`)
771+
772+
const { data: issue } = await github.rest.issues.get({
773+
owner,
774+
repo,
775+
issue_number: issueNumber
776+
})
777+
778+
const prTitle = `🤖 Claude Code: ${issue.title}`
779+
const prBody = `## 🤖 Automated fix by Claude Code
736780
737781
**Related Issue:** #${issueNumber}
738782
**Executed by:** @${{ github.actor }}
739783
740784
### ✅ Next Steps
741-
1. Review the changes in this PR.
742-
2. Confirm that all tests pass.
743-
3. Merge if everything looks good.
785+
1. Review the changes in this PR.
786+
2. Confirm that all tests pass.
787+
3. Merge if everything looks good.
744788
745789
**If additional fixes are needed:** Mention \`@ claude\` in a comment on this PR.
746790
747-
resolves #${issueNumber}
791+
*resolves #${issueNumber}*
748792
749793
---
750-
*Automated PR by [Claude Code Workflow](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})*
751-
`;
794+
*Automated PR by [Claude Code Workflow](https://github.com/${owner}/${repo}/actions/runs/${{ github.run_id }})*
795+
`
752796
753-
const { data: pr } = await github.rest.pulls.create({
754-
owner: context.repo.owner,
755-
repo: context.repo.repo,
756-
title: prTitle,
757-
head: branchName,
758-
base: defaultBranch,
759-
body: prBody,
760-
draft: false
761-
});
762-
763-
console.log(`🎉 PR created successfully: #${pr.number}`);
764-
765-
core.setOutput('pr-number', pr.number);
766-
core.setOutput('pr-url', pr.html_url);
767-
core.setOutput('branch-name', branchName);
797+
const { data: newPr } = await github.rest.pulls.create({
798+
owner,
799+
repo,
800+
title: prTitle,
801+
head: branchName,
802+
base: defaultBranch,
803+
body: prBody,
804+
draft: false
805+
})
806+
807+
pr = newPr
808+
console.log(`🎉 PR created successfully: #${pr.number}`)
809+
}
768810
811+
// 4. Set outputs with the PR info (whether new or existing)
812+
core.setOutput('pr-number', pr.number)
813+
core.setOutput('pr-url', pr.html_url)
814+
core.setOutput('branch-name', branchName)
815+
769816
} catch (error) {
770-
console.error('❌ PR creation error:', error);
771-
core.setOutput('error', error.message);
817+
console.error('❌ PR creation/update error:', error)
818+
core.setOutput('error', error.message)
772819
820+
const failureComment = `❌ **Automatic PR creation failed**\n\n**Error:** \`${error.message}\`\n\n**Solution**: A branch named \`${branchName}\` was created with the changes. Please create a PR from it manually or rerun the job.\n**Details**: [Actions run log](${{ github.server_url }}/${owner}/${repo}/actions/runs/${{ github.run_id }})`
773821
await github.rest.issues.createComment({
774822
issue_number: issueNumber,
775-
owner: context.repo.owner,
776-
repo: context.repo.repo,
777-
body: `❌ **Automatic PR creation failed**\n\n**Error:** \`${error.message}\`\n\n**Solution**: A branch named \`${branchName}\` was created with the changes. Please create a PR from it manually or rerun the job.\n**Details**: [Actions run log](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})`
778-
});
823+
owner,
824+
repo,
825+
body: failureComment
826+
})
779827
}
780828
781829
- name: Notify on Success

0 commit comments

Comments
 (0)