Skip to content

Commit addd53e

Browse files
update rit github update repo formula to treat account with +100 repos
Signed-off-by: GuillaumeFalourd <[email protected]>
1 parent c54b326 commit addd53e

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

github/update/repo/src/formula/formula.py

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,62 @@
77
def run(token, username, default_branch, repos):
88

99
url_repos = f"https://api.github.com/user/repos?type=owner&per_page=100&sort=full_name"
10-
10+
1111
authorization = f"token {token}"
1212
headers = {
1313
"Accept": "application/vnd.github.v3+json",
1414
"Authorization" : authorization,
1515
}
1616

17-
r1 = requests.get(
18-
url = url_repos,
19-
headers = headers
20-
)
17+
all_repositories = get_repositories(url_repos, headers)
2118

22-
if r1.status_code == 200:
23-
datas = r1.json()
24-
all_repositories = []
19+
if repos == "all":
20+
repositories = all_repositories
21+
22+
else: # I want to select
23+
question1 = [
24+
inquirer.Checkbox("repositories",
25+
message = f"\033[1m\033[36m{username}\033[0m \033[1mrepository to delete:\033[0m ",
26+
choices = all_repositories,
27+
),
28+
]
29+
answer = inquirer.prompt(question1)
30+
repositories = answer["repositories"]
2531

26-
for d in datas:
27-
all_repositories.append(d["name"])
32+
for repository in repositories:
33+
url_heads = f"https://api.github.com/repos/{username}/{repository}/git/refs/heads"
2834

29-
if repos == "all":
30-
repos = all_repositories
31-
32-
else: # I want to select
33-
question1 = [
34-
inquirer.Checkbox("repositories",
35-
message = f"\033[1m\033[36m{username}\033[0m \033[1mrepository to delete:\033[0m ",
36-
choices = all_repositories,
37-
),
38-
]
39-
answer = inquirer.prompt(question1)
40-
repositories = answer["repositories"]
41-
42-
for repository in repositories:
43-
url_heads = f"https://api.github.com/repos/{username}/{repository}/git/refs/heads"
35+
r = requests.get(
36+
url = url_heads,
37+
headers = headers
38+
)
4439

45-
r2 = requests.get(
46-
url = url_heads,
47-
headers = headers
48-
)
49-
50-
datas = r2.json()
51-
branches = []
40+
datas = r.json()
41+
branches = []
42+
43+
for data in datas:
44+
branch_name = re.search("(?<=refs\/heads\/).*", data["ref"]).group()
45+
branches.append(branch_name)
5246

53-
for data in datas:
54-
branch_name = re.search("(?<=refs\/heads\/).*", data["ref"]).group()
55-
branches.append(branch_name)
56-
57-
if default_branch in branches:
58-
print(f"\n🛠 Updating default branch for \033[36m{repository}\033[0m...")
59-
input_flag_cmd = f"rit github update default-branch --rit_repo_owner=\"{username}\" --rit_git_repo=\"{repository}\" --rit_repo_branch=\"{default_branch}\""
60-
os.system(f"{input_flag_cmd}")
61-
else:
62-
print(f"\n🛠 Creating new \033[36m{default_branch}\033[0m branch for \033[36m{repository}\033[0m...")
63-
input_flag_cmd = f"rit github create branch --rit_repo_owner=\"{username}\" --rit_git_repo=\"{repository}\" --rit_repo_branch=\"{default_branch}\" --rit_branch_default=\"yes\""
64-
os.system(f"{input_flag_cmd}")
47+
if default_branch in branches:
48+
print(f"\n🛠 Updating default branch for \033[36m{repository}\033[0m...")
49+
input_flag_cmd = f"rit github update default-branch --rit_repo_owner=\"{username}\" --rit_git_repo=\"{repository}\" --rit_repo_branch=\"{default_branch}\""
50+
os.system(f"{input_flag_cmd}")
51+
else:
52+
print(f"\n🛠 Creating new \033[36m{default_branch}\033[0m branch for \033[36m{repository}\033[0m...")
53+
input_flag_cmd = f"rit github create branch --rit_repo_owner=\"{username}\" --rit_git_repo=\"{repository}\" --rit_repo_branch=\"{default_branch}\" --rit_branch_default=\"yes\""
54+
os.system(f"{input_flag_cmd}")
55+
56+
def get_repositories(url, headers):
57+
result = []
58+
r = requests.get(
59+
url = url,
60+
headers = headers
61+
)
62+
if "next" in r.links :
63+
result += get_repositories(r.links["next"]["url"], headers)
64+
65+
for repository in r.json():
66+
result.append(repository.get("name"))
67+
68+
return result

0 commit comments

Comments
 (0)