Skip to content

Commit

Permalink
Develop (#47)
Browse files Browse the repository at this point in the history
* Implement Wercker part1

* Implement Wercker part1

* Implement Wercker part2

* Implement Wercker part3

* Implement Wercker part4

* Improve testing

* Improve testing
  • Loading branch information
Henry Ruhs committed Dec 30, 2019
1 parent 55fa631 commit 29230d6
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,36 @@ chroma-feedback --producer=travis
```



Wercker
-------

| Name | Default | Mandatory |
|-------|-------------------------|-----------|
| Host | https://app.wercker.com | optional |
| Slug | | required |
| Token | | required |

Monitor a single project:

```
chroma-feedback --producer=wercker
--wercker-slug <username/application>
--wercker-token <token>
```

Monitor multiple projects:

```
chroma-feedback --producer=wercker
--wercker-slug <<username/application>
--wercker-slug <<username/application>
--wercker-token <token>
```


Consumers
=========

Expand Down
3 changes: 2 additions & 1 deletion chroma_feedback/producer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
'gitlab',
'jenkins',
'teamcity',
'travis'
'travis',
'wercker'
]
1 change: 1 addition & 0 deletions chroma_feedback/producer/wercker/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .core import init, run
67 changes: 67 additions & 0 deletions chroma_feedback/producer/wercker/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from typing import Any, Dict, List
from argparse import ArgumentParser
import requests
from chroma_feedback import helper
from .normalize import normalize_data

ARGS = None


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

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


def run() -> List[Dict[str, Any]]:
result = []

for slug in ARGS.wercker_slug:
result.extend(fetch(ARGS.wercker_host, slug, ARGS.wercker_token))
return result


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

if host and slug and token:
response = requests.get(host + '/api/v3/applications/' + slug, headers =
{
'Bearer': token
})

# process response

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

if 'id' in data:
result.extend(fetch_runs(host, slug, data['id'], token))
return result


def fetch_runs(host : str, slug : str, application_id : str, token : str) -> List[Dict[str, Any]]:
result = []
response = None

if host and slug and application_id and token:
response = requests.get(host + '/api/v3/runs?applicationId=' + application_id, headers =
{
'Bearer': token
})

# process response

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

if helper.get_first(data):
project = helper.get_first(data)
project['slug'] = slug
result.append(normalize_data(project))
return result
21 changes: 21 additions & 0 deletions chroma_feedback/producer/wercker/normalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Any, Dict


def normalize_data(project : Dict[str, Any]) -> Dict[str, Any]:
return\
{
'producer': 'wercker',
'slug': project['slug'],
'active': True,
'status': normalize_status(project['status'], project['result'])
}


def normalize_status(status : str, result : str) -> str:
if status == 'running':
return 'process'
if result == 'aborted':
return 'errored'
if result == 'failed':
return 'failed'
return 'passed'
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'chroma_feedback.producer.jenkins',
'chroma_feedback.producer.teamcity',
'chroma_feedback.producer.travis',
'chroma_feedback.producer.wercker',
'chroma_feedback.consumer',
'chroma_feedback.consumer.agile_innovative_blinkstick',
'chroma_feedback.consumer.lifx_light',
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions tests/producer/wercker/test_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import pytest
from chroma_feedback.producer.wercker.core import fetch


def test_fetch_slug() -> None:
if 'WERCKER_TOKEN' in os.environ:
result = fetch('https://app.wercker.com', 'redaxmedia/chroma-feedback', os.environ['WERCKER_TOKEN'])

assert result[0]['producer'] == 'wercker'
assert result[0]['slug'] == 'redaxmedia/chroma-feedback'
assert result[0]['active'] is True
assert result[0]['status']
else:
pytest.skip('WERCKER_TOKEN is not defined')


def test_fetch_invalid() -> None:
result = fetch(None, None, None)

assert result == []
22 changes: 22 additions & 0 deletions wercker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
box: python:3.7.6

build:
steps:
- script:
name: install
code: |
pip install requests
pip install pylint
pip install pytest
pip install pytest-mock
pip install mock
pip install mypy
- script:
name: lint
code: |
pylint bin/chroma-feedback chroma_feedback tests
mypy bin/chroma-feedback chroma_feedback tests
- script:
name: test
code: |
pytest tests

0 comments on commit 29230d6

Please sign in to comment.