diff --git a/tools/attention_list.py b/tools/attention_list.py index 2698c212..40a06eb3 100644 --- a/tools/attention_list.py +++ b/tools/attention_list.py @@ -20,7 +20,8 @@ import json import threading class FailedPR: - def __init__(self, url, status): + def __init__(self, host, url, status, target_url, created_at, updated_at): + self.host = host self.url = url self.status = status self.target_url = target_url @@ -77,68 +78,70 @@ def get_args(): ) return(parser.parse_args()) -def get_repos(url, path, headers): +def get_gitea_repos(url, headers, gitea_org): repositories = [] i = 1 while True: try: - req_url = url + path + str(i) + req_url = (url + 'orgs/' + gitea_org + + '/repos?limit=50&page=' + str(i)) res = requests.request('GET', url=req_url, headers=headers) + i+=1 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)) + print("The request status is: " + str(res.status_code) + + " | " + str(res.reason)) break return(repositories) -def get_failed_gitea_commits(repositories, headers, args): +def get_gitea_prs(url, headers, gitea_org, repo): + pullrequests = [] + + try: + req_url = (url + 'repos/' + gitea_org + '/' + + repo + '/pulls?state=open') + res = requests.request('GET', url=req_url, headers=headers) + if res.json(): + for pr in res.json(): + pullrequests.append(pr) + except Exception as e: + print("An error has occured: " + str(e)) + print("The request status is: " + str(res.status_code) + + " | " + str(res.reason)) + exit() + return(pullrequests) + +def get_failed_gitea_commits(pull, url, gitea_org, repo, headers): failed_commits = [] - pull_path = 'repos/docs/' - status_path = pull_path - pulls = [] + try: + req_url = (url + 'repos/' + gitea_org + '/' + repo['name'] + + '/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': + o = FailedPR( + host='gitea', + url=pull['url'], + status=res_sta.json()[0]['status'], + target_url=res_sta.json()[0]['target_url'], + created_at=pull['created_at'], + updated_at=res_sta.json()[0]['updated_at'] + ) + failed_commits.append(o) - for repo in repositories: - try: - url = args.url + pull_path + repo['name'] + '/pulls?state=open' - res = requests.request('GET', url=url, headers=headers) - if res.json(): - for pull in res.json(): - pulls.append(pull) - - for pull in pulls: - try: - url = args.url + status_path + repo['name'] + '/commits/' + pull['head']['ref'] + '/statuses?limit=1' - res_sta = requests.request('GET', url=url, headers=headers) - if res_sta.json(): - if res_sta.json()[0]['status'] == 'failure': - failed_commits.append({ - 'url': pull['url'], - 'status': res_sta.json()[0]['status'], - 'target_url': res_sta.json()[0]['target_url'], - 'created_at': pull['created_at'], - 'updated_at': res_sta.json()[0]['updated_at'] - }) - continue - else: - continue - 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)) - break - continue - else: - continue - except Exception as e: - print("An error has occured: " + str(e)) - print("The request status is: " + str(res.status_code) + " | " + str(res.reason)) - 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() return(failed_commits) def get_github_repos(url, headers, gh_org): @@ -158,7 +161,8 @@ def get_github_repos(url, headers, gh_org): break except Exception as e: print("An error has occured: " + str(e)) - print("The request status is: " + str(res.status_code) + " | " + str(res.reason)) + print("The request status is: " + str(res.status_code) + + " | " + str(res.reason)) break return(repositories) @@ -168,7 +172,8 @@ 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/' + gh_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(): @@ -179,28 +184,34 @@ def get_github_prs(url, headers, gh_org, repo): break except Exception as e: print("An error has occured: " + str(e)) - print("The request status is: " + str(res.status_code) + " | " + str(res.reason)) + print("The request status is: " + str(res.status_code) + + " | " + str(res.reason)) break return(pullrequests) def get_failed_gh_commits(pull, url, gh_org, repo, headers): failed_commits = [] try: - req_url = url + 'repos/' + gh_org + '/' + repo['name'] + '/commits/' + pull['head']['sha'] + '/check-runs' + req_url = (url + 'repos/' + gh_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': o = FailedPR( + host='gitea', url=pull['html_url'], status=res_sta.json()['check_runs'][0]['conclusion'], - target_url=res_sta.json()['check_runs'][0]['details_url'], + 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'] + updated_at=(res_sta.json()['check_runs'] + [0]['completed_at']) ) failed_commits.append(o) else: o = FailedPR( + host='gitea', url=pull['html_url'], status='', target_url='', @@ -211,7 +222,8 @@ 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)) + print("The request status is: " + str(res_sta.status_code) + + " | " + str(res_sta.reason)) print(json.dumps(res_sta.json())) exit() return(failed_commits) @@ -228,16 +240,38 @@ def main(): 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.') + if not args.gitea_url: + raise Exception('Please, provide Gitea URL as argument.') headers['Authorization'] = 'token ' + args.gitea_token - repositories = get_repos(url=args.gitea_url, path=repo_path, headers=headers) - commits = get_failed_gitea_commits(repositories=repositories, headers=headers, args=args) - failed_commits.extend(commits) + for org in args.gitea_orgs: + repos = get_gitea_repos( + url=args.gitea_url, + headers=headers, + gitea_org=org + ) + for repo in repos: + pulls = get_gitea_prs( + url=args.gitea_url, + headers=headers, + gitea_org=org, + repo=repo['name'] + ) + if pulls: + for pull in pulls: + commits = get_failed_gitea_commits( + pull=pull, + url=args.gitea_url, + gitea_org=org, + repo=repo, + headers=headers + ) + failed_commits.extend(commits) elif h == 'github': url = 'https://api.github.com/' @@ -248,9 +282,18 @@ def main(): headers['Authorization'] = 'Bearer ' + args.gh_token for org in args.gh_orgs: - repos = get_github_repos(url=url, headers=headers, gh_org=org) + repos = get_github_repos( + url=url, + headers=headers, + gh_org=org + ) for repo in repos: - pulls = get_github_prs(url=url, headers=headers, gh_org=org, repo=repo['name']) + pulls = get_github_prs( + url=url, + headers=headers, + gh_org=org, + repo=repo['name'] + ) if pulls: for pull in pulls: commits = get_failed_gh_commits( @@ -258,11 +301,12 @@ def main(): url=url, gh_org=org, repo=repo, - headers=headers) + headers=headers + ) failed_commits.extend(commits) - - print(json.dumps(failed_commits)) + for o in failed_commits: + print(o.to_json()) if __name__ == '__main__': main() \ No newline at end of file