Skip to content

Commit

Permalink
Merge pull request #50 from LukaszSielski/extract_commons
Browse files Browse the repository at this point in the history
modify
  • Loading branch information
LukaszSielski authored Sep 26, 2024
2 parents 2f2d003 + 99b582f commit 236faaa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
10 changes: 3 additions & 7 deletions scripts/az_cli.py
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}')

13 changes: 2 additions & 11 deletions scripts/gh_cli.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import re
import json
from enum import Enum
from subprocess import check_output, CalledProcessError, STDOUT
from subprocess import check_output

class PrState(str, Enum):
OPEN = 'open'
CLOSED = 'closed'
MERGED = 'merged'
ALL = 'all'

class GhCliException(Exception):
def __init__(self, error):
super().__init__(f'Failed to execute GH CLI command! Error: {error}')

class PrNotFoundException(Exception):
def __init__(self, commit_sha: str):
super().__init__(f"Could not find pull request for commit identitifed by {commit_sha} SHA")

def extract_pr_body(commit_sha: str, pr_state: PrState) -> str:
prDetails = None
try:
prDetails = check_output(f'gh pr list --json body --state {pr_state.value} --search {commit_sha}', shell=True, text=True, stderr=STDOUT)
except CalledProcessError as e:
raise GhCliException(e.output)

prDetails = check_output(f'gh pr list --json body --state {pr_state.value} --search {commit_sha}', shell=True, text=True)
prDetailsJson = json.loads(prDetails)
if len(prDetailsJson) == 0:
raise PrNotFoundException(commit_sha)
Expand Down
35 changes: 16 additions & 19 deletions scripts/test_script.py
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))

0 comments on commit 236faaa

Please sign in to comment.