feat: implement basic migrator

This commit is contained in:
2024-09-27 14:39:13 +02:00
commit c4b0c32a88
2 changed files with 56 additions and 0 deletions

10
README.md Normal file
View File

@@ -0,0 +1,10 @@
# Description
Collection of scripts to migrate GitHub repositories to GitTea as mirrors.
# Environment Variables
API_RESPONSE_FILE="github-api-response.json"
GITEA_URL = "https://<your_domain>/api/v1/"
GITEA_USER = <gitea user>
GITEA_PASS = <gitea passwort, if you use external OIDC, you need to set on after logging in>
GITHUB_TOKEN = <github token created in developer settings with read_code & metadata>

46
read_and_link.py Normal file
View File

@@ -0,0 +1,46 @@
import json
import requests
import os
API_RESPONSE_FILE="github-api-response.json"
GITEA_URL = "https://git.athq.de/api/v1/"
GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]
GITEA_PASS = os.environ["GITEA_PASS"]
GITEA_USER = os.environ["GITEA_USER"]
if __name__ == "__main__":
# query API if file not present #
if not os.path.isfile(API_RESPONSE_FILE):
r = requests.get("https://api.github.com/search/repositories?q=user:FAUSheppy",
headers={'Authorization': "token {}".format(GITHUB_TOKEN)})
print('Authorization: '+ "token {}".format(GITHUB_TOKEN))
with open(API_RESPONSE_FILE, "w") as f:
f.write(json.dumps(r.json(), indent=2))
# load file #
with open(API_RESPONSE_FILE) as f:
content = json.load(f)
for item in content["items"]:
payload = {
"auth_token" : GITHUB_TOKEN,
"mirror" : True,
"repo_name" : item["name"],
"clone_addr" : item["html_url"],
"description" : item["description"]
}
print(json.dumps(payload, indent=2))
r = requests.post(GITEA_URL + "/repos/migrate", auth=(GITEA_USER, GITEA_PASS), json=payload)
try:
r.raise_for_status()
print("Create: {}".format(item["name"]))
except requests.exceptions.HTTPError as e:
if r.status_code == 409:
print("Repo already created")
else:
raise e