Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Made object id optional #1166

Merged
merged 7 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 11 additions & 1 deletion azure-devops/azext_devops/dev/repos/ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# --------------------------------------------------------------------------------------------

from knack.log import get_logger
from knack.util import CLIError
from azext_devops.devops_sdk.v5_0.git.models import GitRefUpdate
from azext_devops.dev.common.git import resolve_git_refs
from azext_devops.dev.common.services import (get_git_client,
Expand Down Expand Up @@ -56,7 +57,7 @@ def create_ref(name, object_id, repository=None, organization=None, project=None
project=project)[0]


def delete_ref(name, object_id, repository=None, organization=None, project=None, detect=None):
def delete_ref(name, object_id=None, repository=None, organization=None, project=None, detect=None):
"""Delete a reference.
:param str name: Name of the reference to delete (example: heads/my_branch).
:param str object_id: Id of the reference to delete.
Expand All @@ -70,6 +71,15 @@ def delete_ref(name, object_id, repository=None, organization=None, project=None
project=project,
repo=repository)
client = get_git_client(organization)

if object_id is None:
ref = client.get_refs(repository_id=repository, project=project, filter=name)
if not ref or len(ref) != 1:
logger.error('ref not found')
raise CLIError("Failed to find object_id for ref " + name + ". Please provide object_id.")

object_id = ref[0].object_id

ref_update = GitRefUpdate(name=resolve_git_refs(name),
new_object_id='0000000000000000000000000000000000000000',
old_object_id=object_id)
Expand Down
30 changes: 30 additions & 0 deletions azure-devops/azext_devops/test/repos/test_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
# Attempt to load mock (works on Python version below 3.3)
from mock import patch, ANY

from knack.util import CLIError

from azext_devops.devops_sdk.v5_0.git.git_client import GitClient
from azext_devops.dev.common.services import clear_connection_cache
from azext_devops.dev.repos.ref import (list_refs, create_ref, delete_ref, lock_ref, unlock_ref)
from azext_devops.test.utils.authentication import AuthenticatedTests
from azext_devops.test.utils.helper import get_client_mock_helper, TEST_DEVOPS_ORG_URL

class MockRef(object):
def __init__(self, object_id):
self.object_id = object_id

class TestRefMethods(AuthenticatedTests):

Expand Down Expand Up @@ -95,6 +100,31 @@ def test_delete_ref(self):
ref_updates=ANY,
repository_id=None)

def test_delete_ref_without_obj_id(self):

refs = []
refs.append(MockRef("0"))

self.mock_get_refs.return_value = refs;

response = delete_ref(name='sample_ref',
organization=TEST_DEVOPS_ORG_URL,
project='sample_project')
# assert
self.mock_update_refs.assert_called_once_with(project='sample_project',
ref_updates=ANY,
repository_id=None)

def test_delete_ref_without_obj_id_invalid_ref_name(self):
sample_invalid_ref = "sample_invalid_ref"
try:
response = delete_ref(name=sample_invalid_ref,
organization=TEST_DEVOPS_ORG_URL,
project='sample_project')
self.fail('we should have received an error')
except CLIError as ex:
self.assertEqual(str(ex), f'Failed to find object_id for ref {sample_invalid_ref}. Please provide object_id.')


if __name__ == '__main__':
unittest.main()
Loading