Skip to content

Commit

Permalink
Implement support for uptimerobot
Browse files Browse the repository at this point in the history
  • Loading branch information
henryruhs committed Apr 24, 2023
1 parent a09975d commit 434136b
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -52,6 +52,7 @@ jobs:
MICROSOFT_GITHUB_TOKEN: ${{ secrets.MICROSOFT_GITHUB_TOKEN }}
NETLIFY_TOKEN: ${{ secrets.NETLIFY_TOKEN }}
TRAVIS_TOKEN: ${{ secrets.TRAVIS_TOKEN }}
UPTIMEROBOT_TOKEN: ${{ secrets.UPTIMEROBOT_TOKEN }}
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
report:
needs: test
Expand Down Expand Up @@ -87,6 +88,7 @@ jobs:
MICROSOFT_GITHUB_TOKEN: ${{ secrets.MICROSOFT_GITHUB_TOKEN }}
NETLIFY_TOKEN: ${{ secrets.NETLIFY_TOKEN }}
TRAVIS_TOKEN: ${{ secrets.TRAVIS_TOKEN }}
UPTIMEROBOT_TOKEN: ${{ secrets.UPTIMEROBOT_TOKEN }}
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
- run: coveralls --service=github
env:
Expand Down
2 changes: 1 addition & 1 deletion chroma_feedback/metadata.py
Expand Up @@ -4,7 +4,7 @@
{
'name': 'chroma-feedback',
'description': 'Turn your RGB powered hardware into a status indicator for continuous integration, continuous deployment and infrastructure monitoring',
'version': '13.0.5',
'version': '13.1.0',
'license': 'MIT',
'keywords': ' '.join(producer.ALL),
'author': 'Henry Ruhs',
Expand Down
1 change: 1 addition & 0 deletions chroma_feedback/producer/__init__.py
Expand Up @@ -18,5 +18,6 @@
'microsoft.github': 'chroma_feedback.producer.microsoft.github',
'netlify': 'chroma_feedback.producer.netlify.netlify',
'travis': 'chroma_feedback.producer.travis.travis',
'uptimerobot': 'chroma_feedback.producer.uptimerobot.uptimerobot',
'vercel': 'chroma_feedback.producer.vercel.vercel'
}
Empty file.
@@ -0,0 +1 @@
from .core import init, run
51 changes: 51 additions & 0 deletions chroma_feedback/producer/uptimerobot/uptimerobot/core.py
@@ -0,0 +1,51 @@
from argparse import ArgumentParser
from typing import List

from chroma_feedback import helper, request
from chroma_feedback.typing import Producer
from .normalize import normalize_data

ARGS = None


def init(program : ArgumentParser) -> None:
global ARGS

if not ARGS:
program.add_argument('--uptimerobot-host', default = 'https://api.uptimerobot.com')
program.add_argument('--uptimerobot-slug', action = 'append')
program.add_argument('--uptimerobot-token', required = True)
ARGS = helper.get_first(program.parse_known_args())


def run() -> List[Producer]:
result = []

if ARGS.uptimerobot_slug:
for slug in ARGS.uptimerobot_slug:
result.extend(fetch(ARGS.uptimerobot_host, slug, ARGS.uptimerobot_token))
else:
result.extend(fetch(ARGS.uptimerobot_host, None, ARGS.uptimerobot_token))
return result


def fetch(host : str, slug : str, token : str) -> List[Producer]:
result = []
response = None

if host and slug and token:
response = request.post(host + '/v2/getMonitors?api_key=' + token,
{
'search': slug
})
elif host and token:
response = request.post(host + '/v2/getMonitors?api_key=' + token)

if response and response.status_code == 200:
data = request.parse_json(response)

if 'monitors' in data:
for monitor in data['monitors']:
if 'friendly_name' in monitor and 'url' in monitor and 'status' in monitor:
result.append(normalize_data(monitor['friendly_name'], monitor['url'], monitor['status']))
return result
22 changes: 22 additions & 0 deletions chroma_feedback/producer/uptimerobot/uptimerobot/normalize.py
@@ -0,0 +1,22 @@
from chroma_feedback import helper
from chroma_feedback.typing import Producer, Status


def normalize_data(slug : str, url : str, status : int) -> Producer:
return\
{
'name': 'uptimerobot',
'slug': slug,
'url': url,
'status': normalize_status(status)
}


def normalize_status(status : int) -> Status:
if status == 0:
return 'skipped'
if status == 1:
return 'started'
if status == 9:
return 'failed'
return 'passed'
1 change: 1 addition & 0 deletions chroma_feedback/typing.py
Expand Up @@ -27,6 +27,7 @@
'microsoft.github',
'netlify',
'travis',
'uptimerobot'
'vercel'
]
ConsumerName = Literal\
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -35,6 +35,7 @@
'chroma_feedback.producer.microsoft.github',
'chroma_feedback.producer.netlify.netlify',
'chroma_feedback.producer.travis.travis',
'chroma_feedback.producer.uptimerobot.uptimerobot',
'chroma_feedback.producer.vercel.vercel',
'chroma_feedback.consumer',
'chroma_feedback.consumer.agile_innovative.blinkstick',
Expand Down
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions tests/producer/uptimerobot/uptimerobot/test_core.py
@@ -0,0 +1,42 @@
import pytest
import os
import argparse
from typing import get_args

from chroma_feedback.producer.uptimerobot import uptimerobot
from chroma_feedback.typing import Status


def test_run_one() -> None:
if os.environ.get('UPTIMEROBOT_TOKEN'):
uptimerobot.core.ARGS = argparse.Namespace(
uptimerobot_host = 'https://api.uptimerobot.com',
uptimerobot_slug =
[
'chrome-feedback-test'
],
uptimerobot_token = os.environ.get('UPTIMEROBOT_TOKEN')
)
result = uptimerobot.core.run()

assert result[0]['name'] == 'uptimerobot'
assert result[0]['slug'] == 'chrome-feedback-test'
assert result[0]['status'] in get_args(Status)
else:
pytest.skip('UPTIMEROBOT_TOKEN is not defined')


def test_run_many() -> None:
if os.environ.get('UPTIMEROBOT_TOKEN'):
uptimerobot.core.ARGS = argparse.Namespace(
uptimerobot_host = 'https://api.uptimerobot.com',
uptimerobot_slug = None,
uptimerobot_token = os.environ.get('UPTIMEROBOT_TOKEN')
)
result = uptimerobot.core.run()

assert result[0]['name'] == 'uptimerobot'
assert result[0]['slug']
assert result[0]['status'] in get_args(Status)
else:
pytest.skip('UPTIMEROBOT_TOKEN is not defined')

0 comments on commit 434136b

Please sign in to comment.