Skip to content

Instantly share code, notes, and snippets.

@FilBot3
Created July 16, 2020 00:35
Show Gist options
  • Select an option

  • Save FilBot3/99fc931aff109b6c91505cd3dce7129b to your computer and use it in GitHub Desktop.

Select an option

Save FilBot3/99fc931aff109b6c91505cd3dce7129b to your computer and use it in GitHub Desktop.
An example of how to use the Azure DevOps Python API Client.
"""List and Sort Groups in Azure DevOps Project
"""
import configparser
import logging
import os
import pprint as pp
# import json
from azure.devops.connection import Connection # pylint: disable=import-error
from msrest.authentication import BasicAuthentication # pylint: disable=import-error
def login_bits() -> Connection:
"""Just does login precursor bits.
"""
if os.environ.get('AZDO_LOG'):
logging.basicConfig(level=logging.DEBUG)
if 'AZDO_PAT' in os.environ and 'AZDO_ORG' in os.environ:
azdo_pat = os.environ.get('AZDO_PAT')
ado_org_url = os.environ.get('AZDO_ORG')
else:
logging.info('Attempting to read ${HOME}/.local/azdo_config.ini')
azdo_config = configparser.ConfigParser()
azdo_config.read(os.environ.get('HOME') + '/.local/azdo_config.ini')
azdo_pat = azdo_config['auth']['pat']
ado_org_url = azdo_config['org']['name']
credentials = BasicAuthentication('', azdo_pat)
connection = Connection(base_url=ado_org_url, creds=credentials)
return connection
def main():
"""Main
"""
# Create a Core Client. This gives us access to the Projects API.
core_client = login_bits().clients_v5_1.get_core_client()
# We'll get the Project's information.
project_info = core_client.get_project(project_id='dudleyp',
include_capabilities=True)
# Create a Graph Client.
graph_client = login_bits().clients_v5_1.get_graph_client()
# Get the descriptor for the project, since it's not in the actual Project
# information originally retrieved.
project_response = graph_client.get_descriptor(storage_key=project_info.id)
# Get a list of all our groups for this Project we got the descriptor for.
graph_response = graph_client.list_groups(
scope_descriptor=project_response.value)
# print it.
pp.pprint(graph_response.graph_groups[0].display_name)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment