-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.py
More file actions
74 lines (60 loc) · 2.08 KB
/
application.py
File metadata and controls
74 lines (60 loc) · 2.08 KB
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
import sys
from github import Github
username = input('GitHub username: ')
password = input('GitHub password: ')
github_user = input('GitHub user/ organization: ')
github_repo = input('GitHub repository name: ')
per_page = int(input('How many pulls fetched per page? (30) ') or "30")
page = int(input('Pick start page: (0) ') or "0")
auto_delete = input('Automatic delete merged repo? (y/n): ')
def validate_bool_input(bool_input):
if auto_delete is "y" or auto_delete is "n":
return True
else:
print('automatic delete must be "y" or "n"!')
return False
# check branch validity
def check(branch):
try:
ref = repo.get_git_ref(branch)
return ref
except:
print("branch " + branch + " is not found")
return None
# delete branch
def delete(ref):
try:
ref.delete()
print(ref.ref + " has been deleted")
except:
pass
# validate auto delete input
if not validate_bool_input(auto_delete):
sys.exit(1)
# create GitHub object
g = Github(username, password, per_page=per_page)
# define repo and pull requests
print("get repo from {}/{}".format(github_user, github_repo))
repo = g.get_user(github_user).get_repo(github_repo)
while True:
print("get pull requests from page {} with {} pull(s) per page".format(page, per_page))
pulls = repo.get_pulls("closed").get_page(page)
print("get head refs")
refs = ["heads/{}".format(pull.head.ref) for pull in pulls]
# break the loop
if len(refs) is 0:
print("page {} is empty, done cleaning".format(page))
break
print("validate {} branche(s)".format(len(refs)))
check_valids = [check(ref) for ref in refs]
print("remove invalid branche(s)")
valids = [val for val in check_valids if val is not None]
print("perform deletion for {} branche(s)".format(len(valids)))
for valid in valids:
if auto_delete is "y":
delete(valid)
else:
is_del = input("Do you want to delete branch " + valid.ref + "? (y/n): ")
if is_del is "y":
delete(valid)
page += 1