Fixes to list organizations as requested by Azure Devops

This commit is contained in:
dolko 2019-01-22 13:43:17 -08:00
Родитель eae9401b78
Коммит 15e71bf798
6 изменённых файлов: 44 добавлений и 12 удалений

Просмотреть файл

@ -13,4 +13,4 @@ class Organizations(Model):
def __init__(self, count=None, value=None):
self.count = count
self.value = value
self.value = value

Просмотреть файл

@ -9,6 +9,7 @@ from msrest.service_client import ServiceClient
from msrest import Configuration, Deserializer
from msrest.exceptions import HttpOperationError
from azure_devops_build_manager.user.user_manager import UserManager
from . import models
@ -26,6 +27,7 @@ class OrganizationManager():
def __init__(self, base_url='https://app.vssps.visualstudio.com', creds=None,
create_organization_url='https://app.vsaex.visualstudio.com'):
"""Inits OrganizationManager"""
self._creds = creds
self._config = Configuration(base_url=base_url)
self._client = ServiceClient(creds, self._config)
#need to make a secondary client for the creating organization as it uses a different base url
@ -66,8 +68,36 @@ class OrganizationManager():
return deserialized
def list_organizations(self, member_id):
def list_organizations(self):
"""List what organizations this user is part of"""
user_manager = UserManager(creds=self._creds)
user_id_aad = user_manager.get_user_id(msa=False)
user_id_msa = user_manager.get_user_id(msa=True)
if (user_id_aad.id == user_id_msa.id):
# Only need to do the one request as ids are the same
organizations = self._list_organizations_request(user_id_aad.id)
else:
def uniq(lst):
last = object()
for item in lst:
if item == last:
continue
yield item
last = item
# Need to do a request for each of the ids and then combine them
organizations_aad = self._list_organizations_request(user_id_aad.id)
organizations_msa = self._list_organizations_request(user_id_msa.id)
organizations = organizations_aad
# Now we just want to take the set of these two lists!
organizations.value = list(uniq(organizations_aad.value + organizations_msa.value))
organizations.count = len(organizations.value)
return organizations
def _list_organizations_request(self, member_id):
url = '/_apis/Commerce/Subscription'
query_paramters = {}

Просмотреть файл

@ -19,4 +19,4 @@ class User(Model):
self.displayName = displayName
self.uniqueName = uniqueName
self.email = email
self.preferredTimeZoneOffset = preferredTimeZoneOffset
self.preferredTimeZoneOffset = preferredTimeZoneOffset

Просмотреть файл

@ -25,9 +25,12 @@ class UserManager(object):
client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)}
self._deserialize = Deserializer(client_models)
def get_user_id(self):
def get_user_id(self, msa=False):
"""Get the user id"""
header_parameters = {}
if msa:
header_parameters['X-VSS-ForceMsaPassThrough'] = 'true'
header_parameters['Accept'] = 'application/json'
request = self._client.get('/_apis/AzureTfs/UserContext')
response = self._client.send(request, header_parameters)

Просмотреть файл

@ -2,13 +2,13 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from azure_devops_build_manager.constants import LINUX_CONSUMPTION, LINUX_DEDICATED, WINDOWS, NODE, PYTHON, JAVA, NET
from azure_devops_build_manager.constants import LINUX_CONSUMPTION, LINUX_DEDICATED, WINDOWS, NODE, PYTHON, JAVA, DOTNET
# You need to fill in the variables with names of the resources you already have
# When you are finished setting the configs then you can run test.cmd
# The create devops objects setting sets whether the test will run create commands. The default is false
CREATE_DEVOPS_OBJECTS = True
CREATE_DEVOPS_OBJECTS = False
# Specify the name of your already created devops objects
ORGANIZATION_NAME = 'dolk-automated-11'

Просмотреть файл

@ -56,13 +56,12 @@ class TestOrganizationManager(unittest.TestCase):
def test_list_organizations(self):
creds = get_credentials()
organization_manager = OrganizationManager(creds=creds)
user_manager = UserManager(creds=creds)
userid = user_manager.get_user_id()
self.assertRegex(userid.id, r"^[0-9A-Za-z-]+$")
organizations = organization_manager.list_organizations(userid.id)
organizations = organization_manager.list_organizations()
self.assertTrue(len(organizations.value), organizations.count)
found_organization = next((organization for organization in organizations.value if organization.accountName == ORGANIZATION_NAME), None)
self.assertTrue(found_organization != None)
for val in (organizations.value):
print(val.accountName)
#found_organization = next((organization for organization in organizations.value if organization.accountName == ORGANIZATION_NAME), None)
#self.assertTrue(found_organization != None)
if __name__ == '__main__':
unittest.main()