microsoft / microsoft/azure-devops-python-api

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

Offen
#448 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
684
Forks
218
Ø Merge
8 T. 10 Std.
Gemergte PRs (30 T.)
1

Beschreibung

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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit create_push() in azure/devops/released/git/git_client_base.py und vergleiche die im Issue gezeigte serialisierte Anfrage mit der Dokumentation der Azure DevOps REST API zu „Pushes - Create“. Verfolge den Aufruf aus lambda_function.py und ermittle, welche Anfrageeingabe den Fehler wegen ungültigem JSON verursacht; abgeschlossen ist die Untersuchung, wenn eine klare, reproduzierbare Erklärung des ungültigen Felds oder Payloads vorliegt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
azure, git, python
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.