Skip to content

Commit

Permalink
Implement Codeship provider
Browse files Browse the repository at this point in the history
  • Loading branch information
redaxmedia committed Nov 4, 2019
1 parent 50d5f9d commit d5323e8
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 10 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,41 @@ chroma-feedback --provider=circle
```


Codeship
--------

| Name | Default | Mandatory |
|--------------|--------------------------|-----------|
| Host | https://api.codeship.com | optional |
| Organization | | required |
| Project | | required |
| Username | | required |
| Password | | required |

Monitor a single project:

```
chroma-feedback --provider=codeship
--codeship-oranization <oranization-uuid>
--codeship-oranization <project-uuid>
--codeship-username <username>
--codeship-password <password>
```

Monitor multiple projects:

```
chroma-feedback --provider=codeship
--codeship-oranization <oranization-uuid>
--codeship-oranization <project-uuid>
--codeship-oranization <project-uuid>
--codeship-username <username>
--codeship-password <password>
```


GitHub
------

Expand Down
2 changes: 1 addition & 1 deletion chroma_feedback/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
'name': 'chroma-feedback',
'description': 'Turn your RGB powered hardware into an extreme feedback device for continuous integration',
'version': '5.0.1',
'version': '5.1.0',
'license': 'GPL-3.0',
'keywords': 'appveyor circle github gitlab jenkins teamcity travis ci notification indication',
'author': 'Henry Ruhs',
Expand Down
1 change: 1 addition & 0 deletions chroma_feedback/provider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[
'appveyor',
'circle',
'codeship',
'github',
'gitlab',
'jenkins',
Expand Down
1 change: 1 addition & 0 deletions chroma_feedback/provider/codeship/__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/provider/codeship/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import base64
import requests
from chroma_feedback import helper
from .normalize import normalize_data

ARGS = None


def init(program):
global ARGS

if not ARGS:
program.add_argument('--codeship-host', default = 'https://api.codeship.com')
program.add_argument('--codeship-organization', required = True)
program.add_argument('--codeship-project', action = 'append', required = True)
program.add_argument('--codeship-username', required = True)
program.add_argument('--codeship-password', required = True)
ARGS = program.parse_known_args()[0]


def run():
result = []
token = fetch_token(ARGS.codeship_host, ARGS.codeship_username, ARGS.codeship_password)

if token:
for project in ARGS.codeship_project:
result.extend(fetch(ARGS.codeship_host, ARGS.codeship_organization, project, token))
return result


def fetch(host, organization, project, token):
response = None

if host and organization and project and token:
response = requests.get(host + '/v2/organizations/' + organization + '/projects/' + project + '/builds', headers =
{
'Authorization': 'Bearer ' + token
})

# process response

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

if 'builds' in data:
return normalize_data(data['builds'][0])
return []


def fetch_token(host, username, password):
response = None

if host and username and password:
username_token = username + ':' + password
response = requests.post(host + '/v2/auth', headers =
{
'Authorization': 'Basic ' + base64.b64encode(username_token.encode('utf-8')).decode('ascii')
})

# process response

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

if data['access_token']:
return data['access_token']
return None
21 changes: 21 additions & 0 deletions chroma_feedback/provider/codeship/normalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def normalize_data(build):
return\
[
{
'provider': 'codeship',
'slug': build['project_id'],
'active': True,
'status': normalize_status(build['status'])
}
]


def normalize_status(status):
if status in ['initiated', 'waiting']:
return 'process'
if status in ['error', 'blocked', 'ignored']:
return 'errored'
if status in ['failed', 'infrastructure_failure']:
return 'failed'
return 'passed'

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'chroma_feedback.provider',
'chroma_feedback.provider.appveyor',
'chroma_feedback.provider.circle',
'chroma_feedback.provider.codeship',
'chroma_feedback.provider.github',
'chroma_feedback.provider.gitlab',
'chroma_feedback.provider.jenkins',
Expand Down
5 changes: 4 additions & 1 deletion tests/consumer/lifx_light/test_light.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from mock import MagicMock
try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock
from chroma_feedback.consumer.lifx_light.light import process_lights

MOCK = MagicMock()
Expand Down
5 changes: 4 additions & 1 deletion tests/consumer/philips_hue/test_light.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from mock import MagicMock
try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock
from chroma_feedback.consumer.philips_hue.light import process_lights

MOCK = MagicMock()
Expand Down
5 changes: 4 additions & 1 deletion tests/consumer/razer_chroma/test_device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from mock import MagicMock
try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock
from chroma_feedback.consumer.razer_chroma.device import process_devices

MOCK = MagicMock()
Expand Down
5 changes: 4 additions & 1 deletion tests/consumer/thingm_blink/test_device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from mock import MagicMock
try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock
from chroma_feedback.consumer.thingm_blink.device import process_devices

MOCK = MagicMock()
Expand Down
5 changes: 4 additions & 1 deletion tests/consumer/xiaomi_yeelight/test_light.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from mock import MagicMock
try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock
from chroma_feedback.consumer.xiaomi_yeelight.light import process_lights

MOCK = MagicMock()
Expand Down
2 changes: 1 addition & 1 deletion tests/provider/appveyor/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_fetch_user():
assert result[0]['active'] is True
assert result[0]['status']
else:
pytest.skip('APPVEYOR_TOKEN not defined')
pytest.skip('APPVEYOR_TOKEN is not defined')


def test_fetch_invalid():
Expand Down
2 changes: 1 addition & 1 deletion tests/provider/circle/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_fetch_user():
assert result[0]['active'] is True
assert result[0]['status']
else:
pytest.skip('CIRCLE_TOKEN not defined')
pytest.skip('CIRCLE_TOKEN is not defined')


def test_fetch_invalid():
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions tests/provider/codeship/test_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import pytest
from chroma_feedback.provider.codeship.core import fetch, fetch_token


def test_fetch_project():
if 'CODESHIP_USERNAME' in os.environ and 'CODESHIP_PASSWORD' in os.environ and 'CODESHIP_ORGANIZATION' in os.environ and 'CODESHIP_PROJECT' in os.environ:
token = fetch_token('https://api.codeship.com', os.environ['CODESHIP_USERNAME'], os.environ['CODESHIP_PASSWORD'])
result = fetch('https://api.codeship.com', os.environ['CODESHIP_ORGANIZATION'], os.environ['CODESHIP_PROJECT'], token)

assert result[0]['provider'] == 'codeship'
assert result[0]['active'] is True
assert result[0]['status']
else:
pytest.skip('CODESHIP_USERNAME or CODESHIP_PASSWORD or CODESHIP_ORGANIZATION or CODESHIP_PROJECT is not defined')


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

assert result == []
2 changes: 1 addition & 1 deletion tests/provider/github/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_fetch_slug():
assert result[0]['active'] is True
assert result[0]['status']
else:
pytest.skip('GITHUB_TOKEN not defined')
pytest.skip('GITHUB_TOKEN is not defined')


def test_fetch_invalid():
Expand Down
2 changes: 1 addition & 1 deletion tests/provider/gitlab/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_fetch_slug():
assert result[0]['active'] is True
assert result[0]['status']
else:
pytest.skip('GITLAB_TOKEN not defined')
pytest.skip('GITLAB_TOKEN is not defined')


def test_fetch_invalid():
Expand Down
1 change: 1 addition & 0 deletions tests/provider/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from chroma_feedback import provider


def test_process(mocker):
program = ArgumentParser()
program.add_argument('-P', '--provider', action = 'append', required = True)
Expand Down

0 comments on commit d5323e8

Please sign in to comment.