Skip to content

Commit

Permalink
Handle cases when not in environment
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
coagulant committed Jul 9, 2013
1 parent 0adf3ed commit 2428911
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
34 changes: 30 additions & 4 deletions releasedate/jenkins.py
Expand Up @@ -5,21 +5,47 @@
import requests


class ImproperlyConfigured(Exception):
pass


def get_previous_tag(tag, build_number):
return tag.replace(build_number, str(int(build_number) - 1))


def get_build_context():
""" Obtain info about current CI build for api request:
* build number
* git tag for the build
* url of the CI job
"""
try:
return {
'build_number': env['BUILD_NUMBER'],
'build_tag': env['BUILD_TAG'],
'job_url': env['JOB_URL']
}
except KeyError as e:
raise ImproperlyConfigured('Missing "%s" env variable. '
'Launch job via Jenkins, or provide evn variable manually.' % e.message)


def run(url=None, repo=None, instance_url=None):
""" Send POST request to releasedate server after jenkins job is successfully finished"""
url = url or sys.argv[1]
repo = repo or sys.argv[2]
instance_url = instance_url or sys.argv[3] if len(sys.argv) > 3 else instance_url
try:
context = get_build_context()
except ImproperlyConfigured as e:
return str(e)

result = requests.post(url, data={
'build_number': env['BUILD_NUMBER'],
'build_tag': env['BUILD_TAG'],
'previous_tag': get_previous_tag(env['BUILD_TAG'], env['BUILD_NUMBER']),
'job_url': env['JOB_URL'],
'build_number': context['build_number'],
'build_tag': context['build_tag'],
'previous_tag': get_previous_tag(context['build_tag'], context['build_number']),
'job_url': context['job_url'],
'repo': repo,
'instance': instance_url,
})
Expand Down
8 changes: 7 additions & 1 deletion test_releasedate.py
Expand Up @@ -61,10 +61,16 @@ def test_client_sent_data(self):

self.assertBodyQueryString(**check_against)

jenkins.run('http://releasedate-server/', '/path/to/my/repo/', 'myserver')
output = jenkins.run('http://releasedate-server/', '/path/to/my/repo/', 'myserver')
assert 'OK' in output
check_against['instance'] = ['myserver']
self.assertBodyQueryString(**check_against)

def test_not_in_env(self):
output = jenkins.run('http://releasedate-server/', '/path/to/my/repo/')
assert 'Missing "BUILD_NUMBER" env variable.' in output
assert len(httpretty.latest_requests) == 0


class TestRunServer(TestCase):

Expand Down

0 comments on commit 2428911

Please sign in to comment.