I have lots of py files in repo and I want to edit them using azure devops pipline, is that possible? every time I put a file I can edit it with CI / CD
Asked
Active
Viewed 185 times
-1
-
1Why do you want to do this? What is your goal? – Daniel Mann May 08 '22 at 14:51
-
Is there any update for this issue? Feel free to let me know if the answer could give you some help. Just a reminder of [this](https://stackoverflow.com/help/someone-answers). – Leo Liu May 30 '22 at 06:12
1 Answers
0
how to edit py file in azure repo using azure devops pipline?
You could use the REST API Pushes - Create:
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pushes?api-version=6.0
Update a file, Request Body:
{
"refUpdates": [
{
"name": "refs/heads/master",
"oldObjectId": "fd1062428e0567cfbfcc28ac59d4bea077ce81c1"
}
],
"commits": [
{
"comment": "Added a few more items to the task list.",
"changes": [
{
"changeType": "edit",
"item": {
"path": "/tasks.md"
},
"newContent": {
"content": "# Tasks\n\n* Item 1\n* Item 2\n* Item 3\n* Item 4\n\nIf you need to add more, update this file and add them!",
"contentType": "rawtext"
}
}
]
}
]
}
And the oldobjectid
is the latest commit ID on the current branch for the Azure Devops Repo.
To get the oldObjectId
(latest commit id), we could use the REST API Pushes - List with URI Parameters top=1
and searchCriteria.refName=refs/heads/master
:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pushes?&$top=1&searchCriteria.refName=refs/heads/master&api-version=6.0
Now, we get the pushId
, and we could use the REST API Pushes - Get to get the commitId
:
Then, we could use the REST API Pushes - Create with request body:
{
"refUpdates": [
{
"name": "refs/heads/master",
"oldObjectId": "e71544e80870e83cfd3eb3a797eda9c6227c66a7"
}
],
"commits": [
{
"comment": "Added task markdown file.",
"changes": [
{
"changeType": "add",
"item": {
"path": "/tasks.md"
},
"newContent": {
"content": "# Tasks\n\n* Item 1\n* Item 2",
"contentType": "rawtext"
}
}
]
}
]
}
The test result:

Leo Liu
- 71,098
- 10
- 114
- 135