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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How do I add branch permission using API with branch name and prevent all changes category?

I saw the documentation we can surely do it but an example would be useful for better understanding. 

API- /rest/branch-permissions/2.0/projects/{projectKey}/repos/{repositorySlug}/restrictions

Correct me if I am wrong with the selection of API call. 

I want to give set of users in JSON format and restrict permission for all others except those in that JSON. I am not sure what should be my JSON schema.  The same way we do it in the UI using Prevent all changes, except by:

1 answer

0 votes
Kristy Atlassian Team May 17, 2018

Hi @Ron Bob,

 

The documentation for branch permissions can be found here and if you find the section that says "Example request representations" you will find an example json request to create a branch permission.

The example givein in the documentation is:

{
    "type": "read-only",
    "matcher": {
        "id": "refs/tags/**",
        "displayId": "refs/tags/**",
        "type": {
            "id": "PATTERN",
            "name": "Pattern"
        },
        "active": true
    },
    "users": [
        "bman",
        "tstark",
        "hulk"
    ],
    "groups": [
        "bitbucket-users"
    ],
    "accessKeys": [
        1,
        2,
        3
    ]
}

 

To answer some of your specific questions:

  • In order to 'prevent all changes' you need to set the "type" field to "read-only"
  • To add the 'except by' you need to add users to the "user" field

Hope that helps! Let me know if you need more details.

Is there any version dependency for using bitbucket server api 2.0 ?

Bitbucket Server does not have a 2.0 REST API. But the Bitbucket Cloud documentation can be found here: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/branch-restrictions

Thanks for answering this. May I know how to remove the restrictions. For eg. disable Prevent all changes.

Could you help me with the same problem?

I had try to use POST by curl to restrict branch permission:

curl --insecure -X POST -H "Authorization: Basic <Base64 hash string>" -H "Content-Type: application/json" -d @@request.json https://somedomain.com/rest/branch-permissions/2.0/projects/MYPRJ/repos/myrepo/restriction

@request.json

{
"type": "read-only",
"matcher": {
"id": "timochkin/release-lock",
"displayId": "timochkin/release-lock",
"type": {
"id": "BRANCH",
"name": "Branch"
},
"active": true
},
"users": [
"atimochkin"
],
"accessKeys": [
1,
2,
3
]
}

and 

HTTP/1.1 200 Connection established

HTTP/1.1 401
Date: Mon, 16 Dec 2019 06:37:48 GMT
Server: Apache
Strict-Transport-Security: max-age=63072000; includeSubdomains; preload
X-AREQUESTID: @257UFSx97x28529719x0
X-ASEN: SEN-2976722
X-AUSERID: 1246
X-AUSERNAME: atimochkin
Cache-Control: no-cache, no-transform
Vary: X-AUSERNAME,Accept-Encoding
Content-Type: application/json;charset=UTF-8
X-Content-Type-Options: nosniff
WWW-Authenticate: OAuth realm="https%3A%2F%2Fsomedomain.com"
Transfer-Encoding: chunked

{"errors":[{"context":null,"message":"You are not permitted to access this resource","exceptionName":"com.atlassian.bitbucket.AuthorisationException"}]}

 

In myrepo, account have Admin permission, what i do wrong?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Powering DevOps with Bitbucket Server & Data Center

Hi everyone, The Cloud team recently announced 12 new DevOps features that help developers ship better code, faster   ! While we’re all excited about the new improvements to Bitbucket ...

1,869 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you