about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-11-05 12:25:28 -0700
committerFranck Cuny <franck@fcuny.net>2022-11-05 12:25:28 -0700
commitb46791f7fd45d0d12a46f2ed18f2756cf013dbc0 (patch)
tree3731276421ad7d8be236e5c47dc35c13392a3c45
parentfeat(hosts/tahoe): enable gitea again (diff)
downloadworld-b46791f7fd45d0d12a46f2ed18f2756cf013dbc0.tar.gz
feat(tools/import-gh-to-gitea): script to import my repositories
I'm going to move (again) from GH to my own instance of gitea. This
script does the migration for me.
Diffstat (limited to '')
-rwxr-xr-xtools/import-gh-to-gitea/import-gh-to-gitea.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/tools/import-gh-to-gitea/import-gh-to-gitea.py b/tools/import-gh-to-gitea/import-gh-to-gitea.py
new file mode 100755
index 0000000..b59c8eb
--- /dev/null
+++ b/tools/import-gh-to-gitea/import-gh-to-gitea.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+
+
+import argparse
+
+import requests
+
+
+def main(gh_api_token, gitea_api_token):
+    s = requests.Session()
+    s.headers.update({"Authorization": f"token {gh_api_token}"})
+    s.headers.update({"Accept": "application/vnd.github.v3+json"})
+
+    # hardcoded number of items per page, pagination is not handled.
+    res = s.get("https://api.github.com/user/repos?per_page=200&type=all", timeout=5)
+    res.raise_for_status()
+
+    repos = res.json()
+
+    gts = requests.Session()
+    gts.headers.update({"Accept": "application/json"})
+    gts.headers.update({"Content-Type": "application/json"})
+    gts.headers.update({"Authorization": f"token {gitea_api_token}"})
+    for repo in repos:
+        # archived projects go to the attic.
+        owner = ""
+        if repo.get("archived"):
+            owner = "attic"
+        else:
+            owner = "fcuny"
+
+        data = {
+            "auth_username": "fcuny",
+            "auth_token": gh_api_token,
+            "clone_addr": repo.get("html_url"),
+            "mirror": False,
+            "private": repo.get("private"),
+            "repo_name": repo.get("name"),
+            "repo_owner": owner,
+            "service": "git",
+            "description": repo.get("description"),
+        }
+        print(f"importing {data['repo_name']} from {data['clone_addr']}")
+        res = gts.post(
+            "https://git.fcuny.net/api/v1/repos/migrate",
+            json=data,
+        )
+        try:
+            res.raise_for_status()
+        except Exception as e:
+            print(f"failed for {data['repo_name']} with {e}")
+
+
+if __name__ == "__main__":
+    argp = argparse.ArgumentParser()
+    argp.add_argument("-g", "--gh-token-file", nargs=1, type=argparse.FileType("r"))
+    argp.add_argument("-G", "--gitea-token-file", nargs=1, type=argparse.FileType("r"))
+    args = argp.parse_args()
+
+    gh_api_token = args.gh_token_file[0].readline().strip()
+    gitea_api_token = args.gitea_token_file[0].readline().strip()
+    main(gh_api_token, gitea_api_token)