about summary refs log tree commit diff
path: root/packages/import-gh-to-gitea/delete-gh-repositories.py
blob: b87c0f6b9ff1f56508f693fdc8fab3b519f3bbc9 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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)