about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2023-01-03 17:49:35 -0800
committerFranck Cuny <franck@fcuny.net>2023-01-03 17:49:35 -0800
commit282a22cb76acb6687e2eaf98357ec3923c14829a (patch)
treec97ad20c29680eb7fdd603eac7a6c97e6ab646e3
parentfeat: update to nix 22.11 (diff)
downloadworld-282a22cb76acb6687e2eaf98357ec3923c14829a.tar.gz
feat: script to delete repositories on GitHub
Delete repositories that are marked as private in GitHub and already
exists in my gitea instance.
-rwxr-xr-xtools/import-gh-to-gitea/delete-gh-repositories.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/tools/import-gh-to-gitea/delete-gh-repositories.py b/tools/import-gh-to-gitea/delete-gh-repositories.py
new file mode 100755
index 0000000..b87c0f6
--- /dev/null
+++ b/tools/import-gh-to-gitea/delete-gh-repositories.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python3.10
+
+import argparse
+
+import requests
+
+
+def main(gitea_api_token, gh_api_token):
+    gitea = requests.Session()
+    gitea.headers.update({"Authorization": f"token {gitea_api_token}"})
+    gitea.headers.update({"Accept": "application/json"})
+    gitea.headers.update({"Content-Type": "application/json"})
+
+    not_done = True
+    page = 1
+
+    gitea_repos = []
+    while not_done:
+        url = f"https://git.fcuny.net/api/v1/user/repos?page={page}&limit=10"
+        res = gitea.get(
+            url,
+            timeout=5,
+        )
+        res.raise_for_status()
+
+        repos = res.json()
+        if len(repos) == 0:
+            not_done = False
+        else:
+            page = page + 1
+
+        for repo in repos:
+            name = repo.get("name")
+            gitea_repos.append(name)
+
+    github = requests.Session()
+    github.headers.update({"Authorization": f"token {gh_api_token}"})
+    github.headers.update({"Accept": "application/vnd.github.v3+json"})
+
+    not_done = True
+    page = 1
+    github_repos = []
+    while not_done:
+        url = f"https://api.github.com/user/repos?page={page}&type=all"
+        res = github.get(
+            url,
+            timeout=5,
+        )
+        res.raise_for_status()
+        repos = res.json()
+        if len(repos) == 0:
+            not_done = False
+        else:
+            page = page + 1
+
+        for repo in repos:
+            name = repo.get("name")
+            if (
+                repo.get("owner").get("login") == "fcuny"
+                and repo.get("private") == True
+            ):
+                github_repos.append(name)
+
+    for repo in github_repos:
+        if repo in gitea_repos:
+            url = f"https://api.github.com/repos/fcuny/{repo}"
+            print(f"deleting {url}")
+            res = github.delete(
+                url,
+                timeout=5,
+            )
+            res.raise_for_status()
+
+
+if __name__ == "__main__":
+    argp = argparse.ArgumentParser()
+    argp.add_argument("-t", "--gt-file", nargs=1, type=argparse.FileType("r"))
+    argp.add_argument("-T", "--gh-file", nargs=1, type=argparse.FileType("r"))
+
+    args = argp.parse_args()
+    gitea_api_token = args.gt_file[0].readline().strip()
+    github_api_token = args.gh_file[0].readline().strip()
+
+    main(gitea_api_token, github_api_token)