Attention List #4

Closed
sgode wants to merge 16 commits from (deleted):attention_list into main
Showing only changes of commit b71f45c846 - Show all commits

View File

@ -45,6 +45,13 @@ def get_args():
default='https://gitea.eco.tsi-dev.otc-service.com/api/v1/', default='https://gitea.eco.tsi-dev.otc-service.com/api/v1/',
help='Base URL for API request.' help='Base URL for API request.'
) )
parser.add_argument(
'--hoster',
default='gitea',
choices=['gitea', 'github'],
nargs='?',
help='Hoster for the API calls. Choose between \"github\" and \"gitea\".'
)
return(parser.parse_args()) return(parser.parse_args())
def get_repos(url, path, headers): def get_repos(url, path, headers):
@ -112,6 +119,48 @@ def get_failed_commits(repositories, headers, args):
break break
return(failed_commits) return(failed_commits)
Outdated
Review

you can either use existing SDK or at least make a wrapper for requests so that you do not repeat the same exception handling block over and over again (smth like in https://review.opendev.org/c/zuul/zuul/+/850008/10/zuul/driver/gitea/giteaconnection.py#364 or in https://github.com/opentelekomcloud/ansible-collection-gitcontrol/tree/main/plugins/module_utils)

you can either use existing SDK or at least make a wrapper for requests so that you do not repeat the same exception handling block over and over again (smth like in https://review.opendev.org/c/zuul/zuul/+/850008/10/zuul/driver/gitea/giteaconnection.py#364 or in https://github.com/opentelekomcloud/ansible-collection-gitcontrol/tree/main/plugins/module_utils)
def get_github_repos(url, headers, org):
repositories = []
i = 1
while True:
try:
req_url = url + 'orgs/' + org + '/repos?page=' + str(i)
res = requests.request('GET', url=req_url, headers=headers)
if res.json():
for repo in res.json():
repositories.append(repo)
i+=1
continue
else:
break
except Exception as e:
print("An error has occured: " + str(e))
print("The request status is: " + str(res.status_code) + " | " + str(res.reason))
break
return(repositories)
def get_github_prs(url, headers, org, repo):
pullrequests = []
i = 1
while True:
try:
req_url = url + 'repos/' + org + '/' + repo + '/pulls?state=open&page=' + str(i)
res = requests.request('GET', url=req_url, headers=headers)
if res.json():
for pr in res.json():
pullrequests.append(pr)
i+=1
continue
else:
break
except Exception as e:
print("An error has occured: " + str(e))
print("The request status is: " + str(res.status_code) + " | " + str(res.reason))
Outdated
Review

pls decide on function naming convention, get_gitea_failed_commits fits more other functions

pls decide on function naming convention, get_gitea_failed_commits fits more other functions
Outdated
Review

done

done
break
return(pullrequests)
def main(): def main():
@ -120,6 +169,7 @@ def main():
if args.debug: if args.debug:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
if args.hoster == 'gitea':
repo_path = 'orgs/docs/repos?limit=50&page=' repo_path = 'orgs/docs/repos?limit=50&page='
headers = {} headers = {}
headers['accept'] = 'application/json' headers['accept'] = 'application/json'
@ -129,5 +179,27 @@ def main():
failed_commits = get_failed_commits(repositories=repositories, headers=headers, args=args) failed_commits = get_failed_commits(repositories=repositories, headers=headers, args=args)
print(json.dumps(failed_commits)) print(json.dumps(failed_commits))
elif args.hoster == 'github':
url = 'https://api.github.com/'
headers = {}
headers['accept'] = 'application/json'
headers['Authorization'] = 'Bearer ' + args.token
for org in args.orgs:
repos = get_github_repos(url, headers, org)
pulls = []
for repo in repos:
pulls.extend(get_github_prs(url, headers, org, repo['name']))
# print(pulls)
print(len(pulls))
if __name__ == '__main__': if __name__ == '__main__':
main() main()