Code Monkey home page Code Monkey logo

Comments (9)

tedchamb avatar tedchamb commented on May 18, 2024 2

@jfthuong The fix has been released in 0.1.24

Example method to upload a file and link it as a work item attachment:

def upload_work_item_attachment(wit_client, work_item_id, file_name, callback=None):
    # upload file / create attachment
    with open(file_name, 'r+b') as file:
        # use mmap, so we don't load entire file in memory at the same time, and so we can start
        # streaming before we are done reading the file.
        mm = mmap.mmap(file.fileno(), 0)
        attachment = wit_client.create_attachment(mm, file_name=file_name, callback=callback)

    # Link Work Item to attachment
    patch_document = [
        JsonPatchOperation(
            op="add",
            path="/relations/-",
            value={
                "rel": "AttachedFile",
                "url": attachment.url
            }
        )
    ]
    wit_client.update_work_item(patch_document, work_item_id)

from azure-devops-python-api.

tedchamb avatar tedchamb commented on May 18, 2024

Seleha from the TCM team has provided us with this helpful information:

This will be a two step process

  1. The first step is to add an attachment to the test case work item.
    This can be done using this API - https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/attachments/create?view=azure-devops-rest-5.0
  2. To attach the uploaded attachment to the test step, you can use this API sample - https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/update?view=azure-devops-rest-5.0#add_an_attachment with comment as [TestStep=] and URL as obtained from the previous API.
    If however you are open to using C# or JS clients, this step is simplified. The samples can be found here - https://blogs.msdn.microsoft.com/devops/2016/11/02/how-to-use-test-step-using-rest-client-helper/

Regards
Saleha

In the python code the methods for step 1 and 2 can be found here:

  1. WorkItemTrackingClient create_attachment
  2. WorkItemTrackingClient update_work_item

I hope this helps,
Ted

from azure-devops-python-api.

jfthuong avatar jfthuong commented on May 18, 2024

Thank you for the answer, that will help a bit to understand.

Could you tell me actually what is the stream for the create_attachment function?

    def create_attachment(self, upload_stream, project=None, file_name=None, upload_type=None, area_path=None):
        """CreateAttachment.
        Uploads an attachment.
        :param object upload_stream: Stream to upload
        ...
        """

I tried both solutions below but none seems to work:

with open('./smiley.png', "rb") as f:
    binary_content = bytes(f.read())
attachment = client_wit.create_attachment(binary_content, project, 'smiley.png')
attachment = client_wit.create_attachment("Hello my friend", project, 'test6.txt')

(this solution creates a text file but it contains the double quotes in the text)

Thanks

from azure-devops-python-api.

tedchamb avatar tedchamb commented on May 18, 2024

@jfthuong , sorry about that. There is currently a bug blocking uploads, that is being tracked is being tracked by #129. Hope to get this resolved soon.

from azure-devops-python-api.

jfthuong avatar jfthuong commented on May 18, 2024

I see.

Hope it will be resolved soon.

The only step I miss is that one (uploading a file correctly).

This the code I have used for attaching an uploaded attachment to a Test Step
NOTE: You need to have both the attribute name and the comment [TestStep=x]: when you update the Test Case otherwise it is not linked to Test Step.

def add_attachment(wit_id: int, project: str, url:str, comment: str, name:str = None):
    """Add attachment already uploaded to a WorkItem"""
    # We want to have the possibility to set a comment
    attributes = {"comment": comment}
    if name is not None:
        attributes["name"] = name

    patch_document = [
        JsonPatchOperation(
            op="add",
            path="/relations/-",
            value={
                "rel": "AttachedFile",
                "url": url,
                "attributes": attributes,
            },
        )
    ]
    return wit_client.update_work_item(patch_document, wit_id, project)

def add_attachment_to_step(tc_id:int, project:str, url:str, step:int, comment="", name=""):
    """Add an attachment to a given step of a Test Case"""
    name = name if name else re.sub(r".*fileName=", "", url)
    comment = f"[TestStep={step}]:{comment}"
    return add_attachment(tc_id, project, url, comment, name)

attachment = client.wit.create_attachment(my_stream, project, 'my_picture.png')
add_attachment_to_step(my_tc, project, attachment.url, 3, "Expected result")

I will close the issue and hope #129 to be solved very soon.

Thanks again for the support

from azure-devops-python-api.

jfthuong avatar jfthuong commented on May 18, 2024

Awesome!!!

from azure-devops-python-api.

bbalfour5 avatar bbalfour5 commented on May 18, 2024

The example method seems to be working for me, except I get the error JsonPatchOperation is not defined. Is there a library or extension I need to use for it?

from azure-devops-python-api.

ym500 avatar ym500 commented on May 18, 2024

hi @bbalfour5 , just come to it now
you need to import it with

from azure.devops.v5_1.py_pi_api import JsonPatchOperation

from azure-devops-python-api.

jfthuong avatar jfthuong commented on May 18, 2024

Actually I was importing it like from azure.devops.v5_1.work_item_tracking.models

from azure.devops.v5_1.work_item_tracking.models import JsonPatchOperation

Or if you use version 4.1 of the API:

from vsts.work_item_tracking.v4_1.models.json_patch_operation import JsonPatchOperation

from azure-devops-python-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.