Improved.

This commit is contained in:
Varun 2020-10-12 17:07:51 +05:30
parent 3046f12870
commit ca18d9d61a
No known key found for this signature in database
GPG Key ID: 93FB46DCF16E0D6F
2 changed files with 25 additions and 49 deletions

View File

@ -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)

View File

@ -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")
giteaCreateRepo(m,repo.private)
topics = repo.get_topics()
giteaSetRepoTopics(repo_owner,repo_name,topics)
print(" ")