From a86247f5e0bfed638b878a6be551b466e0d36d7f Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 11:31:33 +0100 Subject: [PATCH 01/12] add first version of auto approve script --- .github/workflows/pr-review.yaml | 29 +++++++++++++++------ .pr-bot.yml | 10 ------- scripts/python_auto_pr_approve.py | 43 +++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 18 deletions(-) delete mode 100644 .pr-bot.yml create mode 100644 scripts/python_auto_pr_approve.py diff --git a/.github/workflows/pr-review.yaml b/.github/workflows/pr-review.yaml index 4ee3dc5..a79602f 100644 --- a/.github/workflows/pr-review.yaml +++ b/.github/workflows/pr-review.yaml @@ -1,16 +1,29 @@ -name: Auto PR Review +name: Auto Approve Pull Requests -on: [pull_request] +on: + pull_request: + types: [opened, synchronize] jobs: - test: + auto-approve: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3.5.2 - - uses: actions/setup-node@v3 - - uses: omio-labs/pr-reviewer-bot@v1 + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 with: + python-version: '3.x' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install github3.py + + - name: Auto Approve Pull Requests + env: GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} - - + run: | + python scripts/python_auto_pr_approve.py \ No newline at end of file diff --git a/.pr-bot.yml b/.pr-bot.yml deleted file mode 100644 index 000ac8d..0000000 --- a/.pr-bot.yml +++ /dev/null @@ -1,10 +0,0 @@ -approvalRules: - - changedFiles: - allowed: - - infra/* - - apps/* - - argocd-deploys/* - - README.md - - changedFiles: - allowed: - - scripts/build_and_push.py diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py new file mode 100644 index 0000000..d837cdc --- /dev/null +++ b/scripts/python_auto_pr_approve.py @@ -0,0 +1,43 @@ +import os +from github3 import login +import json + +# Get the Github token from the environment variable +github_token = os.getenv('GH_TOKEN') + +# List of files to trigger auto-approval +files_to_trigger_approval = ['README.md', 'build_and_push.py'] + +# Login to Github using the token +gh = login(token=github_token) + +# Get the pull request event payload from the environment variable +event_payload = os.getenv('GITHUB_EVENT_PATH') + +# Load the payload +with open(event_payload, 'r') as f: + payload = f.read() + +# Parse the payload +payload_data = json.loads(payload) + +# Get the pull request number and repository +pr_number = payload_data['number'] +repo_name = payload_data['repository']['full_name'] + +# Get the repository object +repo = gh.repository(repo_name) + +# Get the pull request object +pr = repo.pull_request(pr_number) + +# Get the list of files changed in the pull request +files_changed = [file['filename'] for file in pr.files()] + +# Check if any of the files to trigger auto-approval are changed +if any(file in files_changed for file in files_to_trigger_approval): + # Add the reviewer (you can add more reviewers if needed) + pr.add_reviewer('Python Auto Approval') + + # Approve the pull request + pr.create_review(commit_id=pr.head.sha, event='APPROVE') \ No newline at end of file From 9e9e9f02bd0b9c19458c85ef582df281d8f2b753 Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 11:36:57 +0100 Subject: [PATCH 02/12] update python script --- scripts/python_auto_pr_approve.py | 35 ++++++++----------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index d837cdc..3f8dacc 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -1,43 +1,26 @@ import os -from github3 import login -import json +from github import Github # Get the Github token from the environment variable github_token = os.getenv('GH_TOKEN') # List of files to trigger auto-approval -files_to_trigger_approval = ['README.md', 'build_and_push.py'] +files_to_trigger_approval = ['README.md', 'file2.py'] # Login to Github using the token -gh = login(token=github_token) +gh = Github(github_token) -# Get the pull request event payload from the environment variable -event_payload = os.getenv('GITHUB_EVENT_PATH') - -# Load the payload -with open(event_payload, 'r') as f: - payload = f.read() - -# Parse the payload -payload_data = json.loads(payload) - -# Get the pull request number and repository -pr_number = payload_data['number'] -repo_name = payload_data['repository']['full_name'] - -# Get the repository object -repo = gh.repository(repo_name) - -# Get the pull request object -pr = repo.pull_request(pr_number) +# Get the repository and pull request from the github context +repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) +pull_request = repository.get_pull(int(os.getenv('GITHUB_REF').split('/')[-1])) # Get the list of files changed in the pull request -files_changed = [file['filename'] for file in pr.files()] +files_changed = [file.filename for file in pull_request.get_files()] # Check if any of the files to trigger auto-approval are changed if any(file in files_changed for file in files_to_trigger_approval): # Add the reviewer (you can add more reviewers if needed) - pr.add_reviewer('Python Auto Approval') + pull_request.create_review_request(reviewers=['Python Auto Approver']) # Approve the pull request - pr.create_review(commit_id=pr.head.sha, event='APPROVE') \ No newline at end of file + pull_request.create_review(body='Approved', event='APPROVE') \ No newline at end of file From a552b8721fe68c2db3c232fbc876d646045bbd16 Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 11:39:19 +0100 Subject: [PATCH 03/12] update pip module --- .github/workflows/pr-review.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-review.yaml b/.github/workflows/pr-review.yaml index a79602f..318d42e 100644 --- a/.github/workflows/pr-review.yaml +++ b/.github/workflows/pr-review.yaml @@ -20,7 +20,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install github3.py + pip install PyGithub - name: Auto Approve Pull Requests env: From d305011450479d64a119292db054788618a2fe1e Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 11:59:04 +0100 Subject: [PATCH 04/12] update script and workflow for auto approval --- .github/workflows/pr-review.yaml | 2 ++ scripts/python_auto_pr_approve.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr-review.yaml b/.github/workflows/pr-review.yaml index 318d42e..d308f81 100644 --- a/.github/workflows/pr-review.yaml +++ b/.github/workflows/pr-review.yaml @@ -25,5 +25,7 @@ jobs: - name: Auto Approve Pull Requests env: GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + GITHUB_REPOSITORY: ${{ github.repository }} + GITHUB_REF: ${{ github.event.pull_request.head.ref }} run: | python scripts/python_auto_pr_approve.py \ No newline at end of file diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index 3f8dacc..3380f2b 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -12,7 +12,7 @@ # Get the repository and pull request from the github context repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) -pull_request = repository.get_pull(int(os.getenv('GITHUB_REF').split('/')[-1])) +pull_request = repository.get_pull(os.getenv('GITHUB_REF').split('/')[-1]) # Get the list of files changed in the pull request files_changed = [file.filename for file in pull_request.get_files()] From 2defa04eb6fb715f4858614d6b1d0a633cb6e315 Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 12:00:45 +0100 Subject: [PATCH 05/12] testing script --- scripts/python_auto_pr_approve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index 3380f2b..3f8dacc 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -12,7 +12,7 @@ # Get the repository and pull request from the github context repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) -pull_request = repository.get_pull(os.getenv('GITHUB_REF').split('/')[-1]) +pull_request = repository.get_pull(int(os.getenv('GITHUB_REF').split('/')[-1])) # Get the list of files changed in the pull request files_changed = [file.filename for file in pull_request.get_files()] From 377f86894633a02ab3b7106eb432d716923e9bdd Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 12:06:21 +0100 Subject: [PATCH 06/12] update script and test 02 --- scripts/python_auto_pr_approve.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index 3f8dacc..a2edc71 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -12,7 +12,11 @@ # Get the repository and pull request from the github context repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) -pull_request = repository.get_pull(int(os.getenv('GITHUB_REF').split('/')[-1])) +pull_request_number = os.getenv('GITHUB_REF').split('/')[-1] +if pull_request_number != 'merge': + pull_request = repository.get_pull(int(pull_request_number)) +else: + print("Pull request not found") # Get the list of files changed in the pull request files_changed = [file.filename for file in pull_request.get_files()] From 79b9cfbf997affbec48a527a06ddb89828aaebd5 Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 12:12:27 +0100 Subject: [PATCH 07/12] update script and test 03 --- .github/workflows/pr-review.yaml | 1 + scripts/python_auto_pr_approve.py | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pr-review.yaml b/.github/workflows/pr-review.yaml index d308f81..0344c8d 100644 --- a/.github/workflows/pr-review.yaml +++ b/.github/workflows/pr-review.yaml @@ -24,6 +24,7 @@ jobs: - name: Auto Approve Pull Requests env: + PULL_REQUEST_NUMBER: ${{ github.event.number }} GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} GITHUB_REPOSITORY: ${{ github.repository }} GITHUB_REF: ${{ github.event.pull_request.head.ref }} diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index a2edc71..a6d39ab 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -12,11 +12,12 @@ # Get the repository and pull request from the github context repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) -pull_request_number = os.getenv('GITHUB_REF').split('/')[-1] -if pull_request_number != 'merge': - pull_request = repository.get_pull(int(pull_request_number)) -else: - print("Pull request not found") +pull_request_number = os.getenv('PULL_REQUEST_NUMBER') +#os.getenv('GITHUB_REF').split('/')[-1] +#if pull_request_number != 'merge': +# pull_request = repository.get_pull(int(pull_request_number)) +#else: +# print("Pull request not found") # Get the list of files changed in the pull request files_changed = [file.filename for file in pull_request.get_files()] From 25727505750e44c4e1ab1d432dbf3100671bce85 Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 12:13:43 +0100 Subject: [PATCH 08/12] update script and test 04 --- scripts/python_auto_pr_approve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index a6d39ab..0eb0b7d 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -12,7 +12,7 @@ # Get the repository and pull request from the github context repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) -pull_request_number = os.getenv('PULL_REQUEST_NUMBER') +pull_request = os.getenv('PULL_REQUEST_NUMBER') #os.getenv('GITHUB_REF').split('/')[-1] #if pull_request_number != 'merge': # pull_request = repository.get_pull(int(pull_request_number)) From 323e4dfbcee3047df9ed954252518c7b029a4db2 Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 12:15:07 +0100 Subject: [PATCH 09/12] update script and test 05 --- scripts/python_auto_pr_approve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index 0eb0b7d..e67e317 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -12,7 +12,7 @@ # Get the repository and pull request from the github context repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) -pull_request = os.getenv('PULL_REQUEST_NUMBER') +pull_request = os.getenv(int('PULL_REQUEST_NUMBER')) #os.getenv('GITHUB_REF').split('/')[-1] #if pull_request_number != 'merge': # pull_request = repository.get_pull(int(pull_request_number)) From 220e857a5b2759f6d8e825951c10e185b95c4daa Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 12:17:43 +0100 Subject: [PATCH 10/12] update script and test 06 --- scripts/python_auto_pr_approve.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index e67e317..74c6e91 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -12,7 +12,9 @@ # Get the repository and pull request from the github context repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) -pull_request = os.getenv(int('PULL_REQUEST_NUMBER')) +pull_request_number = os.getenv('PULL_REQUEST_NUMBER') +pull_request = int(pull_request_number) + #os.getenv('GITHUB_REF').split('/')[-1] #if pull_request_number != 'merge': # pull_request = repository.get_pull(int(pull_request_number)) From 2e56d8f4af1b6036e636cc10eeda0100eb02806a Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 12:20:03 +0100 Subject: [PATCH 11/12] update script and test 07 --- scripts/python_auto_pr_approve.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index 74c6e91..5ff8449 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -12,8 +12,7 @@ # Get the repository and pull request from the github context repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) -pull_request_number = os.getenv('PULL_REQUEST_NUMBER') -pull_request = int(pull_request_number) +pull_request = int(os.getenv('PULL_REQUEST_NUMBER')) #os.getenv('GITHUB_REF').split('/')[-1] #if pull_request_number != 'merge': From 4e3b6565a8704cda7d909f3a6454913da34f682b Mon Sep 17 00:00:00 2001 From: Nataniel Almeida Date: Fri, 9 Aug 2024 12:26:15 +0100 Subject: [PATCH 12/12] update script and test 08 --- scripts/python_auto_pr_approve.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/python_auto_pr_approve.py b/scripts/python_auto_pr_approve.py index 5ff8449..e75e991 100644 --- a/scripts/python_auto_pr_approve.py +++ b/scripts/python_auto_pr_approve.py @@ -12,7 +12,8 @@ # Get the repository and pull request from the github context repository = gh.get_repo(os.getenv('GITHUB_REPOSITORY')) -pull_request = int(os.getenv('PULL_REQUEST_NUMBER')) +pull_request_number = int(os.getenv('PULL_REQUEST_NUMBER')) +pull_request = repository.get_pull(int(pull_request_number)) #os.getenv('GITHUB_REF').split('/')[-1] #if pull_request_number != 'merge':