如何使用Python从Azure Devops获取项目

问题描述 投票:0回答:2

我制作了一个 python 脚本,可以从 Azure Devops 克隆整个数据库存储库来比较文件。回购协议仍然很小,但绝对不会保持这种状态。

为了解决这个问题,我正在尝试编写一个仅从存储库下载项目的脚本。问题是 API 相当拗口,我很难理解它。目前,身份验证和获取存储库是一项不错的任务,但事实证明获取特定项目更加困难。我仍然想使用 python 包装器。

有人可以帮助我解决这个问题或将我重定向到一些有用的链接吗? (API使用手册除外)。

python api azure-devops
2个回答
1
投票

谢谢你vshandra。将您的建议作为答案发布以帮助其他社区成员。

Python API 包含用于与 Azure DevOps 交互的存储库。该 API 将为 Azure CLI 的 Azure DevOps 扩展提供支持。

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

有关完整信息,请检查 Azure DevOps Python API

还要在建立连接时使用个人访问令牌检查访问令牌


0
投票

Azure DevOps Python 代码的参考资料相当少。也许使用 Azure DevOps 的人不使用 Python。

Microsoft 基于 REST API 提供了 API https://github.com/microsoft/azure-devops-python-api。微软提供的示例代码https://github.com/Microsoft/azure-devops-python-samples非常简单。

以下是我下载 zip 文件路径的示例代码:

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

collectionUri = "https://dev.azure.com/"
projectName = "test"
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)
© www.soinside.com 2019 - 2024. All rights reserved.