about summary refs log tree commit diff
path: root/packages/import-gh-to-gitea/import-gh-to-gitea.py
blob: b59c8ebb4857649d279aa1dbcbd49ec88240a54d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)