Skip to content

Commit

Permalink
Merge pull request ksdme#28 from SerekKiri/travis-status
Browse files Browse the repository at this point in the history
Adds travis status support
  • Loading branch information
ksdme authored Nov 12, 2018
2 parents 4e421bf + 0a84e5e commit ec0972c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
22 changes: 21 additions & 1 deletion org_status/org_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(':')
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions org_status/status_providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Status(Enum):


class StatusProvider:
NAME = None
BadgeTemplate = None

def __init__(self, group):
Expand All @@ -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,
)
1 change: 1 addition & 0 deletions org_status/status_providers/appveyor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class AppVeyorStatus(StatusProvider):
NAME = 'appveyor'
BadgeTemplate = ('https://ci.appveyor.com/api/projects/status/{host}/'
'{group}/{repo}?branch={branch}&svg=true')

Expand Down
1 change: 1 addition & 0 deletions org_status/status_providers/gitlab_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class GitLabCIStatus(StatusProvider):
NAME = 'gitlabci'
BadgeTemplate = ('https://gitlab.com/{group}'
'/{repo}/badges/{branch}/pipeline.svg')

Expand Down
15 changes: 15 additions & 0 deletions org_status/status_providers/travis.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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

0 comments on commit ec0972c

Please sign in to comment.