|
| 1 | +name: Auto PR and Merge on Push by Specific User |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +jobs: |
| 9 | + auto-pr-and-merge: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Check User |
| 13 | + id: check_user |
| 14 | + run: | |
| 15 | + echo "user_matched=${{ github.actor == 'unclecode' }}" |
| 16 | + echo "user_matched=${{ github.actor == 'unclecode' }}" >> $GITHUB_ENV |
| 17 | +
|
| 18 | + - name: Create Pull Request |
| 19 | + if: env.user_matched == 'true' |
| 20 | + id: create_pull_request |
| 21 | + uses: actions/github-script@v5 |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const payload = { |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + head: 'main', |
| 28 | + base: 'live', |
| 29 | + title: 'Auto PR from main to live', |
| 30 | + body: 'Automatically generated PR to keep live branch up-to-date', |
| 31 | + draft: false, |
| 32 | + }; |
| 33 | + |
| 34 | + // Create the pull request |
| 35 | + await github.rest.pulls.create(payload).then(pr => { |
| 36 | + core.setOutput('pr_number', pr.data.number); |
| 37 | + }).catch(err => core.setFailed(`Failed to create PR: ${err.message}`)); |
| 38 | +
|
| 39 | + - name: Merge Pull Request |
| 40 | + if: env.user_matched == 'true' |
| 41 | + uses: actions/github-script@v5 |
| 42 | + with: |
| 43 | + script: | |
| 44 | + const pr_number = ${{ steps.create_pull_request.outputs.pr_number }}; |
| 45 | + if (!pr_number) { |
| 46 | + core.setFailed('PR number is undefined, skipping merge.'); |
| 47 | + return; |
| 48 | + } |
| 49 | +
|
| 50 | + const payload = { |
| 51 | + owner: context.repo.owner, |
| 52 | + repo: context.repo.repo, |
| 53 | + pull_number: parseInt(pr_number, 10), |
| 54 | + merge_method: 'merge', // Options: 'merge', 'squash', or 'rebase' |
| 55 | + }; |
| 56 | + |
| 57 | + // Attempt to merge the pull request |
| 58 | + await github.rest.pulls.merge(payload).then(response => { |
| 59 | + if (response.status !== 200) { |
| 60 | + core.setFailed('Failed to merge the pull request'); |
| 61 | + } |
| 62 | + }).catch(err => core.setFailed(`Failed to merge PR: ${err.message}`)); |
0 commit comments