This repository was archived by the owner on Apr 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmirror.py
56 lines (45 loc) · 1.5 KB
/
mirror.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
import requests
import os
import github
import json
import sys
from loguru import logger
gitea_token = os.getenv("GITEA_TOKEN")
gitea_url = os.getenv("GITEA_URL")
mirror_org = os.getenv("MIRROR_ORG")
github_token = os.getenv("GITHUB_TOKEN")
gitea_api_url = f"{gitea_url}/api/v1"
def migrate(repo, headers, org_id):
body = {
"clone_addr": repo.clone_url,
"description": repo.description,
"mirror": True,
"private": False,
"repo_name": repo.name,
"uid": org_id,
}
r = requests.post(f"{gitea_api_url}/repos/migrate", json=body, headers=headers)
if r.status_code == 201:
logger.info(f"Created {repo.name} mirror from GitHub")
else:
logger.error(f"Could not create {repo.name}. Skipping.")
def main():
headers = {
"accept": "application/json",
"Authorization": f"token {gitea_token}",
"Content-Type": "application/json",
}
gh = github.Github(github_token)
repos = gh.get_user().get_repos(affiliation="owner", visibility="public")
org_id = json.loads(
requests.get(f"{gitea_api_url}/orgs/{mirror_org}", headers=headers).text
)["id"]
for repo in repos:
if not repo.fork:
migrate(repo, headers, org_id)
if __name__ == "__main__":
required_vars = ["GITEA_URL", "GITEA_TOKEN", "GITHUB_TOKEN", "MIRROR_ORG"]
for var in required_vars:
if not os.getenv(var):
sys.exit(f"Required environment variables: {', '.join(required_vars)}")
main()