From 0a84e5e31f200c85dcd6532e5abe3ef66f21622b Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 12 Nov 2018 11:36:09 +0100 Subject: [PATCH] travis: Add travis system status check Closes https://github.com/ksdme/org-status/issues/2 travis: Fix issues with code --- org_status/org_status.py | 22 +++++++++++++++++++++- org_status/status_providers/__init__.py | 17 +++++++++++++++++ org_status/status_providers/appveyor.py | 1 + org_status/status_providers/gitlab_ci.py | 1 + org_status/status_providers/travis.py | 15 +++++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) diff --git a/org_status/org_status.py b/org_status/org_status.py index 88f37f0..e3e9207 100644 --- a/org_status/org_status.py +++ b/org_status/org_status.py @@ -4,7 +4,7 @@ from termcolor import colored -from org_status.status_providers import Status +from org_status.status_providers import Status, get_supported_status_providers from org_status.org_hosts import get_all_supported_hosts from org_status.encoders import get_all_supported_encoders @@ -13,6 +13,14 @@ def get_host_token(host_name): return environ['{}_TOKEN'.format(host_name.upper())] +def get_status_provider_statuses(): + for provider in get_supported_status_providers(): + try: + yield (provider, provider.get_status_provider_status()) + except NotImplementedError: + yield (provider, None) + + def generate_fetch_jobs(org_strings): for org_string in org_strings: host, sym, org = org_string.strip().partition(':') @@ -78,6 +86,7 @@ def get_argument_parser(): parser.add_argument('--skip-host-checks', action='store_true') parser.add_argument('--export-repos', type=str) parser.add_argument('--format', type=str, default='gitman') + parser.add_argument('--check-providers-only', action='store_true') return parser @@ -120,6 +129,17 @@ def main(): hosts_only_print = print if args.hosts_only else verbose + if args.check_providers_only: + for provider, status in get_status_provider_statuses(): + if provider is not None: + print(styled(f'{provider.NAME}: {str(status).lower()}', + 'green' if status else 'red')) + else: + print(styled( + f'{provider.NAME} does not support status check', 'red')) + + return + if len(args.orgs) == 0 and args.hosts_only: args.orgs = ['coala'] elif len(args.orgs) == 0: diff --git a/org_status/status_providers/__init__.py b/org_status/status_providers/__init__.py index 73ea4b2..1ef557c 100644 --- a/org_status/status_providers/__init__.py +++ b/org_status/status_providers/__init__.py @@ -18,6 +18,7 @@ class Status(Enum): class StatusProvider: + NAME = None BadgeTemplate = None def __init__(self, group): @@ -38,3 +39,19 @@ def status_from_badge_svg(self, svg): for variant in variants: if variant in svg: return status + + @classmethod + def get_status_provider_status(self): + raise NotImplementedError() + + +def get_supported_status_providers(): + from org_status.status_providers.travis import TravisBuildStatus + from org_status.status_providers.gitlab_ci import GitLabCIStatus + from org_status.status_providers.appveyor import AppVeyorStatus + + return ( + TravisBuildStatus, + GitLabCIStatus, + AppVeyorStatus, + ) diff --git a/org_status/status_providers/appveyor.py b/org_status/status_providers/appveyor.py index 5585ecd..48275c1 100644 --- a/org_status/status_providers/appveyor.py +++ b/org_status/status_providers/appveyor.py @@ -4,6 +4,7 @@ class AppVeyorStatus(StatusProvider): + NAME = 'appveyor' BadgeTemplate = ('https://ci.appveyor.com/api/projects/status/{host}/' '{group}/{repo}?branch={branch}&svg=true') diff --git a/org_status/status_providers/gitlab_ci.py b/org_status/status_providers/gitlab_ci.py index 9dd21c6..ed32ad9 100644 --- a/org_status/status_providers/gitlab_ci.py +++ b/org_status/status_providers/gitlab_ci.py @@ -4,6 +4,7 @@ class GitLabCIStatus(StatusProvider): + NAME = 'gitlabci' BadgeTemplate = ('https://gitlab.com/{group}' '/{repo}/badges/{branch}/pipeline.svg') diff --git a/org_status/status_providers/travis.py b/org_status/status_providers/travis.py index 1964772..ac766cd 100644 --- a/org_status/status_providers/travis.py +++ b/org_status/status_providers/travis.py @@ -1,12 +1,16 @@ +import json import requests from org_status.status_providers import StatusProvider, Status class TravisBuildStatus(StatusProvider): + NAME = 'travis' BadgeTemplate = ('https://api.travis-ci.org/{group}' '/{repo}.svg?branch={branch}') + TravisStatusUrl = 'https://pnpcptp8xh9k.statuspage.io/api/v2/status.json' + def get_status(self, repo, host, branch='master'): badge_result = requests.get(self.get_badge_url(repo, host, @@ -16,3 +20,14 @@ def get_status(self, repo, host, branch='master'): return Status.UNDETERMINED return self.status_from_badge_svg(badge_result.text) + + @classmethod + def get_status_provider_status(cls): + try: + response = requests.get(cls.TravisStatusUrl) + status = json.loads(response.text) + + description = status['status']['description'] + return description.lower() == 'all systems operational' + except Exception: + return None