|
| 1 | +from github import Github |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +def commit_and_push_changes(repo, branch, commit_message): |
| 6 | + try: |
| 7 | + repo.git.add(update=True) |
| 8 | + repo.index.commit(commit_message) |
| 9 | + origin = repo.remote(name='origin') |
| 10 | + origin.push(refspec=branch) |
| 11 | + print("Changes committed and pushed successfully.") |
| 12 | + except Exception as e: |
| 13 | + print(f"Error occurred while committing and pushing changes: {str(e)}") |
| 14 | + |
| 15 | +def main(): |
| 16 | + # GitHub authentication using personal access token |
| 17 | + # Replace 'YOUR_PERSONAL_ACCESS_TOKEN' with your actual token |
| 18 | + g = Github(os.environ.get('GITHUB_REPOSITORY')) |
| 19 | + |
| 20 | + # Get repository and pull request number from environment variables |
| 21 | + repo_name = os.environ.get('GITHUB_REPOSITORY') |
| 22 | + pull_request_number = os.environ.get('GITHUB_REF').split('/')[-1] |
| 23 | + |
| 24 | + if not repo_name or not pull_request_number: |
| 25 | + print("Repository name or pull request number not found in environment variables.") |
| 26 | + sys.exit(1) |
| 27 | + |
| 28 | + # Get repository object |
| 29 | + repo = g.get_repo(repo_name) |
| 30 | + |
| 31 | + # Get the pull request object |
| 32 | + pr = repo.get_pull(int(pull_request_number)) |
| 33 | + |
| 34 | + # Get the list of changed files in the pull request |
| 35 | + changed_files = [file.filename for file in pr.get_files()] |
| 36 | + |
| 37 | + print(changed_files) |
| 38 | + # Filter the list to include only JSON files in the 'tests' directory |
| 39 | + # changed_json_files = [file for file in changed_files if file.startswith('tests/') and file.endswith('.json')] |
| 40 | + |
| 41 | + # if changed_json_files: |
| 42 | + # # Commit and push changes |
| 43 | + # commit_message = "Update JSON files" |
| 44 | + # commit_and_push_changes(repo, pr.base.ref, commit_message) |
| 45 | + # else: |
| 46 | + # print("No changes detected in JSON files.") |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + main() |
0 commit comments