diff --git a/repoCache.py b/repoCache.py new file mode 100644 index 0000000..f47b702 --- /dev/null +++ b/repoCache.py @@ -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) + diff --git a/src/helper.py b/src/helper.py index c4001c0..46acb41 100644 --- a/src/helper.py +++ b/src/helper.py @@ -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] \ No newline at end of file + 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 diff --git a/src/localCacheHelper.py b/src/localCacheHelper.py new file mode 100644 index 0000000..8985f02 --- /dev/null +++ b/src/localCacheHelper.py @@ -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) \ No newline at end of file