I am trying to add a file into respos folder using below API’s:
ado_req_headers_ATT = {'Content-Type':'application/octet-stream'}
Get_Branch_url = str(organization_url)+str('/_apis/git/repositories/')+str(ReposID)+str('/refs?filter=heads/&api-version=6.0')
print(Get_Branch_url)
Get_Branch_Details = requests.get(url=Get_Branch_url,headers=ado_req_headers_ATT,auth=('',ADO_AUTH_PAT))
#print(Get_Branch_Details.status_code)
print(Get_Branch_Details.text)
Get_Branch_Details=Get_Branch_Details.json()
for item in Get_Branch_Details["value"]:
if item["name"] == **'refs/heads/XXX'**:
old_object_ID = item["objectId"]
old_object_name= item["name"]
print(old_object_name,":-",old_object_ID) **## Prints refs/heads/XXX : YYYYYYYYYYYYYYYYYYYYYY**
The response for the above url includes:
{
"value": [
{...
**"name": "refs/heads/XXX",
"objectId": "YYYYYYYYYYYYYYYYYYYYYY",**
"creator": {
"displayName": "AAAAAAAAAA",
"url": "https://spsprodsin1.vssps.visualstudio.com/UUUUUU",
"_links": {
"avatar": {
"href": "https://dev.azure.com/TTTTTT"
}
},
"id": "MMMMMMMMM",
"uniqueName": "PPPPPPPP",
And treating this Object ID: YYYYYYYYYYYYYYYYYYYYYY as old ObjectID and trying to push a file into path: refs/heads/XXX using below API:
###### Push a File in Ref_Devops-dev ###############################
ado_req_headers_ATT = {"Content-Type":"application/json-patch+json"}
push_file_url = str(organization_url)+str('/_apis/git/repositories/')+str(ReposID)+str('/pushes?api-version=6.0')
Body_data = {"refUpdates": [{"name": refs/heads/XXX,"oldObjectId":YYYYYYYYYYYYYYYYYYYYYY}],"commits":[{"comment": "Added text file.","changes":[{"changeType": "add","item": {"path":"/test.txt"},"newContent": {"content": "# Tasksnn* Item 1n* Item 2","contentType": "rawtext"}}]}]}
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.json())
print(Push_file.status_code)
print(Push_file.text)
But the response were :
{"$id":"1","innerException":null,"message":"TF401035: The object 'YYYYYYYYYYYYYYYYYYYYYY' does not exist.","typeName":"Microsoft.TeamFoundation.Git.Server.GitObjectDoesNotExistException, Microsoft.TeamFoundation.Git.Server","typeKey":"GitObjectDoesNotExistException","errorCode":0,"eventId":3000}
And sure missing a final step & basic as well. Any suggestions pls
Advertisement
Answer
Above error can be eliminated by referring the commit API instead of mentioned API & get the latest commit ID.