-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·63 lines (49 loc) · 1.91 KB
/
main.py
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
#!/usr/bin/env python
import os
import requests
import sys
from tempfile import mkdtemp
from git import Repo
from tqdm import tqdm
from github.repository_query import RepositoryQuery as GithubRepositoryQuery
from sourcehut.client import Client as SourcehutClient
def main():
github_token = os.getenv("GITHUB_API_KEY")
sourcehut_token = os.getenv("SOURCEHUT_ACCESS_TOKEN")
if github_token is None or sourcehut_token is None:
print(
"Please ensure GITHUB_API_KEY and SOURCEHUT_API_KEY are exported in the environment."
)
sys.exit(1)
sourcehut_client = SourcehutClient(token=sourcehut_token)
github_repos = GithubRepositoryQuery(token=github_token)
tmpd = mkdtemp("-github-repos")
for repo in tqdm(github_repos.iterator()):
if not repo["viewerCanAdminister"] or repo["isArchived"] or repo["isDisabled"]:
pass
# TODO: Check if repository already exists already in sr.ht
name = repo["name"]
clone_to = f"{tmpd}/{name}"
print(f"Cloning {name} to {clone_to}")
print(f"Creating new repository in Sourcehut for: {name}")
res = sourcehut_client.post(
"/api/repos",
{
"name": name,
"description": repo["description"],
"visibility": "private" if repo["isPrivate"] else "public",
},
)
# TODO: Handle response errors etc.
cloned_repo = Repo.clone_from(repo["sshUrl"], f"{tmpd}/{name}")
remote = cloned_repo.create_remote(
name="sourcehut",
# TODO: This shouldn't be hardcoded, we can query the sh API for this
url=f"git@git.sr.ht:~w1lkins/{name}",
)
print(f"Pushing {name} to sourcehut.")
remote.push(refspec="master:master")
# TODO: Ask user if they want to cleanup the directory
os.removedirs(tmpd)
if __name__ == "__main__":
main()