Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from azure.cli.core.azclierror import ClientRequestError, RequiredArgumentMissingError
from knack.log import get_logger
from ..utils._flexible_server_util import (
resolve_executable,
run_cmd,
run_subprocess,
fill_action_template,
Expand All @@ -29,7 +30,8 @@ def github_actions_setup(cmd, client, resource_group_name, server_name, database
raise RequiredArgumentMissingError("Provide remote branch name to allow pushing the action file to your remote branch.")
if action_name is None:
action_name = server.name + '_' + database_name + "_deploy"
gitcli_check_and_login()
gh_path = gitcli_check_and_login()
git_path = resolve_executable("git")

database_engine = 'postgresql'

Expand All @@ -41,35 +43,39 @@ def github_actions_setup(cmd, client, resource_group_name, server_name, database
administrator_login_password=administrator_login_password,
file_name=sql_file_path,
repository=repository,
action_name=action_name)
action_name=action_name,
gh_path=gh_path,
git_path=git_path)

action_path = get_git_root_dir() + GITHUB_ACTION_PATH + action_name + '.yml'
action_path = get_git_root_dir(git_path) + GITHUB_ACTION_PATH + action_name + '.yml'
logger.warning("Making git commit for file %s", action_path)
run_subprocess(["git", "add", action_path])
run_subprocess(["git", "commit", "-m", "Add github action file"])
run_subprocess([git_path, "add", action_path])
run_subprocess([git_path, "commit", "-m", "Add github action file"])

if allow_push:
logger.warning("Pushing the created action file to origin %s branch", branch)
run_subprocess(["git", "push", "origin", branch])
run_subprocess([git_path, "push", "origin", branch])
else:
logger.warning('You did not set --allow-push parameter. Please push the prepared file %s to your remote repo and run "deploy run" command to activate the workflow.', action_path)


def github_actions_run(action_name, branch):

gitcli_check_and_login()
gh_path = gitcli_check_and_login()
logger.warning("Created an event for %s.yml in branch %s", action_name, branch)
run_subprocess(["gh", "workflow", "run", action_name + ".yml", "--ref", branch])
run_subprocess([gh_path, "workflow", "run", action_name + ".yml", "--ref", branch])


def gitcli_check_and_login():
try:
output = run_cmd(["gh"], capture_output=True)
gh_path = resolve_executable("gh")
output = run_cmd([gh_path], capture_output=True)
except FileNotFoundError as ex:
raise ClientRequestError('Please install "Github CLI" to run this command.') from ex
if output.returncode:
raise ClientRequestError('Please install "Github CLI" to run this command.')

output = run_cmd(["gh", "auth", "status"], capture_output=True)
output = run_cmd([gh_path, "auth", "status"], capture_output=True)
if output.returncode:
run_subprocess(["gh", "auth", "login"], stdout_show=True)
run_subprocess([gh_path, "auth", "login"], stdout_show=True)
return gh_path
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,25 @@ def test_subprocess_preserves_arguments_in_each_output_mode(self):

expected = [executable, *original[1:]]
if stdout_show:
popen.assert_called_once_with(expected)
popen.assert_called_once_with(expected, shell=False)
else:
popen.assert_called_once_with(expected, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
popen.assert_called_once_with(
expected, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
process.wait.assert_called_once_with()
self.assertEqual(arguments, original)

def test_unix_and_explicit_paths_keep_existing_lookup(self):
with mock.patch.object(util.sys, 'platform', 'linux'), \
def test_unix_lookup_uses_which_for_names_and_explicit_paths(self):
executable = os.path.abspath(os.path.join('installed tools', 'gh'))
with mock.patch.object(util, 'is_windows', return_value=False), \
mock.patch('shutil.which', return_value=executable) as which, \
mock.patch.object(util, 'get_safe_executable_path') as find:
self.assertEqual(util.resolve_executable('gh'), 'gh')
find.assert_not_called()

with mock.patch.object(util.sys, 'platform', 'win32'), \
mock.patch.object(util, 'get_safe_executable_path') as find:
for executable in (os.path.abspath(os.path.join('installed tools', 'gh.exe')),
os.path.join('.', 'gh.exe'), os.path.join('tools', 'gh.exe')):
with self.subTest(executable=executable):
self.assertEqual(util.resolve_executable(executable), executable)
for argument in ('gh', executable, os.path.join('.', 'gh'), os.path.join('tools', 'gh')):
with self.subTest(argument=argument):
self.assertEqual(util.resolve_executable(argument), executable)
which.assert_called_with(argument)
which.return_value = None
with self.assertRaises(FileNotFoundError):
util.resolve_executable('gh')
find.assert_not_called()


Expand All @@ -75,6 +76,7 @@ def setUp(self):
self.addCleanup(temporary.cleanup)
self.project = temporary.name
self.executable = os.path.join(self.project, 'installed tools', 'gh.exe')
self.git_executable = os.path.join(self.project, 'installed tools', 'git.exe')
self.workflow = self.project + util.GITHUB_ACTION_PATH + 'deploy database.yml'
server = SimpleNamespace(
id='/subscriptions/test-subscription/resourceGroups/test-rg/'
Expand All @@ -99,7 +101,8 @@ def setUp(self):
self.upload = mock.Mock(return_value=subprocess.CompletedProcess([], 0, stdout=b'', stderr=b''))
self.git = mock.Mock()
patches = (
(deploy, 'gitcli_check_and_login', mock.Mock()),
(deploy, 'gitcli_check_and_login', mock.Mock(return_value=self.executable)),
(deploy, 'resolve_executable', mock.Mock(return_value=self.git_executable)),
(deploy, 'get_git_root_dir', mock.Mock(return_value=self.project)),
(deploy, 'run_subprocess', self.git),
(util, 'get_git_root_dir', mock.Mock(return_value=self.project)),
Expand Down Expand Up @@ -133,9 +136,9 @@ def test_setup_uploads_missing_credentials_through_stdin(self):
self.assertEqual(json.loads(self.upload.call_args.kwargs['input']), self.credentials)
self.files.assert_any_call(self.workflow, 'w', encoding='utf8')
self.assertEqual(self.git.call_args_list, [
mock.call(['git', 'add', self.workflow]),
mock.call(['git', 'commit', '-m', 'Add github action file']),
mock.call(['git', 'push', 'origin', 'feature/database']),
mock.call([self.git_executable, 'add', self.workflow]),
mock.call([self.git_executable, 'commit', '-m', 'Add github action file']),
mock.call([self.git_executable, 'push', 'origin', 'feature/database']),
])

def test_setup_stops_after_credential_upload_failure(self):
Expand All @@ -159,6 +162,9 @@ def test_setup_stops_after_credential_upload_failure(self):
class WindowsDeploymentToolsTest(unittest.TestCase):

def setUp(self):
platform = mock.patch.object(util, 'is_windows', return_value=True)
platform.start()
self.addCleanup(platform.stop)
temporary = tempfile.TemporaryDirectory()
self.addCleanup(temporary.cleanup)
self.project = os.path.join(temporary.name, 'project with spaces')
Expand Down Expand Up @@ -196,7 +202,7 @@ def test_workflow_run_resolves_every_launch(self):
])
popen.assert_called_once_with(
[self.gh, 'workflow', 'run', 'deploy database.yml', '--ref', 'feature/database'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)

def test_setup_resolves_git_and_preserves_paths(self):
client = mock.Mock()
Expand All @@ -217,13 +223,14 @@ def test_setup_resolves_git_and_preserves_paths(self):

action_path = self.project + util.GITHUB_ACTION_PATH + 'deploy database.yml'
expected = [
mock.call([self.git, 'add', action_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE),
mock.call([self.git, 'add', action_path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False),
mock.call([self.git, 'commit', '-m', 'Add github action file'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE),
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False),
]
if allow_push:
expected.append(mock.call([self.git, 'push', 'origin', 'feature/database'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE))
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False))
self.assertEqual(popen.call_args_list, expected)

def test_interactive_login_uses_resolved_tool(self):
Expand All @@ -232,7 +239,7 @@ def test_interactive_login_uses_resolved_tool(self):
mock.patch.object(util.subprocess, 'Popen',
return_value=mock.MagicMock(returncode=0)) as popen:
deploy.gitcli_check_and_login()
popen.assert_called_once_with([self.gh, 'auth', 'login'])
popen.assert_called_once_with([self.gh, 'auth', 'login'], shell=False)

def test_git_root_and_secret_queries_use_resolved_tools(self):
completed = subprocess.CompletedProcess([], 0, stdout=self.project.encode('utf8'))
Expand Down
Loading
Loading