Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,457,033
Community Members
 
Community Events
176
Community Groups

Bitbucket REST API: "Something went wrong" when trying to create a branch restriction

I'm trying to use the bitbucket REST api to create a branch restriction rule on a repository according https://developer.atlassian.com/cloud/bitbucket/rest/api-group-branch-restrictions/#api-repositories-workspace-repo-slug-branch-restrictions-post.

Here is the python code I'm using:

 url = f"https://api.bitbucket.org/2.0/repositories/{workspace}/{repository}/branch-restrictions"

headers = {
"Accept": "application/json",
"Content-Type": "application/json",
}

payload = json.dumps(
{
"type": "branchrestriction",
"kind": "push",
"branch_match_kind": "branching_model",
"branch_type": "development",
"users": [ALLOWED_USER], # ALLOWED_USER = "{asdf-asdf-asdf-asdf-asdf}"
}
)

response = requests.request(
"POST",
url,
data=payload,
headers=headers,
auth=(BBCLI_USERNAME, BBCLI_PASSWORD),
)

The response I get is:

500
{'error': {'id': '8f1cbdfd520e48c8b362078b9420c139',
'message': 'Something went wrong'},
'type': 'error'}

This happens for repositories that don't have any branch restrictions set up and for ones that have some but not the rule I'm trying to set up.

Listing branch permissions works fine.

 

What's wrong with my request?

1 answer

1 accepted

1 vote
Answer accepted

Hey @Nick de Palézieux ,

Thank you for reaching out to Atlassian Support.

Accordingly, to the API reference for the Create branch restriction endpoint, I think the syntax you have used for defining the users might be incorrect, and thus the error you are facing.

I went ahead and did some tests in my repository and was able to successfully create a branch restriction using the following example request body : 

{

  "kind": "push",

  "branch_match_kind": "branching_model",

  "branch_type": "development",

  "users": [

    {

      "username": "bitbucket_username_1"
},
{

      "username": "bitbucket_username_2"

    }

  ]

}

The resulting complete curl command is the below : 

curl --request POST --user USERNAME:APP_PASSWORD --url https://api.bitbucket.org/2.0/repositories/<WORKSPACE>/<REPOSITORY>/branch-restrictions --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{

  "kind": "push",

  "branch_match_kind": "branching_model",

  "branch_type": "development",

  "users": [

    {

      "username": "bitbucket_username_1"
},
{

      "username": "bitbucket_username_2"

    }

  ]

}'

You can try using the command above and check how it goes, and then adapt it to your python script syntax.

Hope that helps! Let me know in case you run into any issues.

Thank you, @Nick de Palézieux .

Kind regards,

Patrik S

Hi @Patrik S 

 

Thanks for your response. You're right that the problem was the way I was specifying the user. I don't find the documentation to be clear enough.

Trying your suggestion with a single user works. However, your example of specifying two users seems wrong, as you are providing a dictionary with two entries that have the same key `username`.

Should it instead be

"users": [

    { "username": "bitbucket_username_1" },

    { "username": "bitbucket_username_2" }

  ]

Patrik S Atlassian Team Jun 29, 2022

Hey @Nick de Palézieux ,

Happy to hear that it worked!

As for the syntax to add more than one user, you're right!  I've updated my previous answer to fix the syntax, thank you for the heads-up!

Kind regards,

Patrik S

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS

Atlassian Community Events