From b71f45c846d7fa89cd4cf24419396e75a83870f4 Mon Sep 17 00:00:00 2001 From: Sebastian Gode Date: Wed, 21 Sep 2022 09:13:54 +0000 Subject: [PATCH] GitHub first try --- tools/attention_list.py | 86 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 7 deletions(-) diff --git a/tools/attention_list.py b/tools/attention_list.py index a5344922..353aaa38 100644 --- a/tools/attention_list.py +++ b/tools/attention_list.py @@ -45,6 +45,13 @@ def get_args(): default='https://gitea.eco.tsi-dev.otc-service.com/api/v1/', 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()) def get_repos(url, path, headers): @@ -112,6 +119,48 @@ def get_failed_commits(repositories, headers, args): break return(failed_commits) +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)) + break + return(pullrequests) + def main(): @@ -120,14 +169,37 @@ def main(): if args.debug: logging.basicConfig(level=logging.DEBUG) - repo_path = 'orgs/docs/repos?limit=50&page=' - headers = {} - headers['accept'] = 'application/json' - headers['Authorization'] = 'token ' + args.token + if args.hoster == 'gitea': + repo_path = 'orgs/docs/repos?limit=50&page=' + headers = {} + headers['accept'] = 'application/json' + headers['Authorization'] = 'token ' + args.token + + repositories = get_repos(url=args.url, path=repo_path, headers=headers) + failed_commits = get_failed_commits(repositories=repositories, headers=headers, args=args) + 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)) + + + + + + - repositories = get_repos(url=args.url, path=repo_path, headers=headers) - failed_commits = get_failed_commits(repositories=repositories, headers=headers, args=args) - print(json.dumps(failed_commits)) if __name__ == '__main__': main() \ No newline at end of file