diff --git a/tools/attention_list.py b/tools/attention_list.py index 8152e80a..027295b1 100644 --- a/tools/attention_list.py +++ b/tools/attention_list.py @@ -22,6 +22,8 @@ import logging import requests import json import re +import os +import yaml class FailedPR: @@ -59,7 +61,7 @@ def get_args(): parser = argparse.ArgumentParser( description='Bootstrap repositories.') parser.add_argument( - '--gh-orgs', + '--github-orgs', nargs='+', default=['opentelekomcloud-docs'], help='One or more GitHub organizations to be queried for failed Pull ' @@ -75,7 +77,7 @@ def get_args(): 'Default: [docs]' ) parser.add_argument( - '--gh-token', + '--github-token', help='API Token for GitHub.' ) parser.add_argument( @@ -94,7 +96,7 @@ def get_args(): 'Default: https://gitea.eco.tsi-dev.otc-service.com/api/v1/' ) parser.add_argument( - '--gh-url', + '--github-url', default='https://api.github.com/', help='GitHub Base URL for API request.\n' 'Default: https://api.github.com/' @@ -106,8 +108,14 @@ def get_args(): help='Git hoster to be queried for failed Pull Requests.\n' 'Default: [github, gitea].' ) + parser.add_argument( + '--yaml', + action='store_true', + help='Set yaml output format instead of json.' + ) return parser.parse_args() + def add_builds_to_obj(obj, url, tenant): """ This method trys to find all build jobs under a Zuul buildset. @@ -132,6 +140,7 @@ def add_builds_to_obj(obj, url, tenant): obj.jobs = jobs return obj + def get_gitea_repos(url, headers, gitea_org): """ Get all Repositories of one Gitea orgainzation @@ -157,10 +166,11 @@ def get_gitea_repos(url, headers, gitea_org): except Exception as e: print("An error has occured: " + str(e)) print("The request status is: " + str(res.status_code) + - " | " + str(res.reason)) + " | " + str(res.reason)) break return repositories + def get_gitea_prs(url, headers, gitea_org, repo): """ Collect all Pull Requests of a Gitea Repository @@ -169,7 +179,7 @@ def get_gitea_prs(url, headers, gitea_org, repo): try: req_url = (url + 'repos/' + gitea_org + '/' - + repo + '/pulls?state=open') + + repo + '/pulls?state=open') res = requests.request('GET', url=req_url, headers=headers) if res.json(): for pr in res.json(): @@ -177,18 +187,19 @@ def get_gitea_prs(url, headers, gitea_org, repo): except Exception as e: print("An error has occured: " + str(e)) print("The request status is: " + str(res.status_code) + - " | " + str(res.reason)) + " | " + str(res.reason)) exit() return pullrequests -def get_failed_gitea_commits(pull, url, gitea_org, repo, headers): + +def get_gitea_failed_commits(pull, url, gitea_org, repo, headers): """ Collect all failed gitea commits of one repository. """ failed_commits = [] try: req_url = (url + 'repos/' + gitea_org + '/' + repo['name'] + - '/commits/' + pull['head']['ref'] + '/statuses?limit=1') + '/commits/' + pull['head']['ref'] + '/statuses?limit=1') res_sta = requests.request('GET', url=req_url, headers=headers) if res_sta.json(): if res_sta.json()[0]['status'] == 'failure': @@ -211,10 +222,11 @@ def get_failed_gitea_commits(pull, url, gitea_org, repo, headers): 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)) + " | " + str(res_sta.reason)) return failed_commits -def get_github_repos(url, headers, gh_org): + +def get_github_repos(url, headers, github_org): """ Get all repositories of one GitHub organization """ @@ -223,24 +235,25 @@ def get_github_repos(url, headers, gh_org): while True: try: - req_url = url + 'orgs/' + gh_org + '/repos?page=' + str(i) + req_url = url + 'orgs/' + github_org + '/repos?page=' + str(i) res = requests.request('GET', url=req_url, headers=headers) if res.json(): for repo in res.json(): - if repo['archived'] == False: + if repo['archived'] is False: repositories.append(repo) - i+=1 + 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)) + " | " + str(res.reason)) break return repositories -def get_github_prs(url, headers, gh_org, repo): + +def get_github_prs(url, headers, github_org, repo): """ Get all Pull Requests of one GitHub repository """ @@ -249,32 +262,32 @@ def get_github_prs(url, headers, gh_org, repo): while True: try: - req_url = (url + 'repos/' + gh_org + '/' + repo + - '/pulls?state=open&page=' + str(i)) + req_url = (url + 'repos/' + github_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 + 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)) + " | " + str(res.reason)) break return pullrequests -def get_failed_gh_commits(pull, url, gh_org, repo, headers): +def get_github_failed_commits(pull, url, github_org, repo, headers): """ Collect all Failed Pull Requests of one GitHub repository """ failed_commits = [] try: - req_url = (url + 'repos/' + gh_org + '/' + repo['name'] + - '/commits/' + pull['head']['sha'] + '/check-runs') + req_url = (url + 'repos/' + github_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: @@ -283,7 +296,7 @@ def get_failed_gh_commits(pull, url, gh_org, repo, headers): o = FailedPR( host='github', url=pull['html_url'], - org=gh_org, + org=github_org, repo=repo['name'], pullrequest=pull['title'], status=res_sta.json()['check_runs'][0]['conclusion'], @@ -299,7 +312,7 @@ def get_failed_gh_commits(pull, url, gh_org, repo, headers): o = FailedPR( host='github', url=pull['html_url'], - org=gh_org, + org=github_org, repo=repo['name'], pullrequest=pull['title'], created_at=pull['created_at'], @@ -311,11 +324,11 @@ def get_failed_gh_commits(pull, url, gh_org, repo, headers): 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)) + " | " + str(res_sta.reason)) return failed_commits -def create_json_result(failed_commits): +def create_result(failed_commits): """ Create Result """ @@ -331,7 +344,32 @@ def create_json_result(failed_commits): else: result['meta']['count'] = 0 - return json.dumps(result) + return result + + +def get_token(hoster, args): + token = '' + if hoster == 'github': + if args.github_token: + token = args.github_token + elif os.getenv('GITHUB_TOKEN'): + token = os.getenv('GITHUB_TOKEN') + else: + raise Exception( + 'Please, provide GitHub Token as console argument or \n' + 'as environment variable GITHUB_TOKEN') + elif hoster == 'gitea': + if args.gitea_token: + token = args.gitea_token + elif os.getenv('GITEA_TOKEN'): + token = os.getenv('GITEA_TOKEN') + else: + raise Exception( + 'Please, provide GitHub Token as console argument or \n' + 'as environment variable GITEA_TOKEN') + else: + raise ValueError('No supported Git hoster provided.') + return token def main(): @@ -346,11 +384,13 @@ def main(): headers = {} headers['accept'] = 'application/json' - if not args.gitea_token: - raise Exception('Please, provide Gitea Token as argument.') + headers['Authorization'] = 'token ' + get_token( + hoster=h, + args=args + ) + if not args.gitea_url: raise Exception('Please, provide Gitea URL as argument.') - headers['Authorization'] = 'token ' + args.gitea_token for org in args.gitea_orgs: repos = get_gitea_repos( @@ -367,7 +407,7 @@ def main(): ) if pulls: for pull in pulls: - commits = get_failed_gitea_commits( + commits = get_gitea_failed_commits( pull=pull, url=args.gitea_url, gitea_org=org, @@ -377,41 +417,49 @@ def main(): failed_commits.extend(commits) elif h == 'github': - if not args.gh_url: - raise ValueError('Parameter --gh-url not found.') - url = args.gh_url + if not args.github_url: + raise ValueError('Parameter --github-url not found.') + url = args.github_url headers = {} headers['accept'] = 'application/json' - if not args.gh_token: - raise Exception('Please, provide GitHub Token as argument.') - headers['Authorization'] = 'Bearer ' + args.gh_token + headers['Authorization'] = 'Bearer ' + get_token( + hoster=h, + args=args + ) - for org in args.gh_orgs: + for org in args.github_orgs: repos = get_github_repos( url=url, headers=headers, - gh_org=org + github_org=org ) for repo in repos: pulls = get_github_prs( url=url, headers=headers, - gh_org=org, + github_org=org, repo=repo['name'] ) if pulls: for pull in pulls: - commits = get_failed_gh_commits( + commits = get_github_failed_commits( pull=pull, url=url, - gh_org=org, + github_org=org, repo=repo, headers=headers ) failed_commits.extend(commits) + else: + raise ValueError("No supported hoster found.") - print(create_json_result( - failed_commits=failed_commits)) + result = create_result(failed_commits=failed_commits) + if args.yaml: + result = yaml.dump(result) + else: + result = json.dumps(result) + + print(result) if __name__ == '__main__':