I am trying to add a file into respos folder using below API’s:
JavaScript
x
13
13
1
ado_req_headers_ATT = {'Content-Type':'application/octet-stream'}
2
Get_Branch_url = str(organization_url)+str('/_apis/git/repositories/')+str(ReposID)+str('/refs?filter=heads/&api-version=6.0')
3
print(Get_Branch_url)
4
Get_Branch_Details = requests.get(url=Get_Branch_url,headers=ado_req_headers_ATT,auth=('',ADO_AUTH_PAT))
5
#print(Get_Branch_Details.status_code)
6
print(Get_Branch_Details.text)
7
Get_Branch_Details=Get_Branch_Details.json()
8
for item in Get_Branch_Details["value"]:
9
if item["name"] == **'refs/heads/XXX'**:
10
old_object_ID = item["objectId"]
11
old_object_name= item["name"]
12
print(old_object_name,":-",old_object_ID) **## Prints refs/heads/XXX : YYYYYYYYYYYYYYYYYYYYYY**
13
The response for the above url includes:
JavaScript
1
16
16
1
{
2
"value": [
3
{
4
**"name": "refs/heads/XXX",
5
"objectId": "YYYYYYYYYYYYYYYYYYYYYY",**
6
"creator": {
7
"displayName": "AAAAAAAAAA",
8
"url": "https://spsprodsin1.vssps.visualstudio.com/UUUUUU",
9
"_links": {
10
"avatar": {
11
"href": "https://dev.azure.com/TTTTTT"
12
}
13
},
14
"id": "MMMMMMMMM",
15
"uniqueName": "PPPPPPPP",
16
And treating this Object ID: YYYYYYYYYYYYYYYYYYYYYY as old ObjectID and trying to push a file into path: refs/heads/XXX using below API:
JavaScript
1
11
11
1
###### Push a File in Ref_Devops-dev ###############################
2
ado_req_headers_ATT = {"Content-Type":"application/json-patch+json"}
3
push_file_url = str(organization_url)+str('/_apis/git/repositories/')+str(ReposID)+str('/pushes?api-version=6.0')
4
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"}}]}]}
5
Body_data = json.dumps(Body_data)
6
print(Body_data)
7
Push_file= requests.post(url=push_file_url,headers=ado_req_headers_ATT,data=Body_data,auth=('',ADO_AUTH_PAT))
8
print(Push_file.json())
9
print(Push_file.status_code)
10
print(Push_file.text)
11
But the response were :
JavaScript
1
2
1
{"$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}
2
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.