microsoft / microsoft/azure-devops-python-api

azure.devops.exceptions.AzureDevOpsServiceError: The body of the request contains invalid Json. Parameter name: contentStream

未關閉
#448 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Python
星號
684
分支
218
平均合併
8 天 10 小時
30 天內合併 PR
1

描述

Background:

I'm using the azure-devops-python-api - library in order to programmatically push a commit to my Azure DevOps organization (git) repository.

The DevOps REST API docs for "Pushes - Create" did not help me to figure out the solution.

This similar discussion did also not resolve my problem.

Problem:

Bad request error (HTTP 400) when calling create_push() in module azure\devops\released\git\git_client_base.py.

I would like understand what exactly goes wrong here.
The error message is very generic, I could not determine which of the inputs are the problem.


Details:

In the following, I will post the whole code involved at the very bottom, but before I will detail the error traceback and the HTTP-request-details.

The entire exception traceback is:

Exception has occurred: AzureDevOpsServiceError       (note: full exception trace is shown but execution is paused at: <module>)

The body of the request contains invalid Json.
Parameter name: contentStream

  File "C:\Users\username\Projects\project\ics-venv\Lib\site-packages\azure\devops\client.py", line 270, in _handle_error
    raise AzureDevOpsServiceError(wrapped_exception)
  File "C:\Users\username\Projects\project\ics-venv\Lib\site-packages\azure\devops\client.py", line 68, in _send_request
    self._handle_error(request, response)
  File "C:\Users\username\Projects\project\ics-venv\Lib\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 "C:\Users\username\Projects\project\ics-venv\Lib\site-packages\azure\devops\released\git\git_client_base.py", line 1555, in create_push
    content=content)
  File "C:\Users\username\Projects\project\src\lambda_functions\json_automation\lambda_function.py", line 251, in create_feature_branch
    project=self._project_name,
  File "C:\Users\username\Projects\project\src\lambda_functions\json_automation\lambda_function.py", line 592, in lambda_handler
    create_branch_response = pr_handler.create_feature_branch()
  File "C:\Users\username\Projects\project\src\packages\ics_lambda\__init__.py", line 31, in run_local
    result = lambda_handler(event, context=context)
  File "C:\Users\username\Projects\project\src\lambda_functions\json_automation\lambda_function.py", line 615, in <module> (Current frame)
    ics_lambda.run_local(lambda_handler, event=test_event)
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.7.9\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.7.9\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
    
azure.devops.exceptions.AzureDevOpsServiceError: The body of the request contains invalid Json.
Parameter name: contentStream

The request.body is:

{
  "commits": [{ "commitId": "12312314123123123213123123123" }],
  "refUpdates": [
    {
      "name": "target-branch",
      "oldObjectId": "12312314123123123213123123123",
      "repositoryId": "project_x"
    }
  ],
  "repository": {
    "defaultBranch": "feature-branch",
    "parentRepository": {
      "name": "project_x",
      "project": { "id": "project_x", "name": "project_x" }
    },
    "project": { "id": "project_x", "name": "project_x" }
  }
}

The request headers are:

{
  "Content-Type": "application/json; charset=utf-8",
  "Accept": "application/json;api-version=5.1",
  "X-TFS-FedAuthRedirect": "Suppress",
  "X-VSS-ForceMsaPassThrough": "true",
  "X-TFS-Session": "..."
}

The request URL is:

https://dev.azure.com/organization/project_x/_apis/git/repositories/project_x/pushes

Source Code:

The code involved until invoking the git_client.create_push() - method, is:

from azure.devops.v5_1.git.models import (
    GitCommitRef,
    GitPush,
    GitRefUpdate,
    GitRepositoryRef,
    GitRepository,
    TeamProjectReference,
)
from azure.devops.connection import Connection


release_head_commit_id = '12312314123123123213123123123'
project_name = 'project_x'
repo_name = 'project_x'
release_branch_name = 'target-branch'
feature_branch_name = 'feature-branch'
git_client = Connection(base_url=organization_url, creds=credentials).clients.get_git_client()

project_reference = TeamProjectReference(
    abbreviation=None,
    default_team_image_url=None,
    description=None,
    id=project_name,
    last_update_time=None,
    name=project_name,
    revision=None,
    state=None,
    url=None,  # potentially pass the URL to the git repo
    visibility=None,
)

parent_repo = GitRepositoryRef(
    collection=None,
    id=None,
    is_fork=None,
    name=repo_name,
    project=project_reference,
    remote_url=None,
    ssh_url=None,
    url=None,
)

git_repo = GitRepository(
    _links=None,
    default_branch=release_branch_name,
    id=None,
    is_fork=None,
    name=None,
    parent_repository=parent_repo,
    project=project_reference,
    remote_url=None,
    size=None,
    ssh_url=None,
    url=None,
    valid_remote_urls=None,
    web_url=None,
)

commit_refs: List[GitCommitRef] = [
    GitCommitRef(
        _links=None,
        author=None,
        change_counts=None,
        changes=None,
        comment=None,
        comment_truncated=None,
        commit_id=release_head_commit_id,
        committer=None,
        parents=None,
        push=None,
        remote_url=None,
        statuses=None,
        url=None,
        work_items=None,
    ),
]

ref_updates: List[GitRefUpdate] = [
    GitRefUpdate(
        repository_id=repo_name,
        name=feature_branch_name,
        old_object_id=release_head_commit_id,
    ),
]

push_obj = GitPush(
    _links=None,
    date=None,
    push_correlation_id=None,
    pushed_by=None,
    push_id=None,
    url=None,
    commits=commit_refs,
    ref_updates=ref_updates,
    repository=git_repo,
)

git_client.create_push(
    push_obj,
    repo_name,
    project=project_name,
)

PS:
Since there is no easy-to-use documentation for the python API, I copied the __init__ - args for each class instantiation, where all args are set to None by default.
As it is not made clear whether or not they are required, I left them visible for the moment. But the plan is to remove them from the code once I figured out which of them I need to use.

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 azure/devops/released/git/git_client_base.py 中的 create_push() 開始,並將 issue 中顯示的序列化請求與 Azure DevOps REST API 的「Pushes - Create」文件進行比較。追蹤來自 lambda_function.py 的呼叫,並確定哪個請求輸入會產生無效 JSON 錯誤;當能夠對無效欄位或 payload 作出清楚且可重現的解釋時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
azure, git, python
領域
api, backend
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
需要釐清
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。