some changes

This commit is contained in:
2022-09-26 14:07:55 +00:00
parent 5466630a41
commit 78184c5217

View File

@ -17,6 +17,7 @@ import argparse
import logging import logging
import requests import requests
import json import json
import threading
def get_args(): def get_args():
@ -31,9 +32,12 @@ def get_args():
'Default: [docs]' 'Default: [docs]'
) )
parser.add_argument( parser.add_argument(
'--token', '--gh-token',
required=True, help='API Token for GitHub.'
help='API Token' )
parser.add_argument(
'--gitea-token',
help='API Token for Gitea'
) )
parser.add_argument( parser.add_argument(
'--debug', '--debug',
@ -47,10 +51,10 @@ def get_args():
) )
parser.add_argument( parser.add_argument(
'--hoster', '--hoster',
default='gitea', default=['gitea', 'github'],
choices=['gitea', 'github'], nargs='+',
nargs='?', help='Git hoster to be queried for failed Pull Requests.\n
help='Hoster for the API calls. Choose between \"github\" and \"gitea\".' 'Default: [github, gitea].'
) )
return(parser.parse_args()) return(parser.parse_args())
@ -75,7 +79,7 @@ def get_repos(url, path, headers):
break break
return(repositories) return(repositories)
def get_failed_commits(repositories, headers, args): def get_failed_gitea_commits(repositories, headers, args):
failed_commits = [] failed_commits = []
pull_path = 'repos/docs/' pull_path = 'repos/docs/'
status_path = pull_path status_path = pull_path
@ -161,9 +165,8 @@ def get_github_prs(url, headers, org, repo):
break break
return(pullrequests) return(pullrequests)
def get_failed_gh_commits(pulls, url, org, repo, headers): def get_failed_gh_commits(pull, url, org, repo, headers):
failed_commits = [] failed_commits = []
for pull in pulls:
try: try:
req_url = url + 'repos/' + org + '/' + repo['name'] + '/commits/' + pull['head']['sha'] + '/check-runs' req_url = url + 'repos/' + org + '/' + repo['name'] + '/commits/' + pull['head']['sha'] + '/check-runs'
res_sta = requests.request('GET', url=req_url, headers=headers) res_sta = requests.request('GET', url=req_url, headers=headers)
@ -205,29 +208,35 @@ def main():
if args.debug: if args.debug:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
if args.hoster == 'gitea': for h in args.hoster:
if h == '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'
headers['Authorization'] = 'token ' + args.token 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) repositories = get_repos(url=args.url, path=repo_path, headers=headers)
failed_commits = get_failed_commits(repositories=repositories, headers=headers, args=args) commits = get_failed_gitea_commits(repositories=repositories, headers=headers, args=args)
print(json.dumps(failed_commits)) failed_commits.extend(commits)
elif args.hoster == 'github': elif h == 'github':
url = 'https://api.github.com/' url = 'https://api.github.com/'
headers = {} headers = {}
headers['accept'] = 'application/json' headers['accept'] = 'application/json'
headers['Authorization'] = 'Bearer ' + args.token if not args.gh_token:
raise Exception('Please, provide GitHub Token as argument.')
headers['Authorization'] = 'Bearer ' + args.gh_token
for org in args.orgs: for org in args.orgs:
repos = get_github_repos(url, headers, org) repos = get_github_repos(url, headers, org)
for repo in repos: for repo in repos:
pulls = get_github_prs(url, headers, org, repo['name']) pulls = get_github_prs(url, headers, org, repo['name'])
if pulls: if pulls:
for pull in pulls:
commits = get_failed_gh_commits( commits = get_failed_gh_commits(
pulls=pulls, pull=pull,
url=url, url=url,
org=org, org=org,
repo=repo, repo=repo,