about summary refs log tree commit diff
path: root/tools/import-gh-to-gitea/archive-projects.py
blob: 41bd8988c837f1fe3195743b30ff3ebbf83ca1a9 (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
#!/usr/bin/env python3

import argparse

import requests


def main(api_token):
    s = requests.Session()
    s.headers.update({"Authorization": f"token {api_token}"})
    s.headers.update({"Accept": "application/json"})
    s.headers.update({"Content-Type": "application/json"})

    not_done = True
    page = 1
    while not_done:
        url = f"https://git.fcuny.net/api/v1/user/repos?page={page}&limit=10"
        res = s.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:
            if repo.get("owner").get("login") == "attic":
                if repo.get("archived") is False:
                    name = repo.get("name")
                    data = {"archived": True}
                    res = s.patch(
                        f"https://git.fcuny.net/api/v1/repos/attic/{name}", json=data
                    )
                    res.raise_for_status()
                    print(f"set {name} to archived: {res.status_code}")


if __name__ == "__main__":
    argp = argparse.ArgumentParser()
    argp.add_argument("-t", "--token-file", nargs=1, type=argparse.FileType("r"))

    args = argp.parse_args()
    api_token = args.token_file[0].readline().strip()

    main(api_token)