-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from LukaszSielski/extract_commons
modify
- Loading branch information
Showing
3 changed files
with
21 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
from subprocess import check_output, CalledProcessError, STDOUT | ||
from subprocess import check_call | ||
|
||
class AzCliException(Exception): | ||
def __init__(self, error): | ||
super().__init__(f'Failed to execute AZ CLI command! Error: {error}') | ||
|
||
def tag_work_items(work_items_ids: list[str], tag: str) -> None: | ||
for work_item_id in work_items_ids: | ||
print(f'Trying to tag {work_item_id} with tag {tag}') | ||
try: | ||
check_output(f'az boards work-item update --id {work_item_id} --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags={tag}"', shell=True, text=True, stderr=STDOUT) | ||
print(f'Successfully updated work item {work_item_id} with tag {tag}') | ||
except CalledProcessError as e: | ||
raise AzCliException(f"Update process failed for {work_item_id}, error: {e.output}") | ||
check_call(f'az boards work-item update --id {work_item_id} --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags={tag}"', shell=True) | ||
print(f'Successfully updated work item {work_item_id} with tag {tag}') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,50 @@ | ||
import unittest | ||
from unittest import mock | ||
import argparse | ||
from gh_cli import PrNotFoundException, GhCliException | ||
from az_cli import AzCliException | ||
from gh_cli import PrNotFoundException | ||
import script | ||
import subprocess | ||
|
||
@mock.patch('script.argparse.ArgumentParser.parse_args', return_value=argparse.Namespace(commit_sha='123', deploy_env='dev')) | ||
@mock.patch('gh_cli.check_output') | ||
@mock.patch('az_cli.check_output') | ||
@mock.patch('az_cli.check_call') | ||
class TestTagging(unittest.TestCase): | ||
|
||
def test_should_properly_tag_ado_work_item_if_single_work_item_tag_in_pr_body(self, az_cli_check_output, gh_cli_check_output, parse_args_mock): | ||
def test_should_properly_tag_ado_work_item_if_single_work_item_tag_in_pr_body(self, az_cli_check_call, gh_cli_check_output, parse_args_mock): | ||
gh_cli_check_output.return_value = '[{"body":"AB#123456"}]' | ||
|
||
script.main() | ||
|
||
gh_cli_check_output.assert_called_once_with('gh pr list --json body --state merged --search 123', shell=True, text=True, stderr=-2) | ||
az_cli_check_output.assert_called_once_with('az boards work-item update --id 123456 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True, text=True, stderr=-2) | ||
gh_cli_check_output.assert_called_once_with('gh pr list --json body --state merged --search 123', shell=True, text=True) | ||
az_cli_check_call.assert_called_once_with('az boards work-item update --id 123456 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True) | ||
|
||
def test_should_properly_tag_ado_work_items_if_many_work_item_tag_in_pr_body(self, az_cli_check_output, gh_cli_check_output, parse_args_mock): | ||
def test_should_properly_tag_ado_work_items_if_many_work_item_tag_in_pr_body(self, az_cli_check_call, gh_cli_check_output, parse_args_mock): | ||
gh_cli_check_output.return_value = '[{"body":"AB#123456 AB#654321"}]' | ||
|
||
script.main() | ||
|
||
gh_cli_check_output.assert_called_once_with('gh pr list --json body --state merged --search 123', shell=True, text=True, stderr=-2) | ||
az_cli_check_output.assert_has_calls([ | ||
mock.call('az boards work-item update --id 123456 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True, text=True, stderr=-2), | ||
mock.call('az boards work-item update --id 654321 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True, text=True, stderr=-2), | ||
gh_cli_check_output.assert_called_once_with('gh pr list --json body --state merged --search 123', shell=True, text=True) | ||
az_cli_check_call.assert_has_calls([ | ||
mock.call('az boards work-item update --id 123456 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True), | ||
mock.call('az boards work-item update --id 654321 --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags=VNXT CL Dev"', shell=True), | ||
]) | ||
|
||
def test_should_throw_exception_if_pr__not_found(self, az_cli_check_output, gh_cli_check_output, parse_args_mock): | ||
def test_should_throw_exception_if_pr__not_found(self, az_cli_check_call, gh_cli_check_output, parse_args_mock): | ||
gh_cli_check_output.return_value = '[]' | ||
|
||
with self.assertRaises(PrNotFoundException) as e: | ||
script.main() | ||
self.assertEquals('Could not find pull request for commit identitifed by 123 SHA', str(e.exception)) | ||
|
||
def test_should_throw_exception_if_gh_cli_command_failed(self, az_cli_check_output, gh_cli_check_output, parse_args_mock): | ||
def test_should_throw_exception_if_gh_cli_command_failed(self, az_cli_check_call, gh_cli_check_output, parse_args_mock): | ||
gh_cli_check_output.side_effect = subprocess.CalledProcessError('test', 'test', 'Error!') | ||
|
||
with self.assertRaises(GhCliException) as e: | ||
with self.assertRaises(subprocess.CalledProcessError): | ||
script.main() | ||
self.assertEquals('Failed to execute GH CLI command! Error: Error!', str(e.exception)) | ||
|
||
def test_should_throw_exception_if_az_cli_command_failed(self, az_cli_check_output, gh_cli_check_output, parse_args_mock): | ||
def test_should_throw_exception_if_az_cli_command_failed(self, az_cli_check_call, gh_cli_check_output, parse_args_mock): | ||
gh_cli_check_output.return_value = '[{"body":"AB#123456 AB#654321"}]' | ||
az_cli_check_output.side_effect = subprocess.CalledProcessError('test', 'test', 'Error!') | ||
az_cli_check_call.side_effect = subprocess.CalledProcessError('test', 'test', 'Error!') | ||
|
||
with self.assertRaises(AzCliException) as e: | ||
with self.assertRaises(subprocess.CalledProcessError): | ||
script.main() | ||
self.assertEquals('Failed to execute AZ CLI command! Error: Update process failed for 123456, error: Error!', str(e.exception)) |