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 摘要。