Pushing to a Bitbucket Repository via the API

My use case is as follows:
1 - A user wants to backup changes (i.e version control) to their work to their Bitbucket Repo
2 - We ask the user to provide us with the necessary credentials and to do this operation on their behalf
3 - We also ask the user for other details like the repository name, branch, etc to which this change needs to be pushed to.
4 - Every time a user does some change and wishes to save the change, we will push those to the configured repo.

My questions - Is the Atlassian Connect framework building an add-on for Bitbucket [1] the correct approach to do this or should I just go ahead an use the REST API with OAuth?

I tried following the guide [1] to build a sample add-on but the addon.authenticate() seems to fail despite providing it with the key from my user account. I get a 401 when it tries to do the JWT auth for the /connect-example endpoint.

[1] - https://developer.atlassian.com/bitbucket/guides/getting-started.html

@pratik, it doesn’t sound like you have any use for the Connect framework here, if I understand correctly that the user interface you provide is somewhere else, not embedded in bitbucket.org.

You should be able to build this integration using the Bitbucket REST API and OAuth 2.0, as you say.

Thanks @epehrson!

So I’m going to try OAuth 2 [1] followed by creating a new commit [2]. The only way to commit seems to be with multipart/form-data or application/x-www-form-urlencoded. Would there be any way to send a body with base64 encoded content? The equivalent of

curl -X POST \
  https://url/ \
  -H 'content-type: application/json' \
  -d '{"encoding": "base64", "content" : "<Base 64 encoded content>"}'

[1] Use OAuth on Bitbucket Cloud | Bitbucket Cloud | Atlassian Support
[2] https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/{username}/{repo_slug}/src#post

I was able to get this to work by sending the payload of the HTTP POST as x-www-form-urlencoded with the key being the path to the file and the value being the content of the file.

I had to play around the API a bit since I was trying to push to a specific branch and the documentation did not describe a way to do it.

curl -X POST \
  {{url}}/2.0/repositories/{{username}}/{{repo_slug}}/src \
  -H 'authorization: Bearer {{access_token}}' \
  -H 'content-type: application/x-www-form-urlencoded' \
  -d '{{file_path={{file_data}}&message={{my_commit_message}}&author={{name}} <{{emai}}>&parents={{parent_sha1}}&branch={{branch_name}}'

The parent sha1 can be obtained from the branch by looking at the target->hash in the response for the request below

curl -X GET \
  {{url}}/2.0/repositories/{{username}}/{{repo_slug}}/refs/branches/{{branch_name}} \
  -H 'authorization: Bearer {{access_token}}'

Hi Pratik,

Could you please share me code snippet, As i am unable to store the file contents into Bitbucket Repo. as you have mentioned bitbucket API documentation does not given clarity on this.

Thanks
Raja

My POST API ;
https://api.bitbucket.org/2.0/repositories/nitesh/test-extract/src?file_path=/README.md&message=test_record&parents=6c85744e48469801e8ed2bf91c437923fce4d252&branch=single_pick

Getting following error :
{
“type”: “error”,
“error”: {
“message”: “Something went wrong”,
“id”: “3a78894bf11d4466b0f35c58f1b752e0”
}
}

I’m using Token-based but I’m still see this message

Hello @SuhaibBaba

Please don’t cross-post. You have asked that question in this thread.