-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanRepos.py
123 lines (96 loc) · 3.56 KB
/
cleanRepos.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
import string
from subprocess import call
import json
import requests
from datetime import datetime
import threading
##################$##
# Make sure to add #
# to this list #
# anything you do #
# not want deleted #
# from the org. #
#####################
filterList = ['SuperImportantRepo', 'PlsDontDeleteMe']
orgname = "MyVerySpecialOrganization"
st = datetime.now().strftime('%Y-%m-%d')
xxx = input("Access token (https://github.com/settings/tokens/new): ")
GitHubUser = input("GitHub Username: ")
menu = '''Menu:
1. Delete all repos
2. Delete all project repos
3. Delete all personal repos
Choice:
'''
ans = input(menu)
# Returns a list a given element from all git repos (ie, all of their ssh_url's)
def getRepo(element):
data = []
for page in range(1, 100): # Technically limited to 10,000 repos, but come on.
# Get next page of repos
responce = json.loads(requests.get('https://api.github.com/orgs/' + orgname + '/repos?per_page=500&access_token=' + xxx + '&page=' + str(page)).text)
for d in responce:
data.append(d[element])
# Stop when there are no more repos
if responce == []:
break
return data
# Backup all repos in gits/ and put a listing of all of them in ./repos.txt
def backupGroup(group):
for repo in group:
command = "cd /tmp/" + st + "; git clone https://" + GitHubUser + ":" + xxx + "@github.com/" + repo
call(command, shell=True)
def backup():
call("mkdir /tmp/" + st, shell=True)
repos = getRepo("full_name")
workers = 8
threads = [threading.Thread(target=backupGroup, args=[repos[i::workers]]) for i in range(workers)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
command = "ls /tmp/" + st + "/ > repos.txt"
call(command, shell=True)
def getList(filt):
people = open("repos.txt", 'r').readlines()
for i in range(len(people)):
people[i] = "".join([c for c in people[i] if c in string.ascii_letters + string.digits + '-'])
people = [repo for repo in people if repo not in filt]
return people
def confirmDelete():
ans = input("Done backing up, continue to deleting? [y/N]")
if ans != "y" and ans != "Y":
quit()
if ans == '1':
ans = input("Finally, verify that the only things you want filtered are: " + str(filterList) + "\nContinue? [y/N] ")
if ans != "y" and ans != "Y":
quit()
print("Backing up")
backup()
confirmDelete()
for repo in getList(filterList):
command = "curl -XDELETE -H 'Authorization: token " + xxx + "' https://api.github.com/repos/" + orgname + "/" + repo
print(command)
call(command, shell=True)
elif ans == '2':
print("Backing up")
backup()
confirmDelete()
for repo in getList(filterList):
if repo[:len("project")] == "project":
print(repo)
command = "curl -XDELETE -H 'Authorization: token " + xxx + "' https://api.github.com/repos/" + orgname + "/" + repo
call(command, shell=True)
elif ans == '3':
print("Backing up")
backup()
confirmDelete()
for repo in getList(filterList):
if not repo[:len("project")] == "project":
command = "curl -XDELETE -H 'Authorization: token " + xxx + "' https://api.github.com/repos/" + orgname + "/" + repo
print(command)
call(command, shell=True)
else:
print("Ahhh")
quit()