【问题标题】:How to get Azure DevOps client using python script如何使用 python 脚本获取 Azure DevOps 客户端
【发布时间】:2020-02-13 14:01:01
【问题描述】:

我正在尝试使用 python 脚本获取 Azure DevOps 的客户端。 这是我为此目的使用的代码:

from azure.devops.connection import Connection
   git_client = p_context.connection.clients.get_git_client()

p_context 是我的 Azure DevOps 组织的访问令牌。

以下是我面临的错误:

Traceback (most recent call last):
  File "/home/vsts/work/1/s/IdentifyGitBuildCommitItems.py", line 95, in <module>
    t = get_commit_changes_for_build(context ,azdevops_projectid ,azdevops_build_id ,azdevops_build_number ,bld_commit.id ,azdevops_repository_name)
  File "/home/vsts/work/1/s/IdentifyGitBuildCommitItems.py", line 51, in get_commit_changes_for_build
    comt = git_client.get_changes(p_commit_id ,repository_id=p_repository_name ,project=p_project_id)
  File "/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/azure/devops/released/git/git_client_base.py", line 250, in get_changes
    query_parameters=query_parameters)
  File "/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/azure/devops/client.py", line 104, in _send
    response = self._send_request(request=request, headers=headers, content=content, media_type=media_type)
  File "/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/azure/devops/client.py", line 68, in _send_request
    self._handle_error(request, response)
  File "/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/azure/devops/client.py", line 295, in _handle_error
    status_code=response.status_code))
azure.devops.exceptions.AzureDevOpsClientRequestError: Page not found.  Operation returned a 404 status code.
##[error]/opt/hostedtoolcache/Python/3.6.10/x64/bin/python failed with return code: 1

这是我为我的用例关注的完整 GitHub 示例:https://github.com/venkatra/dbt_hacks/blob/PERSISTENT_TABLE_MATERIALIZATION/IdentifyGitBuildCommitItems.py

【问题讨论】:

    标签: python azure-devops devops


    【解决方案1】:

    您可以查看Azure DevOps Python API 并尝试以下代码:

    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 
    git_client = connection.clients.get_git_client()
    

    【讨论】:

      【解决方案2】:

      我已经找出问题并解决了错误。 由于我的源代码存在于我的个人 GitHub 帐户而不是 Azure Git 存储库中,因此提示错误。当我将源代码从个人 Git 移动到 Azure Git 存储库时,错误得到了解决。

      【讨论】:

      • 很高兴看到您的问题得到解决,您可以Accept it as an Answer,这可能对阅读此主题的其他社区成员有所帮助。
      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 2016-09-12
      • 2022-10-17
      • 2012-01-29
      • 2021-08-31
      • 2016-12-03
      • 2020-07-07
      • 2016-03-10
      相关资源
      最近更新 更多