After lots lots of struggle, finally framed below code to interact with Azure Devops via REST API but dont see file in Azure Devops- Repos's Branches.
First created a attachment using below API:
## Create attachment ###########################################
ado_req_headers_ATT = {"Content-Type":"application/octet-stream"}
directory_path = str('C:\\Users\\XXX\\YYY')
txt_files = [f for f in os.listdir(directory_path) if f.endswith('.json')]
if len(txt_files) != 1:
raise ValueError('should be only one json file in the current directory')
else:
file_name = txt_files[0]
print("Test_file",file_name)
file_path = directory_path+"\\"+str(file_name)
with open(file_path, "rb") as f:
Test_file=f.read()
print(len(Test_file))
create_attach_url = str(organization_url)+str('/_apis/wit/attachments?fileName=')+file_name+str('&uploadType=Simple&api-version=6.0')
create_attach_details = requests.post(url=create_attach_url,headers=ado_req_headers_ATT,data=Test_file,auth=('',ADO_AUTH_PAT))
attachment_obj = create_attach_details.json()
print("create_attach_details_ID_:-",attachment_obj['id']) ## Got attachment ID
print("create_attach_details_URL_:-",attachment_obj['url']) ## Got Url, when this url get hit in browser, got the attachment to get download.
print("Attachment_details_Status_Code:-",create_attach_details.status_code) # code: 201
Later uploaded the attachment using below API:
## upload attachment ###########################################
CL=len(Test_file)
Range=(f'"Content-Range":"bytes 0-{CL-1}/{CL}"')
ado_req_headers_ATT = str('{"Content-Type":"application/octet-stream","Content-Length":"')+str(CL)+str('"')+str(',')+Range+str('}')
ado_req_headers_ATT=json.loads(ado_req_headers_ATT)
print(ado_req_headers_ATT)
upload_attach_url = str(organization_url)+str('/_apis/wit/attachments/')+str(attachment_obj['id'])+str('?api-version=6.0')
print(upload_attach_url)
upload_attach_details = requests.put(url=upload_attach_url,headers=ado_req_headers_ATT,data=Test_file,auth=('',ADO_AUTH_PAT))
print(upload_attach_details)
attachment_obj = upload_attach_details.json()
print(attachment_obj)
print("Attachment_details:-",create_attach_details.text)
##print("create_attach_details_ID_:-",attachment_obj['id']) ## Got attachment ID
##print("create_attach_details_URL_:-",attachment_obj['url']) ## Got Url, when this url get hit in browser, got the attachment to get download.
print("Attachment_upload_Status_Code:-",create_attach_details.status_code)## Code: 201
Both the API's are suucessful but still couldn't see the file in azure devops-Repos Branches. What am i missing, couldn't understand or post anymore API's need to hit ? pls suggest.
Post , i have tried to update the workitem using below API:
##### Update Attachment in path #######################################
ado_req_headers_ATT = {"Content-Type":"application/json-patch+json","dataType":"application/json-patch+json",}
update_attach_url = str(organization_url)+str('/_apis/wit/workitems/')+str(attachment_obj['id'])+str('?api-version=6.0')
print(update_attach_url)
#Body_data = [{"op": "test","path": "/ref","value": 3},{"op": "add","path": "/heads/branchname","value": "Adding the necessary spec"},{"op": "add","path": "/relations/-","value": {"rel": "AttachedFile","url": attachment_obj['url'],"attributes": {"comment": "Spec for the work"}}}]
Body_data = [{"op": "add","path": ref/headers/banchname,"value": {"rel": "AttachedFile","url": str(attachment_obj['url'])+str('?fileName=')+file_name,"attributes": {"comment": "Spec for the work"}}}]
Body_data = json.dumps(Body_data)
print(Body_data)
update_attach_details = requests.patch(url=update_attach_url,headers=ado_req_headers_ATT,data=Body_data,auth=('',ADO_AUTH_PAT))
print(update_attach_details.text) ## Page not found
print(update_attach_details.status_code) ## 404
Any suggestions please