Niall Hodgen
Niall Hodgen

Reputation: 11

Bitbucket Cloud API: is there a way to edit a file in a repo?

The Bitbucket Server API documentation outlines an endpoint to submit a PUT request and edit a file within a specified repository. However, the Bitbucket Cloud API does not seem to have this same functionality included.

Is there a direct or indirect way to edit a file using Bitbucket Cloud API?

My specific use case is to bump the version number specified within a build pipeline YAML file each time a successful Production release happens.

The current Python template provided for the Bitbucket Server API is as follows:

import requests
import json

url = "http://{baseurl}/rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/browse/{path}"

headers = {
  "Accept": "application/json"
}

response = requests.request(
   "PUT",
   url,
   headers=headers
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

However, the Cloud API base URL is obviously different and it will not work.

Upvotes: 1

Views: 619

Answers (1)

Niall Hodgen
Niall Hodgen

Reputation: 11

The work around for this on the Bitbucket Cloud API was to:

  1. GET the original file that I want to edit

  2. Write the contents of this file to a new empty file with the same name

  3. Edit the contents of the file to suit my needs

  4. POST the edited file (Posting a file with the same name to the same location causes an overwrite of the original)

Things to note:

  • I used Python to write my script, using the Requests library for the HTTP calls and a couple other libraries to parse the file as needed.
  • My script committed directly to the main branch. In order to do so, you need to ensure that you are not blocked by any branching restrictions. I had to create an exception for the CI account we use to do any automation tasks like this.

Upvotes: 0

Related Questions