Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Commit

Permalink
Merge 6a04129 into 991c05e
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcaricio committed May 30, 2016
2 parents 991c05e + 6a04129 commit 513b830
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
8 changes: 2 additions & 6 deletions pierone/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,12 @@ def image_exists(token_name: str, image: DockerImage) -> bool:
return image.tag in result


def get_latest_tag(token_name: str, image: DockerImage) -> bool:
token = get_existing_token(token_name)
if not token:
raise Unauthorized()

def get_latest_tag(token: str, image: DockerImage) -> bool:
url = 'https://{}'.format(image.registry)
path = '/teams/{team}/artifacts/{artifact}/tags'.format(team=image.team, artifact=image.artifact)

try:
r = request(url, path, token['access_token'])
r = request(url, path, token)
r.raise_for_status()
except:
return None
Expand Down
9 changes: 7 additions & 2 deletions pierone/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from clickclick import AliasedGroup, OutputFormat, UrlType, error, print_table

from .api import DockerImage, docker_login, get_latest_tag, request
from .exceptions import PieroneException

KEYRING_KEY = 'pierone'

Expand Down Expand Up @@ -347,14 +348,18 @@ def latest(config, team, artifact, url, output):
'''Get latest tag/version of a specific artifact'''
# validate that the token exists!
set_pierone_url(config, url)
get_token()
token = get_token()

registry = config.get('url')
if registry.startswith('https://'):
registry = registry[8:]
image = DockerImage(registry=registry, team=team, artifact=artifact, tag=None)

print(get_latest_tag('pierone', image))
latest_tag = get_latest_tag(token, image)
if latest_tag:
print(latest_tag)
else:
raise PieroneException('Latest tag not found')


@cli.command('scm-source')
Expand Down
5 changes: 5 additions & 0 deletions pierone/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from click import ClickException


class PieroneException(ClickException):
'''Thrown when something does not go as expected'''
13 changes: 0 additions & 13 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,6 @@ def test_get_latest_tag_non_json(monkeypatch):
assert data is None


def test_unauthorized(monkeypatch):
monkeypatch.setattr('pierone.api.get_existing_token', MagicMock(return_value=None))
token_name = 'dummy'
image = DockerImage(registry='registry', team='foo', artifact='bar', tag='latest')
with pytest.raises(Unauthorized) as excinfo:
get_latest_tag(token_name, image)
assert 'Unauthorized: token missing or invalid' in str(excinfo.value)

with pytest.raises(Unauthorized) as excinfo:
image_exists(token_name, image)
assert 'Unauthorized: token missing or invalid' in str(excinfo.value)


def test_image_exists(monkeypatch):
response = MagicMock()
response.status_code = 200
Expand Down
3 changes: 2 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ def test_latest_not_found(monkeypatch, tmpdir):
monkeypatch.setattr('pierone.api.session.get', MagicMock(return_value=response))
with runner.isolated_filesystem():
result = runner.invoke(cli, ['latest', 'myteam', 'myart'], catch_exceptions=False)
assert 'None' == result.output.rstrip()
assert 'Error: Latest tag not found' == result.output.rstrip()
assert result.exit_code == 1


def test_url_without_scheme(monkeypatch, tmpdir):
Expand Down

0 comments on commit 513b830

Please sign in to comment.