-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add commands to manage Git refs (#296)
* add commands to manage Git refs This changeset adds methods to manage Git refs: * list of references * create/update/delete a single reference * resolve pylint warnings * resolve flake8 warnings * remove transcient debugging code * add unit tests for the refs methods * adding content to _init_.py file * delete test/utilities/helper.py as python 2.7.15 doesn't seem to like it * missing .self * add ref commands to utCoverage.json * add vcs test * fix documentation issues * address review comments * renamed reposRefTest.py * defined more strict assert statements * fix Not None typo * allow large response * fix uppercase in format.py * Only add column is value exists * add recording * adapt to the new way to define organization * add lock/unlock method and update integration tests * remove unnecessary stop method * try to adapt the tests * Update test_ref.py * Update commands.py * Update test_reposRefTest.py * Update test_ref_createListUpdateDelete.yaml * Update test_ref.py Remove commented code
- Loading branch information
Showing
14 changed files
with
2,609 additions
and
4 deletions.
There are no files selected for viewing
Empty file.
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
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
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
from knack.log import get_logger | ||
from knack.util import CLIError | ||
from vsts.git.v4_0.models.git_ref_update import GitRefUpdate | ||
from vsts.exceptions import VstsServiceError | ||
from azext_devops.dev.common.git import resolve_git_refs | ||
from azext_devops.dev.common.services import (get_git_client, | ||
resolve_instance_project_and_repo) | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
# pylint: disable=redefined-builtin | ||
def list_refs(filter=None, repository=None, organization=None, project=None, detect=None): | ||
"""List the references. | ||
:param str filter: A filter to apply to the refs (starts with). Example: head or heads/ for the branches. | ||
:param str repository: Name or ID of the repository. | ||
:param str organization: Azure Devops organization URL. Example: https://dev.azure.com/MyOrganizationName/ | ||
:param str project: Name or ID of the project. | ||
:param str detect: Automatically detect organization and project. Default is "on". | ||
""" | ||
try: | ||
organization, project, repository = resolve_instance_project_and_repo( | ||
detect=detect, | ||
organization=organization, | ||
project=project, | ||
repo=repository) | ||
client = get_git_client(organization) | ||
return client.get_refs(repository_id=repository, | ||
project=project, | ||
filter=filter) | ||
except VstsServiceError as ex: | ||
raise CLIError(ex) | ||
|
||
|
||
def create_ref(name, object_id, repository=None, organization=None, project=None, detect=None): | ||
"""Create a reference. | ||
:param str name: Name of the reference to create (example: heads/my_branch or tags/my_tag). | ||
:param str object_id: Id of the object to create the reference from. | ||
:param str repository: Name or ID of the repository. | ||
:param str organization: Azure Devops organization URL. Example: https://dev.azure.com/MyOrganizationName/ | ||
:param str project: Name or ID of the project. | ||
:param str detect: Automatically detect organization and project. Default is "on". | ||
""" | ||
try: | ||
organization, project, repository = resolve_instance_project_and_repo( | ||
detect=detect, | ||
organization=organization, | ||
project=project, | ||
repo=repository) | ||
client = get_git_client(organization) | ||
# by default, the create method does not support setting the is_locked value | ||
# to True. | ||
ref_update = GitRefUpdate(is_locked=False, | ||
name=resolve_git_refs(name), | ||
new_object_id=object_id, | ||
old_object_id='0000000000000000000000000000000000000000') | ||
return client.update_refs(ref_updates=[ref_update], | ||
repository_id=repository, | ||
project=project)[0] | ||
except VstsServiceError as ex: | ||
raise CLIError(ex) | ||
|
||
|
||
def delete_ref(name, object_id, 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. | ||
:param str repository: Name or ID of the repository. | ||
:param str organization: Azure Devops organization URL. Example: https://dev.azure.com/MyOrganizationName/ | ||
:param str project: Name or ID of the project. | ||
:param str detect: Automatically detect organization and project. Default is "on". | ||
""" | ||
try: | ||
organization, project, repository = resolve_instance_project_and_repo( | ||
detect=detect, | ||
organization=organization, | ||
project=project, | ||
repo=repository) | ||
client = get_git_client(organization) | ||
ref_update = GitRefUpdate(name=resolve_git_refs(name), | ||
new_object_id='0000000000000000000000000000000000000000', | ||
old_object_id=object_id) | ||
return client.update_refs(ref_updates=[ref_update], | ||
repository_id=repository, | ||
project=project)[0] | ||
except VstsServiceError as ex: | ||
raise CLIError(ex) | ||
|
||
|
||
def lock_ref(name, repository=None, organization=None, project=None, detect=None): | ||
"""Lock a reference. | ||
:param str name: Name of the reference to update (example: heads/my_branch). | ||
:param str repository: Name or ID of the repository. | ||
:param str organization: Azure Devops organization URL. Example: https://dev.azure.com/MyOrganizationName/ | ||
:param str project: Name or ID of the project. | ||
:param str detect: Automatically detect organization and project. Default is "on". | ||
""" | ||
return _update_ref(name, True, repository, organization, project, detect) | ||
|
||
|
||
def unlock_ref(name, repository=None, organization=None, project=None, detect=None): | ||
"""Unlock a reference. | ||
:param str name: Name of the reference to update (example: heads/my_branch). | ||
:param str repository: Name or ID of the repository. | ||
:param str organization: Azure Devops organization URL. Example: https://dev.azure.com/MyOrganizationName/ | ||
:param str project: Name or ID of the project. | ||
:param str detect: Automatically detect organization and project. Default is "on". | ||
""" | ||
return _update_ref(name, False, repository, organization, project, detect) | ||
|
||
|
||
def _update_ref(name, locked, repository, organization, project, detect): | ||
try: | ||
organization, project, repository = resolve_instance_project_and_repo( | ||
detect=detect, | ||
organization=organization, | ||
project=project, | ||
repo=repository) | ||
client = get_git_client(organization) | ||
ref_update = GitRefUpdate(is_locked=locked) | ||
return client.update_ref(new_ref_info=ref_update, | ||
repository_id=repository, | ||
filter=name, | ||
project=project) | ||
except VstsServiceError as ex: | ||
raise CLIError(ex) |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
import unittest | ||
|
||
try: | ||
# Attempt to load mock (works on Python 3.3 and above) | ||
from unittest.mock import patch, ANY | ||
except ImportError: | ||
# Attempt to load mock (works on Python version below 3.3) | ||
from mock import patch, ANY | ||
|
||
from azext_devops.vstsCompressed.git.v4_0.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.helper import get_client_mock_helper, TEST_DEVOPS_ORG_URL | ||
|
||
|
||
class TestRefMethods(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.get_refs_patcher = patch('azext_devops.vstsCompressed.git.v4_0.git_client.GitClient.get_refs') | ||
self.update_ref_patcher = patch('azext_devops.vstsCompressed.git.v4_0.git_client.GitClient.update_ref') | ||
self.update_refs_patcher = patch('azext_devops.vstsCompressed.git.v4_0.git_client.GitClient.update_refs') | ||
|
||
# patch get client so no network call is made | ||
self.get_client = patch('azext_devops.vstsCompressed.vss_connection.VssConnection.get_client', new=get_client_mock_helper) | ||
self.resolve_identity_patcher = patch('azext_devops.dev.common.identities.resolve_identity_as_id') | ||
self.get_credential_patcher = patch('azext_devops.dev.common.services.get_credential') | ||
self.validate_token_patcher = patch('azext_devops.dev.common.services.validate_token_for_instance') | ||
|
||
# start the patchers | ||
self.mock_resolve_identity = self.resolve_identity_patcher.start() | ||
self.mock_get_credential = self.get_credential_patcher.start() | ||
self.mock_validate_token = self.validate_token_patcher.start() | ||
|
||
self.mock_get_refs = self.get_refs_patcher.start() | ||
self.mock_update_ref = self.update_ref_patcher.start() | ||
self.mock_update_refs = self.update_refs_patcher.start() | ||
|
||
# Setup mocks for clients | ||
self.mock_get_client = self.get_client.start() | ||
|
||
# clear connection cache before running each test | ||
clear_connection_cache() | ||
|
||
def tearDown(self): | ||
self.mock_get_refs.stop() | ||
self.mock_update_ref.stop() | ||
self.mock_update_refs.stop() | ||
|
||
def test_list_refs(self): | ||
# set return values | ||
self.mock_validate_token.return_value = True | ||
|
||
response = list_refs(organization=TEST_DEVOPS_ORG_URL, | ||
project='sample_project', | ||
detect='off') | ||
self.mock_get_refs.assert_called_once() | ||
self.mock_get_refs.assert_called_once_with(filter=None, | ||
project='sample_project', | ||
repository_id=None) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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
Oops, something went wrong.