Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create_Attachment doesn't seem to work #322

Open
ghost opened this issue Mar 30, 2020 · 1 comment
Open

Create_Attachment doesn't seem to work #322

ghost opened this issue Mar 30, 2020 · 1 comment

Comments

@ghost
Copy link

ghost commented Mar 30, 2020

The python create_attachment call in the git client doesn't seem to work correctly. If you use it as currently implemented you get a 'azure.devops.exceptions.AzureDevOpsServiceError: Nullable object must have a value' exception.

I saw the example as part of issue 129: #129 but that throws the same exception.

If I instead change the create_attachment call to:

def create_attachment(self, upload_stream, file_name, repository_id, pull_request_id, project=None, **kwargs):
        """CreateAttachment.
        [Preview API] Attach a new file to a pull request.
        :param object upload_stream: Stream to upload
        :param str file_name: The name of the file.
        :param str repository_id: The repository ID of the pull request’s target branch.
        :param int pull_request_id: ID of the pull request.
        :param str project: Project ID or project name
        :rtype: :class:`<Attachment> <azure.devops.v6_0.git.models.Attachment>`
        """
        route_values = {}
        if project is not None:
            route_values['project'] = self._serialize.url('project', project, 'str')
        if file_name is not None:
            route_values['fileName'] = self._serialize.url('file_name', file_name, 'str')
        if repository_id is not None:
            route_values['repositoryId'] = self._serialize.url('repository_id', repository_id, 'str')
        if pull_request_id is not None:
            route_values['pullRequestId'] = self._serialize.url('pull_request_id', pull_request_id, 'int')
        if "callback" in kwargs:
            callback = kwargs["callback"]
        else:
            callback = None
        # content = self._client.stream_upload(upload_stream, callback=callback)
        response = self._send(http_method='POST',
                              location_id='965d9361-878b-413b-a494-45d5b5fd8ab7',
                              version='6.0-preview.1',
                              route_values=route_values,
                              content=upload_stream.read(),
                              media_type='application/octet-stream')
        return self._deserialize('Attachment', response)

things work.

@SpinyCucumber
Copy link

I'm experiencing this as well in October of 2023.
Here's a small example, similar to the example in #129 mentioned above:

from azure.devops.connection import Connection
from azure.devops.v7_0.git.git_client import GitClient
from azure.devops.v7_0.git.models import *
from msrest.authentication import BasicAuthentication
from pathlib import Path

# Insert your own credentials/IDs here
personal_access_token = "notarealpersonalaccesstoken"
organization_url = "https://dev.azure.com/notarealorganization"
authentication = BasicAuthentication("", personal_access_token)
repository_id = "some-letters-and-numbers"
pull_request_id = 123456

connection = Connection(organization_url, authentication)
client: GitClient = connection.clients.get_git_client()

# Open file and call create attachment
# Expect file to be in same directory as script
file_name = "test.jpg"
upload_path = Path(__file__).parent / file_name
with upload_path.open("rb") as upload:
    result = client.create_attachment(upload, file_name, repository_id, pull_request_id)
    print(result)

When I run this code, I receive the following error:

Traceback (most recent call last):
  File ".\create_attachment.py", line 22, in <module>
    result = client.create_attachment(upload, file_name, repository_id, pull_request_id, project)
  File "C:\SomeDirectory\.venv\lib\site-packages\azure\devops\v7_0\git\git_client_base.py", line 1229, in create_attachment
    media_type='application/octet-stream')
  File "C:\SomeDirectory\.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:\SomeDirectory\.venv\lib\site-packages\azure\devops\client.py", line 68, in _send_request
    self._handle_error(request, response)
  File "C:\SomeDirectory\.venv\lib\site-packages\azure\devops\client.py", line 270, in _handle_error
    raise AzureDevOpsServiceError(wrapped_exception)
azure.devops.exceptions.AzureDevOpsServiceError: Nullable object must have a value.

Currently, I'm using Python 3.7.4 with azure-devops 7.1.0b3.

I can also vouch that using the create_attachment implementation given in the original post prevents the error, however it requires reading the entire file into memory, which is less than ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant