Skip to content

Commit

Permalink
Add the ability to affix labels to all pull requests created (#22)
Browse files Browse the repository at this point in the history
* Add the ability to affix labels to all pull requests created

* Update unit tests, Update to use a blank list instead of None

* Update changelog

* Remove extra return character in change log

* Add default labels test

* Set test call for set_labels to ANY for default labels test
  • Loading branch information
kaosx5s authored Jun 23, 2020
1 parent e5f1f88 commit 1b8abee
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com),
and this project adheres to [Semantic Versioning](https://semver.org).

## [1.5.0] - 2020-06-23
### Added
- Added the ability to set labels to sent pull requests using a flag.

## [1.4.1] - 2020-06-18
### Fixed
- set_target_branch was not cleaning the file cache, resulting in gordian not being able to get new files in the new branches.
Expand Down
13 changes: 11 additions & 2 deletions gordian/gordian.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def get_basic_parser():
dest='force_changelog',
help='Fail if changelog does not exist or cannot be parsed'
)
parser.add_argument(
'-l','--labels',
required=False,
default=[],
nargs='+',
dest='pr_labels',
help='List of space separated label names you wish to add to your pull request(s)'
)
bumpers = parser.add_mutually_exclusive_group(required=False)
bumpers.add_argument(
'-M', '--major',
Expand Down Expand Up @@ -149,13 +157,14 @@ def transform(args, transformations, repositories, pr_created_callback):
if not args.dry_run:
try:
pull_request = repo._repo.create_pull(args.pr_message, '', args.target_branch, repo.branch_name)
pull_request.set_labels(*args.pr_labels)
pull_request_urls.append(pull_request.html_url)
if pr_created_callback is not None:
logger.debug(f'Calling post pr created callback with: {pull_request}, {repo.branch_name}')
pr_created_callback(repo_name, pull_request)
logger.info(f'PR created: {args.pr_message}. Branch: {repo.branch_name}')
logger.info(f'PR created: {args.pr_message}. Branch: {repo.branch_name}. Labels: {args.pr_labels}')
except GithubException as e:
logger.info(f'PR already exists for {repo.branch_name}')
logger.info(f'PR already exists for {repo.branch_name}. Error: {e}')

if pull_request_urls:
logger.info('Pull requests')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup_reqs = ['pytest', 'pytest-cov', 'pytest-runner', 'flake8']
setuptools.setup(
name="gordian",
version="1.4.1",
version="1.5.0",
author="Intuit",
author_email="[email protected]",
description="A tool to search and replace files in a Git repo",
Expand Down
16 changes: 16 additions & 0 deletions tests/test_gordian.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, config_file='./tests/fixtures/test_config.yaml', dry_run = Fa
self.github_api = None
self.semver_label = None
self.target_branch = 'master'
self.pr_labels = ['test']

def test_apply_transformations_without_changes(self):
with patch('gordian.gordian.Repo') as RepoMock, patch('gordian.transformations.Transformation') as TransformationMockClass:
Expand All @@ -36,6 +37,7 @@ def test_apply_transformations_with_changes(self):
apply_transformations(TestGordian.Args(), [TransformationMockClass])
RepoMock.assert_has_calls([call().bump_version(False), call().bump_version(False)], any_order=True)
RepoMock.assert_has_calls([call()._repo.create_pull('test', '', 'master', ANY), call()._repo.create_pull('test', '', 'master', ANY)], any_order=True)
RepoMock.assert_has_calls([call()._repo.create_pull().set_labels('test'), call()._repo.create_pull().set_labels('test')], any_order=True)

def test_apply_transformations_with_changes_dry_run(self):
with patch('gordian.gordian.Repo') as RepoMock, patch('gordian.transformations.Transformation', ) as TransformationMockClass:
Expand All @@ -44,6 +46,7 @@ def test_apply_transformations_with_changes_dry_run(self):
apply_transformations(TestGordian.Args(dry_run=True), [TransformationMockClass])
RepoMock.assert_has_calls([call().bump_version(True), call().bump_version(True)], any_order=True)
self.assertNotIn(call().repo.create_pull('test', '', 'master', ANY), RepoMock.mock_calls)
self.assertNotIn(call()._repo.create_pull().set_labels('test'), RepoMock.mock_calls)

def test_apply_transformations_with_changes_and_callback(self):
with patch('gordian.gordian.Repo') as RepoMock, patch('gordian.transformations.Transformation', ) as TransformationMockClass:
Expand All @@ -55,6 +58,7 @@ def test_apply_transformations_with_changes_and_callback(self):
apply_transformations(args, [TransformationMockClass], callback_mock)
pull_request = RepoMock.return_value._repo.create_pull.return_value
RepoMock.assert_has_calls([call().bump_version(False), call().bump_version(False)], any_order=True)
RepoMock.assert_has_calls([call()._repo.create_pull().set_labels('test'), call()._repo.create_pull().set_labels('test')], any_order=True)
RepoMock.assert_has_calls([
call()._repo.create_pull('test', '', 'target_branch', ANY),
call()._repo.create_pull('test', '', 'target_branch', ANY)],
Expand All @@ -63,3 +67,15 @@ def test_apply_transformations_with_changes_and_callback(self):
call('testOrg/TestService1', pull_request),
call('testOrg/TestService2', pull_request)]
)

def test_apply_transformations_with_changes_default_labels(self):
with patch('gordian.gordian.Repo') as RepoMock, patch('gordian.transformations.Transformation', ) as TransformationMockClass:
instance = RepoMock.return_value
instance.dirty = True
gordian_args = TestGordian.Args()
gordian_args.pr_labels = []
apply_transformations(gordian_args, [TransformationMockClass])
RepoMock.assert_has_calls([call().bump_version(False), call().bump_version(False)], any_order=True)
RepoMock.assert_has_calls([call()._repo.create_pull('test', '', 'master', ANY), call()._repo.create_pull('test', '', 'master', ANY)], any_order=True)
self.assertNotIn(call()._repo.create_pull().set_labels(ANY), RepoMock.mock_calls)

0 comments on commit 1b8abee

Please sign in to comment.