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