'how to edit py file in azure repo using azure devops pipline?

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



Solution 1:[1]

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

enter image description here

Now, we get the pushId, and we could use the REST API Pushes - Get to get the commitId:

enter image description here

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:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leo Liu-MSFT