I have looked through the documentations and Questions/Discussions on this forum, and yet wasn't able to create a bitbucket branch using the API
I've tried exactly what the documentation suggests:
curl -X POST -s \
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/refs/branches" \
--header 'Accept: application/json' \
--header "Authorization: Bearer $ACCESS_TOKEN" \
-d '{ "name" : "test", "target" : { "hash" : "develop" } }'
and it returns me the error:
{"type": "error", "error": {"message": "Bad request", "fields": {"name": "required key not provided", "target": "required key not provided"}}}
and I've also tried:
curl -X POST -s \as mentioned in this discussion:
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/src/" \
-F "branch=test" \
--header 'Accept: application/json' \
--header "Authorization: Bearer $ACCESS_TOKEN"
{"type":"error","error":{"message":"Something went wrong","id":"db9155a6a32946e99dbc2d9201a9dd57"}}
{"type":"error","error":{"message":"Something went wrong","id":"153531f483bb4baf90ad8968311670da"}}
{"type":"error","error":{"message":"Something went wrong","id":"aa3bd7c29ad44b559a4c450e2e2db7f6"}}
Can anyone help me with this?
Confirmation that your end is working is also appreciated.
edit: Spelling
Hi, @Arthur Gramiscelli Branco! Welcome to the community!
You can run the following request to create a branch using API:
curl -X POST -s \
--url "https://api.bitbucket.org/2.0/repositories/{workspace}/{repository_name}/refs/branches" \
-H "Content-Type: application/json" \
-u username:AppPassword \
-d '{ "name" : "test", "target" : { "hash" : "develop" } }'
I tested this on my side and it's working fine. Could you please run this and confirm if it works for you?
Kind regards,
Caroline
Hi @Caroline R, Thank you for your answer!
Our client wants us to use the Oauth consumer. Can you try again passing a consumer token please?
ps: The consumer I'm using for testing has all available permissions checked.
To fetch my token I'm using:
ACCESS_TOKEN=$(curl -X POST -s https://bitbucket.org/site/oauth2/access_token -d grant_type=client_credentials -u "$consumer_key:$consumer_secret" | jq -r '.access_token')
And I've checked if the token was working using this example (it is):
curl -X GET -s \
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/refs/branches/develop" \
--header 'Accept: application/json' \
--header "Authorization: Bearer $ACCESS_TOKEN"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Caroline R I manually tried again using your example and it worked even with the consumer token, looks like I failed to correctly pass some of my variables in our script, thank you for the help.
What I was doing:
curl -X POST -s \
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/refs/branches" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d "{ "name" : "$NEW_RELEASE", "target" : { "hash" : "develop" } }"
What worked:
curl -X POST -s \
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/refs/branches" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d "{ \"name\" : \"$NEW_RELEASE\", \"target\" : { \"hash\" : \"develop\" } }"
I forgot to backslash my quotes =X hahaha my bad, thank you again
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Awesome, @Arthur Gramiscelli Branco! I'm glad to know it worked!
Please feel free to reach out if you ever need anything else!
Kind regards,
Caroline
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.