diff --git a/tools/attention_list.py b/tools/attention_list.py index d82475b8..470019ff 100644 --- a/tools/attention_list.py +++ b/tools/attention_list.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +# Error Codes +# 1000: Failed check (default error) +# 1001: Check not existing but expected + import argparse import logging import requests @@ -20,16 +24,16 @@ import json import threading class FailedPR: - def __init__(self, host, url, status, target_url, created_at, updated_at): + + def __init__(self, host, url, created_at, updated_at, target_url=None, status=None, error=None): self.host = host self.url = url self.status = status self.target_url = target_url self.created_at = created_at self.updated_at = updated_at + self.error = error - def to_json(self): - return json.dumps(self.__dict__) def get_args(): parser = argparse.ArgumentParser( @@ -66,9 +70,15 @@ def get_args(): parser.add_argument( '--gitea-url', default='https://gitea.eco.tsi-dev.otc-service.com/api/v1/', - help='Base URL for API request.\n' + help='Gitea base URL for API request.\n' 'Default: https://gitea.eco.tsi-dev.otc-service.com/api/v1/' ) + parser.add_argument( + '--gh-url', + default='https://api.github.com/', + help='GitHub Base URL for API request.\n' + 'Default: https://api.github.com/' + ) parser.add_argument( '--hoster', default=['gitea', 'github'], @@ -132,7 +142,8 @@ def get_failed_gitea_commits(pull, url, gitea_org, repo, headers): 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'] + updated_at=res_sta.json()[0]['updated_at'], + error=1000 ) failed_commits.append(o) @@ -206,17 +217,17 @@ def get_failed_gh_commits(pull, url, gh_org, repo, headers): [0]['details_url']), created_at=pull['created_at'], updated_at=(res_sta.json()['check_runs'] - [0]['completed_at']) + [0]['completed_at']), + error=1000 ) failed_commits.append(o) else: o = FailedPR( host='github', url=pull['html_url'], - status='', - target_url='', created_at=pull['created_at'], - updated_at=res_sta.json()['check_runs'][0]['completed_at'] + updated_at=pull['updated_at'], + error=1001, ) failed_commits.append(o) @@ -228,6 +239,17 @@ def get_failed_gh_commits(pull, url, gh_org, repo, headers): exit() return failed_commits +def create_json_result(failed_commits): + failed_commits_json = [] + for o in failed_commits: + failed_commits_json.append(vars(o)) + result = {} + result['meta'] = {} + result['meta']['count'] = len(failed_commits_json) + result['data'] = failed_commits_json + return json.dumps(result) + + def main(): args = get_args() failed_commits = [] @@ -271,7 +293,9 @@ def main(): failed_commits.extend(commits) elif h == 'github': - url = 'https://api.github.com/' + if not args.gh_url: + raise ValueError('Parameter --gh-url not found.') + url = args.gh_url headers = {} headers['accept'] = 'application/json' if not args.gh_token: @@ -301,9 +325,10 @@ def main(): headers=headers ) failed_commits.extend(commits) + + print(create_json_result( + failed_commits=failed_commits)) - for o in failed_commits: - print(o.to_json()) if __name__ == '__main__': main()