| 1 |
# -------------------------------------------------------------------------------------------- |
| 2 |
# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
# -------------------------------------------------------------------------------------------- |
| 5 |
|
| 6 |
from vsts.vss_connection import VssConnection |
| 7 |
|
| 8 |
class BaseManager(object): |
| 9 |
"""The basic manager which the other classes are build on |
| 10 |
|
| 11 |
Attributes: |
| 12 |
organization_name : The name of the DevOps organization |
| 13 |
project_name : The name of the DevOps project |
| 14 |
creds : These are credentials for an Azure user |
| 15 |
""" |
| 16 |
|
| 17 |
def __init__(self, creds, organization_name="", project_name="", repository_name="", pool_name=""): |
| 18 |
# Create the relevant name attributes |
| 19 |
self._organization_name = organization_name |
| 20 |
self._project_name = project_name |
| 21 |
self._creds = creds |
| 22 |
self._repository_name = repository_name |
| 23 |
self._pool_name = pool_name |
| 24 |
|
| 25 |
# Create the relevant clients that are needed by the managers |
| 26 |
self._connection = VssConnection(base_url='https://dev.azure.com/' + organization_name, creds=creds) |
| 27 |
self._agent_client = self._connection.get_client("vsts.task_agent.v4_1.task_agent_client.TaskAgentClient") |
| 28 |
self._build_client = self._connection.get_client('vsts.build.v4_1.build_client.BuildClient') |
| 29 |
self._core_client = self._connection.get_client('vsts.core.v4_0.core_client.CoreClient') |
| 30 |
self._extension_management_client = self._connection.get_client('vsts.extension_management.v4_1.extension_management_client.ExtensionManagementClient') # pylint: disable=line-too-long |
| 31 |
self._git_client = self._connection.get_client("vsts.git.v4_1.git_client.GitClient") |
| 32 |
self._release_client = self._connection.get_client('vsts.release.v4_1.release_client.ReleaseClient') |
| 33 |
self._service_endpoint_client = self._connection.get_client( |
| 34 |
'vsts.service_endpoint.v4_1.service_endpoint_client.ServiceEndpointClient' |
| 35 |
) |
| 36 |
|
| 37 |
def _get_project_by_name(self, project_name): |
| 38 |
"""Helper function to get the project object from its name""" |
| 39 |
projects = self._core_client.get_projects() |
| 40 |
return next((project for project in projects if project.name == project_name), None) |
| 41 |
|
| 42 |
def _get_repository_by_name(self, project, repository_name): |
| 43 |
"""Helper function to get the repository object from its name""" |
| 44 |
repositories = self._git_client.get_repositories(project.id) |
| 45 |
return next((repository for repository in repositories if repository.name == repository_name), None) |
| 46 |
|
| 47 |
def _get_definition_by_name(self, project, definition_name): |
| 48 |
"""Helper function to get definition object from its name""" |
| 49 |
definitions = self._build_client.get_definitions(project.id) |
| 50 |
return next((definition for definition in definitions if definition.name == definition_name), None) |
| 51 |
|
| 52 |
def _get_build_by_name(self, project, name): |
| 53 |
"""Helper function to get build object from its name""" |
| 54 |
builds_unsorted = self._build_client.get_builds(project=project.id) |
| 55 |
builds = sorted(builds_unsorted, key=lambda x: x.start_time, reverse=True) |
| 56 |
return next((build for build in builds if build.definition.name == name), None) |
| 57 |
|
| 58 |
def _get_github_repository_by_name(self, github_repository_name): |
| 59 |
"""Helper function to get a github repository object from its name""" |
| 60 |
service_endpoints = self._service_endpoint_client.get_service_endpoints(self._project_name, type="github") |
| 61 |
github_endpoint = service_endpoints[0] |
| 62 |
repositories = self._build_client.list_repositories( |
| 63 |
project=self._project_name, |
| 64 |
provider_name='github', |
| 65 |
service_endpoint_id=github_endpoint.id, |
| 66 |
repository=github_repository_name |
| 67 |
) |
| 68 |
repository_match = next(( |
| 69 |
repository for repository in repositories.repositories if repository.full_name == github_repository_name |
| 70 |
), None) |
| 71 |
return repository_match |