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
Advertisement
Answer
Below two API’s ie enough to upload the file into repository:
######## Get last commit ID ########################## #Get_commit_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][0]['id'])+str('/commits?api-version=6.0') Get_commit_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][Repository_ID]['id'])+str('/commits?api-version=6.0') print(Get_commit_url) Get_commit_Details = requests.get(url=Get_commit_url,headers=ado_req_headers_ATT,auth=('',ADO_AUTH_PAT)) ##print(Get_Push_Details.text) ##print(Get_Push_Details.status_code) Get_commit_Details=Get_commit_Details.json() print(Get_commit_Details) for item in Get_commit_Details["value"]: print( item["commitId"]) old_objectID= item["commitId"] break ## Run push API ado_req_headers_ATT = {"Content-Type":"application/json-patch+json"} push_file_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][Repository_ID]['id'])+str('/pushes?api-version=6.0') print(push_file_url) ##Body_data = {"refUpdates": [{"name": Repos_obj['value'][0]['defaultBranch'],"oldObjectId":"0000000000000000000000000000000000000000"}],"commits":[{"comment": "Initial Commmit.","changes":[{"changeType": "add","item": {"path":"/Hello.doc"},"newContent": {"content": "# Tasksnn* Item 1Hellon* Item 2","contentType": "rawtext"}}]}]} Body_data = {"refUpdates": [{"name": Repos_obj['value'][Repository_ID]['defaultBranch'],"oldObjectId":old_objectID}],"commits":[{"comment": "Initial Commmit.","changes":[{"changeType": "add","item": {"path":"/Hello.doc"},"newContent": {"content": "# Tasksnn* Item 1Hellon* Item 2","contentType": "rawtext"}}]}]} ##Body_data = {"refUpdates": [{"name": Repos_obj['value'][Repository_ID]['defaultBranch'],"oldObjectId":old_objectID}],"commits":[{"comment": "Remove file.","changes":[{"changeType": "delete","item": {"path":"/Hello.doc"}}]}]} Body_data = json.dumps(Body_data) ##print(Body_data) Push_file= requests.post(url=push_file_url,headers=ado_req_headers_ATT,data=Body_data,auth=('',ADO_AUTH_PAT)) print(Push_file.status_code) print(Push_file.text)
Finally i succeeded !!! Thanks everyone for the support