Fix response body stream already read error in content submission #164
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Simple Auto Merge | |
| on: | |
| pull_request_review: | |
| types: [submitted] | |
| issue_comment: | |
| types: [created] | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| issues: write | |
| jobs: | |
| # Auto-merge on review approval | |
| auto-merge: | |
| if: | | |
| github.event_name == 'pull_request_review' && | |
| github.event.review.state == 'approved' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Auto merge PR | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { data: pr } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.payload.pull_request.number | |
| }); | |
| if (pr.mergeable) { | |
| const mergeResult = await github.rest.pulls.merge({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.payload.pull_request.number, | |
| merge_method: 'merge', | |
| commit_title: `Auto-merge: ${pr.title}`, | |
| commit_message: `Approved by @${context.payload.review.user.login}\n\nPR: #${context.payload.pull_request.number}` | |
| }); | |
| console.log('✅ PR merged successfully:', mergeResult.data.sha); | |
| // Comment on the PR | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.payload.pull_request.number, | |
| body: `🚀 **Auto-merged!** | |
| This PR has been automatically merged to main after receiving approval from @${context.payload.review.user.login}. | |
| ✅ **Status**: Merged and deployed | |
| 🔗 **Commit**: \`${mergeResult.data.sha}\` | |
| 📅 **Merged at**: ${new Date().toISOString()} | |
| Thank you for your contribution! 🎉` | |
| }); | |
| } else { | |
| console.log('❌ PR is not mergeable'); | |
| throw new Error('PR cannot be merged'); | |
| } | |
| # Admin/Owner approval via comment | |
| admin-approval: | |
| if: | | |
| github.event_name == 'issue_comment' && | |
| github.event.issue.pull_request && | |
| contains(github.event.comment.body, '/approve') | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Admin-approve and merge | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| // Check if the commenter is an admin or owner | |
| const { data: commenter } = await github.rest.users.getByUsername({ | |
| username: context.payload.comment.user.login | |
| }); | |
| // Get repository collaborators to check admin status | |
| const { data: collaborators } = await github.rest.repos.listCollaborators({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo | |
| }); | |
| const commenterCollaborator = collaborators.find(collab => | |
| collab.login === context.payload.comment.user.login | |
| ); | |
| // Check if user is admin or owner | |
| const isAdmin = commenterCollaborator && | |
| (commenterCollaborator.permissions.admin === true || | |
| commenterCollaborator.permissions.maintain === true); | |
| const isOwner = context.payload.comment.user.login === context.repo.owner; | |
| if (!isAdmin && !isOwner) { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: `❌ **Admin Approval Required** | |
| Only repository admins, maintainers, or owners can use the \`/approve\` command. | |
| **Current User**: @${context.payload.comment.user.login} | |
| **Required Role**: Admin, Maintainer, or Owner | |
| Please contact a repository admin to approve this PR.` | |
| }); | |
| throw new Error('Insufficient permissions for /approve command'); | |
| } | |
| const { data: pr } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.issue.number | |
| }); | |
| // Check if PR follows naming convention | |
| const branchName = pr.head.ref; | |
| const validPrefixes = ['feature/', 'fix/', 'docs/', 'improve/', 'security/']; | |
| const hasValidPrefix = validPrefixes.some(prefix => branchName.startsWith(prefix)); | |
| if (!hasValidPrefix) { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: `❌ **Admin Approval Rejected** | |
| This PR cannot be approved because it doesn't follow the naming convention. | |
| **Requirements:** | |
| - Branch must start with: \`feature/\`, \`fix/\`, \`docs/\`, \`improve/\`, or \`security/\` | |
| **Current Branch**: \`${branchName}\` | |
| Please ensure your PR follows the naming convention before approval.` | |
| }); | |
| throw new Error('Invalid branch naming convention'); | |
| } | |
| if (pr.mergeable) { | |
| const mergeResult = await github.rest.pulls.merge({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.issue.number, | |
| merge_method: 'merge', | |
| commit_title: `Admin-approved merge: ${pr.title}`, | |
| commit_message: `Admin-approved by @${context.payload.comment.user.login}\n\nPR: #${context.issue.number}` | |
| }); | |
| console.log('✅ PR merged successfully:', mergeResult.data.sha); | |
| // Comment on the PR | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: `🚀 **Admin-Approved and Auto-Merged!** | |
| This PR has been automatically merged to main after admin approval by @${context.payload.comment.user.login}. | |
| ✅ **Status**: Merged and deployed | |
| 🔗 **Commit**: \`${mergeResult.data.sha}\` | |
| 📅 **Merged at**: ${new Date().toISOString()} | |
| 👤 **Approved by**: Admin/Maintainer/Owner | |
| Thank you for your contribution! 🎉` | |
| }); | |
| } else { | |
| console.log('❌ PR is not mergeable'); | |
| throw new Error('PR cannot be merged'); | |
| } |