| 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 |
import time |
| 7 |
import logging |
| 8 |
from msrest.service_client import ServiceClient |
| 9 |
from msrest import Configuration, Deserializer |
| 10 |
from msrest.exceptions import HttpOperationError |
| 11 |
from vsts.exceptions import VstsServiceError |
| 12 |
import vsts.core.v4_1.models.team_project as team_project |
| 13 |
from ..user.user_manager import UserManager |
| 14 |
from ..base.base_manager import BaseManager |
| 15 |
from . import models |
| 16 |
|
| 17 |
|
| 18 |
class ProjectManager(BaseManager): |
| 19 |
""" Manage DevOps projects |
| 20 |
|
| 21 |
Create or list existing projects |
| 22 |
|
| 23 |
Attributes: |
| 24 |
config: url configuration |
| 25 |
client: authentication client |
| 26 |
dserialize: deserializer to process http responses into python classes |
| 27 |
Otherwise see BaseManager |
| 28 |
""" |
| 29 |
|
| 30 |
def __init__(self, base_url='https://{}.visualstudio.com', organization_name="", creds=None, |
| 31 |
create_project_url='https://dev.azure.com'): |
| 32 |
"""Inits Project as per BaseManager and adds relevant other needed fields""" |
| 33 |
super(ProjectManager, self).__init__(creds, organization_name=organization_name) |
| 34 |
base_url = base_url.format(organization_name) |
| 35 |
self._config = Configuration(base_url=base_url) |
| 36 |
self._client = ServiceClient(creds, self._config) |
| 37 |
self._credentials = creds |
| 38 |
# Need to make a secondary client for the creating project as it uses a different base url |
| 39 |
self._create_project_config = Configuration(base_url=create_project_url) |
| 40 |
self._create_project_client = ServiceClient(creds, self._create_project_config) |
| 41 |
client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)} |
| 42 |
self._deserialize = Deserializer(client_models) |
| 43 |
self._user_mgr = UserManager(creds=self._creds) |
| 44 |
|
| 45 |
def create_project(self, projectName): |
| 46 |
"""Create a new project for an organization""" |
| 47 |
try: |
| 48 |
capabilities = dict() |
| 49 |
capabilities['versioncontrol'] = {"sourceControlType": "Git"} |
| 50 |
capabilities['processTemplate'] = {"templateTypeId": "adcc42ab-9882-485e-a3ed-7678f01f66bc"} |
| 51 |
project = team_project.TeamProject( |
| 52 |
description="Azure Functions Devops Build created project", |
| 53 |
name=projectName, |
| 54 |
visibility=0, |
| 55 |
capabilities=capabilities |
| 56 |
) |
| 57 |
queue_object = self._core_client.queue_create_project(project) |
| 58 |
queue_id = queue_object.id |
| 59 |
self._poll_project(queue_id) |
| 60 |
# Sleep as there is normally a gap between project finishing being made and when we can retrieve it |
| 61 |
time.sleep(1) |
| 62 |
project = self._get_project_by_name(projectName) |
| 63 |
project.valid = True |
| 64 |
return project |
| 65 |
except VstsServiceError as e: |
| 66 |
return models.ProjectFailed(e) |
| 67 |
|
| 68 |
def list_projects(self): |
| 69 |
"""Lists the current projects within an organization""" |
| 70 |
url = '/_apis/projects' |
| 71 |
|
| 72 |
# First pass without X-VSS-ForceMsaPassThrough header |
| 73 |
response = self._list_projects_request(url) |
| 74 |
|
| 75 |
deserialized = None |
| 76 |
if response.status_code // 100 != 2: |
| 77 |
logging.error("GET %s", response.url) |
| 78 |
logging.error("response: %s", response.status_code) |
| 79 |
logging.error(response.text) |
| 80 |
raise HttpOperationError(self._deserialize, response) |
| 81 |
else: |
| 82 |
deserialized = self._deserialize('Projects', response) |
| 83 |
|
| 84 |
return deserialized |
| 85 |
|
| 86 |
def _list_projects_request(self, url): |
| 87 |
query_paramters = {} |
| 88 |
query_paramters['includeCapabilities'] = 'true' |
| 89 |
|
| 90 |
header_paramters = {} |
| 91 |
if self._user_mgr.is_msa_account(): |
| 92 |
header_paramters['X-VSS-ForceMsaPassThrough'] = 'true' |
| 93 |
|
| 94 |
request = self._client.get(url, params=query_paramters) |
| 95 |
response = self._client.send(request, headers=header_paramters) |
| 96 |
return response |
| 97 |
|
| 98 |
def _poll_project(self, project_id): |
| 99 |
"""Helper function to poll the project""" |
| 100 |
project_created = False |
| 101 |
while not project_created: |
| 102 |
time.sleep(1) |
| 103 |
res = self._is_project_created(project_id) |
| 104 |
logging.info('project creation is: %s', res.status) |
| 105 |
if res.status == 'succeeded': |
| 106 |
project_created = True |
| 107 |
|
| 108 |
def _is_project_created(self, project_id): |
| 109 |
"""Helper function to see the status of a project""" |
| 110 |
url = '/' + self._organization_name + '/_apis/operations/' + project_id |
| 111 |
query_paramters = {} |
| 112 |
|
| 113 |
header_paramters = {} |
| 114 |
header_paramters['Accept'] = 'application/json' |
| 115 |
if self._user_mgr.is_msa_account(): |
| 116 |
header_paramters['X-VSS-ForceMsaPassThrough'] = 'true' |
| 117 |
|
| 118 |
request = self._create_project_client.get(url, params=query_paramters) |
| 119 |
response = self._create_project_client.send(request, headers=header_paramters) |
| 120 |
|
| 121 |
# Handle Response |
| 122 |
deserialized = None |
| 123 |
if response.status_code // 100 != 2: |
| 124 |
logging.error("GET %s", request.url) |
| 125 |
logging.error("response: %s", response.status_code) |
| 126 |
logging.error(response.text) |
| 127 |
raise HttpOperationError(self._deserialize, response) |
| 128 |
else: |
| 129 |
deserialized = self._deserialize('ProjectPoll', response) |
| 130 |
|
| 131 |
return deserialized |