Comments (9)
@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.
Seleha from the TCM team has provided us with this helpful information:
This will be a two step process
- 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- 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:
- WorkItemTrackingClient create_attachment
- WorkItemTrackingClient update_work_item
I hope this helps,
Ted
from azure-devops-python-api.
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.
@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.
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.
Awesome!!!
from azure-devops-python-api.
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.
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.
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)
- Reconsider removing v5 & v6 HOT 5
- git_client is not found in v7_0 HOT 9
- No module named 'azure.devops.v7_0.release' HOT 3
- No module named 'azure.devops.v7_0.git.git_client' HOT 1
- docstrings are inconsistent making Sphinx documentation not display properly HOT 1
- Can no longer get any client - azure.core.exceptions.DeserializationError HOT 4
- [Showstopper] Cannot use continuation token in API 7.0 and above (thus limiting number of results) HOT 7
- Looking for a sample code in python which can programmatically build and run a CI pipeline for new ADO repo.
- RunPipelineParameters: template_parameters includes mandatory parameters but their values are not found; incorrectly raises AzureDevOpsServiceError HOT 1
- 'DefaultAzureCredential' object has no attribute 'signed_session' HOT 3
- Getting started code throws error in notebook HOT 3
- Code example bug in the readme HOT 5
- Package fails import on Python 3.12, missing dependencies HOT 2
- Dependency missing
- Attachments from Testcases with Rest Api
- Cannot get accounts list for a multi-organization access token
- list_pipelines() always throws AzureDevOpsAuthenticationError - azure-devops v7.1.0b4
- msrest-for-python is deprecated HOT 1
- git_client.create_repository() Failing with msrest.exceptions.SerializationError
- CreatePipelineConfigurationParameters missing parameters HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-devops-python-api.