Added Option to fetch all gitea repository and save it in local as JSON

This commit is contained in:
Varun 2020-10-26 07:34:49 +05:30
parent cc6a8a54a0
commit db4b1c80da
No known key found for this signature in database
GPG Key ID: 93FB46DCF16E0D6F
3 changed files with 154 additions and 1 deletions

56
repoCache.py Normal file
View File

@ -0,0 +1,56 @@
import sys
import os
import json
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(1, "{0}/src/".format(THIS_FOLDER))
from helper import giteaGetAllUsersOrgs,logError,log,giteaHost,giteaSession,giteaGetUserRepos
from localCacheHelper import writeLocalCache
giteaExistsRepos = dict()
session = giteaSession()
##########################################
## Fetches All Users & Its Repository ####
##########################################
log('Fetching All Users & Repositories')
allusers = giteaGetAllUsersOrgs('users')
log(' {0} Users Found '.format(len(allusers)))
for user in allusers:
username = user['username']
repos = giteaGetUserRepos(user['id'])
if repos != 'failed':
log(' {0} Repository Found For {1}'.format(len(repos),username))
for repo in repos:
giteaExistsRepos[ repo['full_name']] = True
else:
logError("Unable To Get [{0}] Repository !".format(username))
##########################################
## Fetches All Orgs & Its Repository ####
##########################################
log('')
log('')
log('')
log('')
log('Fetching All Organizations & Repositories')
allorgs = giteaGetAllUsersOrgs('orgs')
log(' {0} Organizations Found '.format(len(allorgs)))
for org in allorgs:
username = org['username']
repos = giteaGetUserRepos(org['id'])
if repos != 'failed':
log(' {0} Repository Found For {1}'.format(len(repos),username))
for repo in repos:
giteaExistsRepos[ repo['full_name']] = True
else:
logError("Unable To Get [{0}] Repository !".format(username))
writeLocalCache(giteaExistsRepos)

View File

@ -11,6 +11,13 @@ giteaGetUserCache = dict()
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 logError(val):
log('')
log('################# ERROR ####################')
log(val)
log('################# ERROR ####################')
log('')
def log(val):
if val == False:
print(" ")
@ -127,4 +134,63 @@ def giteaGetUser(username):
if r.status_code != 200:
return 'failed'
giteaGetUserCache["{0}".format(username)] = json.loads(r.text)["id"]
return giteaGetUserCache[username]
return giteaGetUserCache[username]
def giteaGetUserRepos(user_uid):
loopCount = 1
results = dict()
while loopCount != 0 :
r = session.get(giteaHost('repos/search?uid={0}&page={1}&limit=50'.format(user_uid,loopCount)))
if r.status_code != 200:
loopCount = 0
break
data = json.loads(r.text)
if data['ok'] == True:
if len(data['data']) == 0:
loopCount = 0
break
else:
if len(results) == 0:
results = data['data']
else:
results = results + data['data']
loopCount += 1
return results
def giteaGetAllUsersOrgs(type):
loopCount = 1
results = dict()
if type == 'users':
type = 'admin/users'
else:
type = 'orgs'
while loopCount != 0 :
r = session.get(giteaHost('{0}?page={1}&limit=50'.format(type,loopCount)))
if r.status_code != 200:
loopCount = 0
break
data = json.loads(r.text)
if len(data) == 0:
loopCount = 0
break
else:
if len(results) == 0:
results = data
else:
results = results + data
loopCount += 1
return results

31
src/localCacheHelper.py Normal file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env python
giteaExistsRepos = dict()
from helper import logError,log,getConfig
import json
config = getConfig()
def writeLocalCache(content):
try:
with open(config['local_cache']['file_path'], 'w') as file:
file.write(json.dumps(content))
except:
logError('Unable To Save Local Cache !')
def readLocalCache():
try:
with open(config['local_cache']['file_path'],'r') as file:
filedata = file.read()
return json.loads(filedata)
except:
logError('Local Cache File Not Found ! / Unable To Ready It')
return dict()
giteaExistsRepos = readLocalCache()
useLocalCache = config['local_cache']['enabled']
def saveLocalCache():
writeLocalCache(giteaExistsRepos)