|
7 | 7 | def run(token, username, default_branch, repos): |
8 | 8 |
|
9 | 9 | url_repos = f"https://api.github.com/user/repos?type=owner&per_page=100&sort=full_name" |
10 | | - |
| 10 | + |
11 | 11 | authorization = f"token {token}" |
12 | 12 | headers = { |
13 | 13 | "Accept": "application/vnd.github.v3+json", |
14 | 14 | "Authorization" : authorization, |
15 | 15 | } |
16 | 16 |
|
17 | | - r1 = requests.get( |
18 | | - url = url_repos, |
19 | | - headers = headers |
20 | | - ) |
| 17 | + all_repositories = get_repositories(url_repos, headers) |
21 | 18 |
|
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"] |
25 | 31 |
|
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" |
28 | 34 |
|
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 | + ) |
44 | 39 |
|
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) |
52 | 46 |
|
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