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 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,9 +165,8 @@ 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)
@ -205,29 +208,35 @@ def main():
if args.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='
headers = {}
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)
failed_commits = get_failed_commits(repositories=repositories, headers=headers, args=args)
print(json.dumps(failed_commits))
commits = get_failed_gitea_commits(repositories=repositories, headers=headers, args=args)
failed_commits.extend(commits)
elif args.hoster == 'github':
elif h == 'github':
url = 'https://api.github.com/'
headers = {}
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:
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(
pulls=pulls,
pull=pull,
url=url,
org=org,
repo=repo,