205 lines
6.6 KiB
Python
205 lines
6.6 KiB
Python
#!/usr/bin/python
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import argparse
|
|
import logging
|
|
import requests
|
|
import json
|
|
|
|
|
|
def get_args():
|
|
parser = argparse.ArgumentParser(
|
|
description='Bootstrap repositories.')
|
|
parser.add_argument(
|
|
'--orgs',
|
|
nargs='+',
|
|
default=['docs'],
|
|
help='One or more organizations to be queried for failed Pull '
|
|
'Requests.\n'
|
|
'Default: [docs]'
|
|
)
|
|
parser.add_argument(
|
|
'--token',
|
|
required=True,
|
|
help='API Token'
|
|
)
|
|
parser.add_argument(
|
|
'--debug',
|
|
action='store_true',
|
|
help='Set debug mode on.'
|
|
)
|
|
parser.add_argument(
|
|
'--url',
|
|
default='https://gitea.eco.tsi-dev.otc-service.com/api/v1/',
|
|
help='Base URL for API request.'
|
|
)
|
|
parser.add_argument(
|
|
'--hoster',
|
|
default='gitea',
|
|
choices=['gitea', 'github'],
|
|
nargs='?',
|
|
help='Hoster for the API calls. Choose between \"github\" and \"gitea\".'
|
|
)
|
|
return(parser.parse_args())
|
|
|
|
def get_repos(url, path, headers):
|
|
repositories = []
|
|
i = 1
|
|
|
|
while True:
|
|
try:
|
|
req_url = url + path + str(i)
|
|
res = requests.request('GET', url=req_url, headers=headers)
|
|
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))
|
|
break
|
|
return(repositories)
|
|
|
|
def get_failed_commits(repositories, headers, args):
|
|
failed_commits = []
|
|
pull_path = 'repos/docs/'
|
|
status_path = pull_path
|
|
pulls = []
|
|
|
|
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':
|
|
# print("Pull Request " + pull['url'] + " has a failed check!")
|
|
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
|
|
return(failed_commits)
|
|
|
|
def get_github_repos(url, headers, org):
|
|
repositories = []
|
|
i = 1
|
|
|
|
while True:
|
|
try:
|
|
req_url = url + 'orgs/' + org + '/repos?page=' + str(i)
|
|
res = requests.request('GET', url=req_url, headers=headers)
|
|
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))
|
|
break
|
|
return(repositories)
|
|
|
|
def get_github_prs(url, headers, org, repo):
|
|
pullrequests = []
|
|
i = 1
|
|
|
|
while True:
|
|
try:
|
|
req_url = url + 'repos/' + 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
|
|
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))
|
|
break
|
|
return(pullrequests)
|
|
|
|
|
|
def main():
|
|
|
|
args = get_args()
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
for org in args.orgs:
|
|
repos = get_github_repos(url, headers, org)
|
|
pulls = []
|
|
for repo in repos:
|
|
pulls.extend(get_github_prs(url, headers, org, repo['name']))
|
|
# print(pulls)
|
|
print(len(pulls))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main() |