From ca18d9d61a9a74c63ad5a113d6a4cbe188541723 Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 12 Oct 2020 17:07:51 +0530 Subject: [PATCH] Improved. --- helper.py | 6 +++- mirror-forked-repository.py | 68 +++++++++++-------------------------- 2 files changed, 25 insertions(+), 49 deletions(-) diff --git a/helper.py b/helper.py index c7fcc4d..308bb2f 100644 --- a/helper.py +++ b/helper.py @@ -6,7 +6,11 @@ import json import sys import os -config = json.loads(open(os.path.expanduser("./config.json")).read().strip()) +THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) +config = json.loads(open(os.path.expanduser("{0}/config.json".format(THIS_FOLDER))).read().strip()) + +def getConfig(): + return config def giteaHost(endPoint): return "{0}/api/v1/{1}".format(config['gitea']['host'],endPoint) diff --git a/mirror-forked-repository.py b/mirror-forked-repository.py index 8d96e1c..906ef44 100644 --- a/mirror-forked-repository.py +++ b/mirror-forked-repository.py @@ -1,53 +1,35 @@ #!/usr/bin/env python # https://github.com/PyGithub/PyGithub -from github import Github -import requests import json import sys import os +from helper import getConfig,giteaSetRepoTopics,giteaSession,giteaCreateRepo,ghApi,giteaCreateOrg,giteaGetUser,config -config = json.loads(open(os.path.expanduser("./config.json")).read().strip()) +config = getConfig() repo_map = config['repomap'] -session = requests.Session() -session.headers.update({ - "Content-type" : "application/json", - "Authorization" : "token {0}".format(config['gitea']['accesstoken']), -}) - -gh = Github(config['github']['accesstoken']) - -def createOrgInGitea(orgname): - body = { - 'full_name' : orgname, - 'username' : orgname, - } - - jsonstring = json.dumps(body) - - r = session.post("{0}/api/v1/orgs/".format(config['gitea']['host'] ), data=jsonstring) - if r.status_code != 201: - jsonstring = json.dumps(r.text) - print("Cannot Create ORG '{0}' Status {1}".format(orgname,jsonstring), file=sys.stderr) - exit(1) - - return json.loads(r.text)["id"] +session = giteaSession() +gh = ghApi() for repo in gh.get_user().get_repos(): if repo.fork: real_repo = repo.full_name.split('/')[1] + gitea_dest_user = repo.owner.login + repo_owner=repo.owner.login + + print('Forked Repository : {0}'.format(repo.full_name)) + if real_repo in repo_map: gitea_dest_user = repo_map[real_repo] - else: - gitea_dest_user = repo.owner.login - r = session.get("{0}/api/v1/users/{1}".format(config['gitea']['host'],gitea_dest_user )) - if r.status_code != 200: - gitea_uid = createOrgInGitea(gitea_dest_user) - else: - gitea_uid = json.loads(r.text)["id"] + gitea_uid = giteaGetUser(gitea_dest_user) + + if gitea_uid == 'failed': + gitea_uid = giteaCreateOrg(gitea_dest_user) + + repo_name = "{0}".format(real_repo) m = { - "repo_name" : "{0}".format(real_repo), + "repo_name" : repo_name, "description" : (repo.description or "not really known")[:255], "clone_addr" : repo.clone_url, "mirror" : True, @@ -55,17 +37,7 @@ for repo in gh.get_user().get_repos(): "uid" : gitea_uid, } - if repo.private: - m["auth_username"] = config['github']['username'] - m["auth_password"] = "{0}".format(config['github']['accesstoken']) - - jsonstring = json.dumps(m) - - r = session.post("{0}/api/v1/repos/migrate".format(config['gitea']['host']), data=jsonstring) - - if r.status_code == 201: - print("[Success] : {0} Repository Created\n\r".format(repo.full_name)) - elif r.status_code == 409: - print("[Warning] : {0} Repository Already Exists\n\r".format(repo.full_name)) - else: - print(r.status_code, r.text, jsonstring,"\n\r") \ No newline at end of file + giteaCreateRepo(m,repo.private) + topics = repo.get_topics() + giteaSetRepoTopics(repo_owner,repo_name,topics) + print(" ") \ No newline at end of file