Skip to content

Commit

Permalink
Finalize codeship integration
Browse files Browse the repository at this point in the history
  • Loading branch information
redaxmedia committed Nov 7, 2019
1 parent 1f462d2 commit 80f6577
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,19 @@ chroma-feedback --provider=circle
Codeship
--------

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

Monitor a single project:

```
chroma-feedback --provider=codeship
--codeship-oranization <oranization-uuid>
--codeship-project <project-uuid>
--codeship-slug <project-id>
--codeship-username <username>
--codeship-password <password>
```
Expand All @@ -146,9 +144,6 @@ Monitor multiple projects:
```
chroma-feedback --provider=codeship
--codeship-oranization <oranization-uuid>
--codeship-project <project-uuid>
--codeship-project <project-uuid>
--codeship-username <username>
--codeship-password <password>
```
Expand Down
42 changes: 31 additions & 11 deletions chroma_feedback/provider/codeship/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,44 @@ def init(program):

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-slug', action = 'append')
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)
auth = fetch_auth(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))
for organization in auth['organizations']:
result.extend(fetch(ARGS.codeship_host, organization['uuid'], ARGS.codeship_slug, auth['access_token']))
return result


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

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

# process response

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

for project in data['projects']:
if not slug or str(project['id']) in slug:
result.extend(fetch_builds(host, organization, project['uuid'], token))
return result
return []


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

if host and organization and project and token:
Expand All @@ -42,12 +62,12 @@ def fetch(host, organization, project, token):
if response and response.status_code == 200:
data = helper.parse_json(response)

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


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

if host and username and password:
Expand All @@ -62,6 +82,6 @@ def fetch_token(host, username, password):
if response and response.status_code == 200:
data = helper.parse_json(response)

if data['access_token']:
return data['access_token']
if data:
return data
return None
2 changes: 1 addition & 1 deletion chroma_feedback/provider/codeship/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def normalize_data(build):


def normalize_status(status):
if status in ['initiated', 'waiting']:
if status in ['initiated', 'testing', 'waiting']:
return 'process'
if status in ['error', 'blocked', 'ignored']:
return 'errored'
Expand Down
2 changes: 1 addition & 1 deletion chroma_feedback/provider/gitlab/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def normalize_data(project):
[
{
'provider': 'gitlab',
'slug': project['user']['username'] + '/' + project['slug'] + '/' + project['name'],
'slug': project['slug'] + '/' + project['name'],
'active': True,
'status': normalize_status(project['status'])
}
Expand Down
1 change: 1 addition & 0 deletions tests/provider/appveyor/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_fetch_user():
result = fetch('https://ci.appveyor.com', None, os.environ['APPVEYOR_TOKEN'])

assert result[0]['provider'] == 'appveyor'
assert result[0]['slug']
assert result[0]['active'] is True
assert result[0]['status']
else:
Expand Down
1 change: 1 addition & 0 deletions tests/provider/circle/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_fetch_user():
result = fetch('https://circleci.com', None, os.environ['CIRCLE_TOKEN'])

assert result[0]['provider'] == 'circle'
assert result[0]['slug']
assert result[0]['active'] is True
assert result[0]['status']
else:
Expand Down
29 changes: 23 additions & 6 deletions tests/provider/codeship/test_core.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import os
import pytest
from chroma_feedback.provider.codeship.core import fetch, fetch_token
from chroma_feedback.provider.codeship.core import fetch, fetch_auth


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)
def test_fetch_slug():
if 'CODESHIP_USERNAME' in os.environ and 'CODESHIP_PASSWORD' in os.environ:
result = []
auth = fetch_auth('https://api.codeship.com', os.environ['CODESHIP_USERNAME'], os.environ['CODESHIP_PASSWORD'])
for organization in auth['organizations']:
result.extend(fetch('https://api.codeship.com', organization['uuid'], '372431', auth['access_token']))

assert result[0]['provider'] == 'codeship'
assert result[0]['slug'] == '372431'
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')
pytest.skip('CODESHIP_USERNAME or CODESHIP_PASSWORD')


def test_fetch_user():
if 'CODESHIP_USERNAME' in os.environ and 'CODESHIP_PASSWORD' in os.environ:
result = []
auth = fetch_auth('https://api.codeship.com', os.environ['CODESHIP_USERNAME'], os.environ['CODESHIP_PASSWORD'])
for organization in auth['organizations']:
result.extend(fetch('https://api.codeship.com', organization['uuid'], None, auth['access_token']))

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


def test_fetch_invalid():
Expand Down
1 change: 1 addition & 0 deletions tests/provider/gitlab/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def test_fetch_slug():
result = fetch('https://gitlab.com', '7311836', os.environ['GITLAB_TOKEN'])

assert result[0]['provider'] == 'gitlab'
assert result[0]['slug']
assert result[0]['active'] is True
assert result[0]['status']
else:
Expand Down
7 changes: 4 additions & 3 deletions tests/provider/travis/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ def test_fetch_slug():
def test_fetch_user():
result = fetch('https://api.travis-ci.org', 'redaxmedia')

assert result[1]['provider'] == 'travis'
assert result[1]['active'] is True
assert result[1]['status']
assert result[0]['provider'] == 'travis'
assert result[0]['slug']
assert result[0]['active'] is True
assert result[0]['status']


def test_fetch_invalid():
Expand Down

0 comments on commit 80f6577

Please sign in to comment.