Created
May 31, 2023 08:06
-
-
Save AlexBaranowski/5ddec78070cf363fdc8c27345d05a4db to your computer and use it in GitHub Desktop.
Removes ALL GitHub repositories for selected organization. Requires Access Token (provided by env variable GH_TOKEN) with admin right/delete repositories rights. Scripts uses python requests and os module only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
GH_TOKEN = os.environ['GH_TOKEN'] | |
GH_HEADERS = {"Accept": "application/vnd.github+json", | |
"Authorization": f"Bearer {GH_TOKEN}", | |
"X-GitHub-Api-Version": "2022-11-28"} | |
ORG_NAME = "TODO-FIXME-YOUR-ORG-NAME" | |
REPO_SET=set() | |
def get_repolist(): | |
max_request = 100 | |
page = 0 | |
# python does not have do while, so we can use while TRUE or copy body of loop before while | |
# in case of error max_requests won't 'eat' all GH rate limits for next hour | |
while page < max_request: | |
r_url = f"https://api.github.com/orgs/{ORG_NAME}/repos?page={page}&per_page=100" | |
rv = requests.get(r_url, headers=GH_HEADERS) | |
# response code != 200 or empty array (no more repos to paginate) | |
if rv.status_code != 200 or len(rv.json()) == 0: | |
break | |
page += 1 | |
for i in rv.json(): | |
REPO_SET.add(i['name']) | |
def remove_repositories(): | |
for repo_name in REPO_SET: | |
print(f"Removing {repo_name} repository") | |
r_url = f"https://api.github.com/repos/{ORG_NAME}/{repo_name}" | |
rv = requests.delete(r_url, headers=GH_HEADERS) | |
# useful for debugging | |
print(rv.text) | |
print(rv.status_code) | |
def main(): | |
get_repolist() | |
remove_repositories() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment