Skip to content

Commit

Permalink
feat(publish): Add publishing of changelog to github
Browse files Browse the repository at this point in the history
  • Loading branch information
relekang committed Aug 19, 2015
1 parent 345c299 commit 74324ba
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
22 changes: 19 additions & 3 deletions semantic_release/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import click

from .history import evaluate_version_bump, get_current_version, get_new_version, set_new_version
from .history.logs import CHANGELOG_SECTIONS, generate_changelog
from .hvcs import check_build_status
from .history.logs import CHANGELOG_SECTIONS, generate_changelog, markdown_changelog
from .hvcs import check_build_status, check_token, post_changelog
from .pypi import upload_to_pypi
from .settings import config
from .vcs_helpers import (commit_new_version, get_current_head_hash, get_repository_owner_and_name,
Expand Down Expand Up @@ -85,12 +85,28 @@ def publish(**kwargs):
"""
Runs the version task before pushing to git and uploading to pypi.
"""
current_version = get_current_version()
click.echo('Current version: {0}'.format(current_version))
level_bump = evaluate_version_bump(current_version, kwargs['force_level'])
new_version = get_new_version(current_version, level_bump)
if version(**kwargs):
push_new_version()
upload_to_pypi()
owner, name = get_repository_owner_and_name()
if check_token():
click.echo('Updating changelog')
post_changelog(
owner,
name,
new_version,
markdown_changelog(new_version, generate_changelog(new_version), header=False)
)
else:
click.echo(click.style('Missing token: cannot post changelog', 'red'), err=True)

click.echo(click.style('New release published', 'green'))
else:
click.echo('Version failed, no release will be published.')
click.echo('Version failed, no release will be published.', err=True)


if __name__ == '__main__':
Expand Down
18 changes: 18 additions & 0 deletions semantic_release/hvcs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

import requests

from .errors import ImproperConfigurationError
Expand Down Expand Up @@ -46,3 +47,20 @@ def check_build_status(owner, repository, ref):
:return: A boolean with the build status
"""
return get_hvcs().check_build_status(owner, repository, ref)


def post_changelog(owner, repository, version, changelog):
"""
Posts the changelog to the current hcvs release API
:param owner: The owner of the repository
:param repository: The repository name
:param version: A string with the new version
:param changelog: A string with the changelog in correct format
:return: a tuple with successtatus and payload from hvcs
"""
return get_hvcs().post_release_changelog(owner, repository, version, changelog)


def check_token():
return get_hvcs().token() is not None
15 changes: 11 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,32 @@ def test_version_check_build_status_not_called_if_disabled(self, mock_check_buil
self.runner.invoke(main, ['version'])
self.assertFalse(mock_check_build_status.called)

@mock.patch('semantic_release.cli.post_changelog')
@mock.patch('semantic_release.cli.upload_to_pypi')
@mock.patch('semantic_release.cli.push_new_version')
@mock.patch('semantic_release.cli.version', return_value=False)
def test_publish_should_do_nothing(self, mock_version, mock_push, mock_upload):
def test_publish_should_do_nothing(self, mock_version, mock_push, mock_upload, mock_log):
result = self.runner.invoke(main, ['publish'])
self.assertEqual(result.exit_code, 0)
mock_version.assert_called_once_with(noop=False, force_level=None)
self.assertFalse(mock_push.called)
self.assertFalse(mock_upload.called)
self.assertFalse(mock_log.called)
self.assertEqual(result.exit_code, 0)

@mock.patch('semantic_release.cli.post_changelog')
@mock.patch('semantic_release.cli.upload_to_pypi')
@mock.patch('semantic_release.cli.push_new_version')
@mock.patch('semantic_release.cli.version', return_value=True)
def test_publish_should_call_functions(self, mock_version, mock_push, mock_upload):
@mock.patch('semantic_release.cli.markdown_changelog', lambda *x, **y: 'CHANGES')
@mock.patch('semantic_release.cli.get_new_version', lambda *x: '2.0.0')
@mock.patch('semantic_release.cli.check_token', lambda: True)
def test_publish_should_call_functions(self, mock_version, mock_push, mock_upload, mock_log):
result = self.runner.invoke(main, ['publish'])
self.assertEqual(result.exit_code, 0)
mock_version.assert_called_once_with(noop=False, force_level=None)
mock_push.assert_called_once_with()
mock_upload.assert_called_once_with()
mock_log.assert_called_once_with('relekang', 'python-semantic-release', '2.0.0', 'CHANGES')
self.assertEqual(result.exit_code, 0)

@mock.patch('semantic_release.cli.changelog', return_value=True)
def test_changelog_should_call_functions(self, mock_changelog):
Expand Down
12 changes: 10 additions & 2 deletions tests/test_hvcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import responses

from semantic_release.errors import ImproperConfigurationError
from semantic_release.hvcs import Github, check_build_status, get_hvcs
from semantic_release.hvcs import Github, check_build_status, check_token, get_hvcs

from . import mock

Expand All @@ -22,6 +22,14 @@ def test_check_build_status(self, mock_github_helper):
check_build_status('owner', 'name', 'ref')
mock_github_helper.assert_called_once_with('owner', 'name', 'ref')

@mock.patch('semantic_release.hvcs.Github.token', lambda: 'token')
def test_check_token_should_return_true(self):
self.assertTrue(check_token())

@mock.patch('semantic_release.hvcs.Github.token', lambda: None)
def test_check_token_should_return_false(self):
self.assertFalse(check_token())


class GithubCheckBuildStatusTests(TestCase):
url = ('https://api.github.com/repos/relekang/rmoq/commits/'
Expand Down Expand Up @@ -67,6 +75,7 @@ def test_should_return_true_if_success(self):
self.assertTrue(Github.check_build_status('relekang', 'rmoq',
'6dcb09b5b57875f334f61aebed695e2e4193db5e'))


class GithubReleaseTests(TestCase):
url = 'https://api.github.com/repos/relekang/rmoq/releases'

Expand Down Expand Up @@ -103,4 +112,3 @@ def test_should_return_false_status_if_it_failed(self):
content_type='application/json'
)
self.assertFalse(Github.post_release_changelog('relekang', 'rmoq', '1.0.0', 'text')[0])

0 comments on commit 74324ba

Please sign in to comment.