1

I made a Python script that clone an entire database repository from Azure Devops to compare files. The repo is still small but it is definitely not staying that way.

To solve this issue, I am trying to write a script that only download an item from the repo. The thing is that the API is quite a mouthful and I have trouble getting into it. For now, authentication and getting repos is an OK task but getting a specific item proved to be harder. I still want to use the Python wrapper.

Can someone help me with this issue or redirecting me to some useful links ? (except the API user manual).

1

2 Answers 2

1

Thank you vshandra. Posting your suggestions as an answer to help other community members.

The Python API contains repository for interacting with Azure DevOps. The API's will power the Azure DevOps extension for Azure CLI.

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
import pprint

# Fill in with your personal access token and org URL
personal_access_token = 'YOURPAT'
organization_url = 'https://dev.azure.com/YOURORG'

# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# Get a client (the "core" client provides access to projects, teams, etc)
core_client = connection.clients.get_core_client()

# Get the first page of projects
get_projects_response = core_client.get_projects()
index = 0
while get_projects_response is not None:
    for project in get_projects_response.value:
        pprint.pprint("[" + str(index) + "] " + project.name)
        index += 1
    if get_projects_response.continuation_token is not None and get_projects_response.continuation_token != "":
        # Get the next page of projects
        get_projects_response = core_client.get_projects(continuation_token=get_projects_response.continuation_token)
    else:
        # All projects have been retrieved
        get_projects_response = None

For complete information check Azure DevOps Python API.

Also to Use Personal access tokens in establishing the connection check Access tokens.

Sign up to request clarification or add additional context in comments.

3 Comments

How do you get the work items of the project using this? These docs just talk about getting the project names. Where can I see the other methods?
@AbhishekRai I found some docs about the work items in the github link that is in the answer: learn.microsoft.com/en-us/rest/api/azure/devops/wit/…
@6c746278 I finished this long time ago. dev.azure.com/Orgname/_apis/projects?api-version=5.1", headers=headers).json()
0

There's quite few reference for Azure devops python code. Maybe people who use Azure devops don't use python.

Microsoft's API based on REST API. The sample code provided by Microsoft is very simple.

Following is my sample code to download a path as zip file:

from msrest.authentication import BasicAuthentication
from azure.devops.connection import Connection

collectionUri = "https://dev.azure.com/"
projectName = "project"
repoName = "repo"
pat = "xxxxxx"

credentials = BasicAuthentication("", pat)
connection = Connection(base_url=collectionUri, creds=credentials)
git_client = connection.clients.get_git_client()
zip = git_client.get_item_zip(repoName, "/path/to/download/", projectName)
with open(repoName + ".zip", "wb") as f:
    for it in zip:
        f.write(it)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.