#!/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 main(): 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( '--url', default='https://gitea.eco.tsi-dev.otc-service.com/api/v1/', help='Base URL for API request.' ) args = parser.parse_args() # logging.basicConfig(level=logging.DEBUG) path = 'orgs/docs/repos?limit=50&page=' headers = {} headers['accept'] = 'application/json' headers['Authorization'] = 'token ' + args.token repositories = [] i = 1 while True: try: url = args.url + path + str(i) res = requests.request('GET', url=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 pull_path = 'repos/docs/' status_path = 'repos/docs/' pulls = [] failed_commits = [] 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 print(json.dumps(failed_commits)) if __name__ == '__main__': main()