|
| 1 | +from typing import Generator |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +from github import UnknownObjectException |
| 6 | +from github.PullRequest import PullRequest |
| 7 | + |
| 8 | +import github_util |
| 9 | + |
| 10 | +github = github_util.load_github() |
| 11 | + |
| 12 | + |
| 13 | +def list_all_json_files() -> Generator[str, None, None]: |
| 14 | + base_dir = 'pom_vulnerability/save_points' |
| 15 | + directory = os.fsencode(base_dir) |
| 16 | + for file in os.listdir(directory): |
| 17 | + filename = os.fsdecode(file) |
| 18 | + if filename.startswith('g__') and filename.endswith('.json'): |
| 19 | + yield base_dir + '/' + filename |
| 20 | + |
| 21 | + |
| 22 | +def read_results_data(json_file_name: str): |
| 23 | + with open(json_file_name) as jsonFile: |
| 24 | + data = json.load(jsonFile) |
| 25 | + return data |
| 26 | + |
| 27 | + |
| 28 | +repo_removed = 0 |
| 29 | +repo_archived = 0 |
| 30 | +not_found = 0 |
| 31 | +merged_prs = 0 |
| 32 | +closed_prs = 0 |
| 33 | +open_prs = 0 |
| 34 | + |
| 35 | +project_name_to_fork = {} |
| 36 | + |
| 37 | +for file in list_all_json_files(): |
| 38 | + data = read_results_data(file) |
| 39 | + project_name: str = data['project_name'] |
| 40 | + print(f'loading project: {project_name}') |
| 41 | + pull_url = data['pull_request'] |
| 42 | + if pull_url == '': |
| 43 | + continue |
| 44 | + |
| 45 | + |
| 46 | + def add_entry_to_remove(): |
| 47 | + if not project_name.lower().startswith('jlleitschuh'): |
| 48 | + fork_name = project_name.split('/')[-1] |
| 49 | + project_name_to_fork[project_name] = f'jlleitschuh/{fork_name}' |
| 50 | + |
| 51 | + |
| 52 | + try: |
| 53 | + repository = github.get_repo(project_name) |
| 54 | + except UnknownObjectException as e: |
| 55 | + repo_removed += 1 |
| 56 | + add_entry_to_remove() |
| 57 | + continue |
| 58 | + if repository.archived: |
| 59 | + repo_archived += 1 |
| 60 | + pull_number = int(str(pull_url).split('/')[-1]) |
| 61 | + try: |
| 62 | + pull_request: PullRequest = repository.get_pull(pull_number) |
| 63 | + except UnknownObjectException as e: |
| 64 | + not_found += 1 |
| 65 | + add_entry_to_remove() |
| 66 | + continue |
| 67 | + if pull_request.merged: |
| 68 | + merged_prs += 1 |
| 69 | + add_entry_to_remove() |
| 70 | + elif pull_request.state == 'closed': |
| 71 | + closed_prs += 1 |
| 72 | + add_entry_to_remove() |
| 73 | + elif pull_request.state == 'open': |
| 74 | + open_prs += 1 |
| 75 | + # Don't add a removal entry, in this state, the fork should not be deleted |
| 76 | + |
| 77 | +print(f'Stats:') |
| 78 | +print(f'\tRepo (Removed {repo_removed}, Archived {repo_archived})') |
| 79 | +print(f'\tPR (Open {open_prs}, Not Found {not_found}, Merged {merged_prs}, Closed {closed_prs})') |
| 80 | + |
| 81 | +# deleted = 0 |
| 82 | +# for project in project_name_to_fork: |
| 83 | +# my_project_name = project_name_to_fork[project] |
| 84 | +# try: |
| 85 | +# my_repo = github.get_repo(my_project_name) |
| 86 | +# except UnknownObjectException as e: |
| 87 | +# continue |
| 88 | +# print(f'Deleting {my_project_name}') |
| 89 | +# my_repo.delete() |
| 90 | +# deleted += 1 |
| 91 | +# |
| 92 | +# print(f'Forks Deleted {deleted}') |
0 commit comments