diff --git a/tools/attention_list.py b/tools/attention_list.py index 3d559f9a..3475b50a 100644 --- a/tools/attention_list.py +++ b/tools/attention_list.py @@ -17,6 +17,7 @@ import argparse import logging import requests import json +import threading def get_args(): @@ -31,9 +32,12 @@ def get_args(): 'Default: [docs]' ) parser.add_argument( - '--token', - required=True, - help='API Token' + '--gh-token', + help='API Token for GitHub.' + ) + parser.add_argument( + '--gitea-token', + help='API Token for Gitea' ) parser.add_argument( '--debug', @@ -47,10 +51,10 @@ def get_args(): ) parser.add_argument( '--hoster', - default='gitea', - choices=['gitea', 'github'], - nargs='?', - help='Hoster for the API calls. Choose between \"github\" and \"gitea\".' + default=['gitea', 'github'], + nargs='+', + help='Git hoster to be queried for failed Pull Requests.\n + 'Default: [github, gitea].' ) return(parser.parse_args()) @@ -75,7 +79,7 @@ def get_repos(url, path, headers): break return(repositories) -def get_failed_commits(repositories, headers, args): +def get_failed_gitea_commits(repositories, headers, args): failed_commits = [] pull_path = 'repos/docs/' status_path = pull_path @@ -161,38 +165,37 @@ def get_github_prs(url, headers, org, repo): break return(pullrequests) -def get_failed_gh_commits(pulls, url, org, repo, headers): +def get_failed_gh_commits(pull, url, org, repo, headers): failed_commits = [] - for pull in pulls: - try: - req_url = url + 'repos/' + org + '/' + repo['name'] + '/commits/' + pull['head']['sha'] + '/check-runs' - res_sta = requests.request('GET', url=req_url, headers=headers) - if res_sta.json(): - if len(res_sta.json()['check_runs']) != 0: - if res_sta.json()['check_runs'][0]['conclusion'] == 'failure': - failed_commits.append({ - 'url': pull['html_url'], - 'status': res_sta.json()['check_runs'][0]['conclusion'], - 'target_url': res_sta.json()['check_runs'][0]['details_url'], - 'created_at': pull['created_at'], - 'updated_at': res_sta.json()['check_runs'][0]['completed_at'] - }) - continue - else: + try: + req_url = url + 'repos/' + org + '/' + repo['name'] + '/commits/' + pull['head']['sha'] + '/check-runs' + res_sta = requests.request('GET', url=req_url, headers=headers) + if res_sta.json(): + if len(res_sta.json()['check_runs']) != 0: + if res_sta.json()['check_runs'][0]['conclusion'] == 'failure': failed_commits.append({ 'url': pull['html_url'], - 'status': 'undefined', - 'target_url': 'undefined', + 'status': res_sta.json()['check_runs'][0]['conclusion'], + 'target_url': res_sta.json()['check_runs'][0]['details_url'], 'created_at': pull['created_at'], - 'updated_at': pull['updated_at'] + 'updated_at': res_sta.json()['check_runs'][0]['completed_at'] }) + continue + else: + failed_commits.append({ + 'url': pull['html_url'], + 'status': 'undefined', + 'target_url': 'undefined', + 'created_at': pull['created_at'], + 'updated_at': pull['updated_at'] + }) - except Exception as e: - print("An error has occured: " + str(e)) - print("The request status is: " + str(res_sta.status_code) + " | " + str(res_sta.reason)) - print(json.dumps(res_sta.json())) - exit() - break + except Exception as e: + print("An error has occured: " + str(e)) + print("The request status is: " + str(res_sta.status_code) + " | " + str(res_sta.reason)) + print(json.dumps(res_sta.json())) + exit() + break return(failed_commits) @@ -205,34 +208,40 @@ def main(): if args.debug: logging.basicConfig(level=logging.DEBUG) - if args.hoster == 'gitea': - repo_path = 'orgs/docs/repos?limit=50&page=' - headers = {} - headers['accept'] = 'application/json' - headers['Authorization'] = 'token ' + args.token + for h in args.hoster: + if h == 'gitea': + repo_path = 'orgs/docs/repos?limit=50&page=' + headers = {} + headers['accept'] = 'application/json' + if not args.gitea_token: + raise Exception('Please, provide Gitea Token as argument.') + headers['Authorization'] = 'token ' + args.gitea_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 + repositories = get_repos(url=args.url, path=repo_path, headers=headers) + commits = get_failed_gitea_commits(repositories=repositories, headers=headers, args=args) + failed_commits.extend(commits) + + elif h == 'github': + url = 'https://api.github.com/' + headers = {} + headers['accept'] = 'application/json' + if not args.gh_token: + raise Exception('Please, provide GitHub Token as argument.') + headers['Authorization'] = 'Bearer ' + args.gh_token - for org in args.orgs: - repos = get_github_repos(url, headers, org) - for repo in repos: - pulls = get_github_prs(url, headers, org, repo['name']) - if pulls: - commits = get_failed_gh_commits( - pulls=pulls, - url=url, - org=org, - repo=repo, - headers=headers) - failed_commits.extend(commits) + for org in args.orgs: + repos = get_github_repos(url, headers, org) + for repo in repos: + pulls = get_github_prs(url, headers, org, repo['name']) + if pulls: + for pull in pulls: + commits = get_failed_gh_commits( + pull=pull, + url=url, + org=org, + repo=repo, + headers=headers) + failed_commits.extend(commits) print(json.dumps(failed_commits))