diff --git a/mirror-forked-repository.py b/mirror-forked-repository.py index 906ef44..db8d5e8 100644 --- a/mirror-forked-repository.py +++ b/mirror-forked-repository.py @@ -1,8 +1,5 @@ #!/usr/bin/env python # https://github.com/PyGithub/PyGithub -import json -import sys -import os from helper import getConfig,giteaSetRepoTopics,giteaSession,giteaCreateRepo,ghApi,giteaCreateOrg,giteaGetUser,config config = getConfig() diff --git a/mirror-gists.py b/mirror-gists.py index 244bd35..08928d8 100644 --- a/mirror-gists.py +++ b/mirror-gists.py @@ -1,10 +1,5 @@ #!/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 = getConfig() session = giteaSession() @@ -62,4 +57,5 @@ for repo in gh.get_user().get_gists(): topics.append('public-{0}-gist'.format(repo_owner)) giteaSetRepoTopics(repo_owner,m["repo_name"],topics) - print(" ") \ No newline at end of file + print(" ") + exit(1) \ No newline at end of file diff --git a/mirror-repository.py b/mirror-repository.py index 13a5f37..efb40a4 100644 --- a/mirror-repository.py +++ b/mirror-repository.py @@ -1,53 +1,32 @@ #!/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 not repo.fork: real_repo = repo.full_name.split('/')[1] + gitea_dest_user = repo.owner.login + repo_owner=repo.owner.login + + print('Source 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 +34,8 @@ 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(" ") + exit(1) \ No newline at end of file