|
1 | | -name: Auto-Approve Pull Requests |
| 1 | +name: Auto-Approve and Merge Pull Requests |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | pull_request: |
|
8 | 8 | workflow_dispatch: |
9 | 9 |
|
10 | 10 | jobs: |
11 | | - auto-approve: |
| 11 | + auto-approve-and-merge: |
12 | 12 | runs-on: ubuntu-latest |
13 | 13 | permissions: |
14 | 14 | pull-requests: write |
| 15 | + contents: write |
15 | 16 |
|
16 | 17 | steps: |
17 | 18 | - name: Checkout repository |
18 | 19 | uses: actions/checkout@v3 |
19 | 20 |
|
20 | | - - name: Auto-approve Pull Requests |
| 21 | + - name: Install PyGithub |
| 22 | + run: pip install PyGithub |
| 23 | + |
| 24 | + - name: Auto-approve and Merge Pull Requests |
21 | 25 | env: |
22 | 26 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
23 | 27 | run: | |
24 | 28 | import os |
25 | 29 | from github import Github |
26 | | -
|
27 | | - def approve_pr(pr): |
| 30 | + def approve_and_merge_pr(pr): |
28 | 31 | pr.create_review(event='APPROVE', body='Automatically approved') |
29 | 32 | print(f"Approved PR #{pr.number}") |
30 | | -
|
| 33 | + if pr.mergeable: |
| 34 | + pr.merge(merge_method='squash') |
| 35 | + print(f"Merged PR #{pr.number}") |
| 36 | + else: |
| 37 | + print(f"PR #{pr.number} is not mergeable") |
31 | 38 | g = Github(os.getenv('GITHUB_TOKEN')) |
32 | 39 | repo = g.get_repo(os.getenv('GITHUB_REPOSITORY')) |
33 | | -
|
34 | 40 | if os.getenv('GITHUB_EVENT_NAME') == 'pull_request': |
35 | 41 | pr_number = os.getenv('GITHUB_EVENT_PULL_REQUEST_NUMBER') |
36 | 42 | pr = repo.get_pull(int(pr_number)) |
37 | | - approve_pr(pr) |
| 43 | + approve_and_merge_pr(pr) |
38 | 44 | else: |
39 | 45 | for pr in repo.get_pulls(state='open'): |
40 | | - approve_pr(pr) |
41 | | -
|
42 | | - print("Auto-approval process completed successfully.") |
| 46 | + approve_and_merge_pr(pr) |
| 47 | + print("Auto-approval and merge process completed successfully.") |
43 | 48 | shell: python |
44 | | - |
|
0 commit comments