Getting Error Message (message: "kind must be specified") while adding branch restrictions
Asked by Niharika Pvs:

Hey Everybody,

I am working on to add a branch restriction to an existing bitbucket repo using the below script but we are facing few errors as mentioned below.

We are using the below CURL Command to add a branch restriction.

```
curl --request POST \
--url 'https://api.bitbucket.org/2.0/repositories/workspace-name/repo-name/branch-restrictions' \
--header 'Accept: application/json' \
--user 'app-user-name:app-password' \
--data '{
"kind": "require_approvals_to_merge",
"pattern": "*",
"value": 2,
"branch_match_kind": "glob"
}'?
```

Error Message:

#### {"type": "error", "error": {"message": "kind must be specified"}} {#toc-hId-178630232}

Any suggestions on what might be the issue/fix for this ?

Thanks in advance


Answers:
author: Theodora Boudale
body: Hi [@Niharika Pvs](/t5/user/viewprofilepage/user-id/5062142) and welcome to the community!

I believe that the issue is that the header **--header 'Content-Type: application/json'** is missing. I get the same error when I use a call similar to yours.

The issue is fixed if I use the following call instead (note the extra header):

```
curl --request POST \
--url 'https://api.bitbucket.org/2.0/repositories/workspace-name/repo-name/branch-restrictions' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--user 'app-user-name:app-password' \
--data '{
"kind": "require_approvals_to_merge",
"pattern": "*",
"value": 2,
"branch_match_kind": "glob"
}'
```

Please feel free to let me know if this works for you and if you need further assistance!

Kind regards,  
Theodora

comments: author: Niharika Pvs
body: Hey [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086)

Thanks for your reply.

I don't see any errors when I execute the curl command as per your suggestion to add **--header 'Content-Type: application/json'**

But as per the below condition it should add a branch restriction of min two approvals to merge but it is not added. Could you please suggest here .
>
> ```
> --data '{
> "kind": "require_approvals_to_merge",
> "pattern": "*",
> "value": 2,
> "branch_match_kind": "glob"
> }'
> ```


author: Theodora Boudale
body: Hi [@Niharika Pvs](/t5/user/viewprofilepage/user-id/5062142),

**1)** Can you please confirm if the repo is hosted in Bitbucket Cloud or in your Bitbucket Server instance?

The API you are using is for Bitbucket Cloud. However, I just noticed that there is no Bitbucket Cloud account with the email you used to post here. Do you perhaps have a Bitbucket Cloud account with a different email?

If you are unsure what you are using, the URL of a Bitbucket Cloud repo on your browser starts with <https://bitbucket.org/>. If the URL of your repo starts with something other than that, it's most likely a Bitbucket Server repo.

2) If you are indeed using Bitbucket Cloud, could you please add the argument **-v** in the curl command, execute the curl command directly on a terminal, and copy-paste the full output here?

**++Please make sure to sanitize the following info in the output prior to posting it here:++**

* The line that starts with **\> authorization:** in the output, includes credentials info. Please make sure to replace that with a dummy value prior to sharing.
* The workspace-id and repo slug also show in the output, make sure to replace these values as well.

Kind regards,  
Theodora


author: Niharika Pvs
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) Thanks for waiting.

Yes the repo URL is starting with <https://bitbucket.org/> .

I'm using the repo with some other email ID and not with the one I posted here


author: Theodora Boudale
body: Hi [@Niharika Pvs](/t5/user/viewprofilepage/user-id/5062142),

Thank you for the confirmation.

Please follow the instructions I listed here:
> If you are indeed using Bitbucket Cloud, could you please add the argument **-v** in the curl command, execute the curl command directly on a terminal, and copy-paste the full output here?
>
> **Please make sure to sanitize the following info in the output prior to posting it here:**
>
> The line that starts with **\> authorization:** in the output, includes credentials info. Please make sure to replace that with a dummy value prior to sharing.  
> The workspace-id and repo slug also show in the output, make sure to replace these values as well.

Afterwards, please provide the command you ran and the output (both sanitized to remove sensitive info) to further investigate this. If you have any questions, please feel free to let me know.

Kind regards,  
Theodora


author: Niharika Pvs
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) Thanks for the support,

Please find the requested output below

```
curl --request POST \
--verbose \
--url 'https://api.bitbucket.org/2.0/repositories/workspacename/reponame/branch-restrictions' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--user 'XXXXXXXX:XXXXXXXXXXX' \
--data '{
"kind": "require_approvals_to_merge",
"pattern": "*",
"value": 2,
"branch_match_kind": "glob"
}'
```

> Note: Unnecessary use of -X or --request, POST is already inferred.
>
> \* Trying XX.XXX.XX.X:XXX...
>
> \* Connected to api.bitbucket.org (XX.XXX.XX.X) port XXX (#0)
>
> \* ALPN, offering h2
>
> \* ALPN, offering http/1.1
>
> \* successfully set certificate verify locations:
>
> \* CAfile: /etc/ssl/cert.pem
>
> \* CApath: none
>
> \* (304) (OUT), TLS handshake, Client hello (1):
>
> \* (304) (IN), TLS handshake, Server hello (2):
>
> \* (304) (OUT), TLS handshake, Client hello (1):
>
> \* (304) (IN), TLS handshake, Server hello (2):
>
> \* (304) (IN), TLS handshake, Unknown (8):
>
> \* (304) (IN), TLS handshake, Certificate (11):
>
> \* (304) (IN), TLS handshake, CERT verify (15):
>
> \* (304) (IN), TLS handshake, Finished (20):
>
> \* (304) (OUT), TLS handshake, Finished (20):
>
> \* SSL connection using TLSv1.3 / AEAD-AES128-GCM-SHA256
>
> \* ALPN, server accepted to use h2
>
> \* Server certificate:
>
> \* subject: C=US; ST=California; L=San Francisco; O=Atlassian, Inc.; CN=\*.bitbucket.org
>
> \* start date: Jul 1 00:00:00 2022 GMT
>
> \* expire date: Jul 21 23:59:59 2023 GMT
>
> \* subjectAltName: host "api.bitbucket.org" matched cert's "\*.bitbucket.org"
>
> \* issuer: C=US; O=DigiCert Inc; OU=[www.digicert.com](http://www.digicert.com); CN=DigiCert SHA2 High Assurance Server CA
>
> \* SSL certificate verify ok.
>
> \* Using HTTP2, server supports multiplexing
>
> \* Connection state changed (HTTP/2 confirmed)
>
> \* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
>
> \* Server auth using Basic with user 'XXXXXXXXX'
>
> \* Using Stream ID: 1 (easy handle XXXXXXXXXX)
>
> \> POST /2.0/repositories/workspace/reponame/branch-restrictions HTTP/2
>
> \> Host: XXXXXXXXXX
>
> \> authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXX
>
> \> user-agent: XXXXXXXX
>
> \> accept: application/json
>
> \> content-type: application/json
>
> \> content-length: 105
>
> \>
>
> \* We are completely uploaded and fine
>
> \* Connection state changed (MAX_CONCURRENT_STREAMS == 200)!
>
> \< HTTP/2 401
>
> \< server: nginx
>
> \* Authentication problem. Ignoring this.
>
> \< www-authenticate: Basic realm="XXXXXXXXX HTTP"
>
> \< vary: Origin
>
> \< cache-control: max-age=0, no-cache, no-store, must-revalidate
>
> \< content-type: text/plain
>
> \< x-b3-traceid: XXXXXXXXXXXX
>
> \< x-usage-output-ops: 0
>
> \< x-dc-location: Micros-3
>
> \< strict-transport-security: max-age=31536000; includeSubDomains; preload
>
> \< date: Mon, 07 Nov 2022 07:01:43 GMT
>
> \< x-request-id: XXXXXXXXXXXX
>
> \< x-usage-user-time: 0.011652
>
> \< x-usage-system-time: 0.000236
>
> \< x-served-by: XXXXXXXXXX
>
> \< expires: Mon, 07 Nov 2022 07:01:43 GMT
>
> \< x-xss-protection: 1; mode=block
>
> \< x-static-version: XXXXXXXXXX
>
> \< x-content-type-options: XXXXXX
>
> \< x-render-time: XXXXXXXXX
>
> \< x-trace-id: XXXXXXXXXX
>
> \< x-usage-input-ops: 0
>
> \< x-frame-options: SAMEORIGIN
>
> \< x-version: XXXXXXXXXX
>
> \< x-request-count: 2905
>
> \< content-length: 0
>
> \<
>
> \* Connection #0 to host api.bitbucket.org left intact


author: Theodora Boudale
body: Hi [@Niharika Pvs](/t5/user/viewprofilepage/user-id/5062142),

Thank you for the info. I see in the output of the call 'HTTP/2 401', so I think the problem is with the credentials used to make the API call.

When you define credentials in the call:

```
--user 'app-user-name:app-password'
```

**app-user-name** should be the Bitbucket username of the account that you genereate the app password for (and ++not the label of the app password++ ). The username can be found on this page <https://bitbucket.org/account/settings/> after you log in to the account you generated the app password for.

**app-password** should be an app password generated from <https://bitbucket.org/account/settings/app-passwords/> with at least **Repositories: Admin** permissions.

I would suggest first checking if the username is correct. Please also keep in mind that this user needs to have admin permissions to the repo.

After you ensure that the correct username is used, and the user has admin access to the repo, if the call is still failing, please generate a new app password with Repositories: Admin permissions.

Please feel free to let me know how it goes.

Kind regards,  
Theodora


author: Niharika Pvs
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) Thanks for the help, It worked fine now. Really appreciate your effort.

The main requirement of me is to restrict a bitbucket repo by adding the branch restrictions in such a way that the changes should merged to branch until unless it should have min of two approvals, then only user can able to merge the changes to the main branch.

For this I wrote the below three diff Curl's to meet the requirement:

1.Write Access To Nobody Condition

```
#Write Access To Nobody

curl --request POST \
--url 'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--user 'app-user-name:app-password' \
--data '{
"kind":"push",
"branch_match_kind": "glob",
"pattern": "*",
"users": [],
"groups": []
}'
```

2.No Of Approvals Required To Merge Condition

```
#No Of Approvals Required To Merge

curl --request POST \
--url 'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--user 'app-user-name:app-password' \
--data '{
"kind": "require_approvals_to_merge",
"pattern": "*",
"value": 2,
"branch_match_kind": "glob"
}'
```

3.Prevent A Merge With Unresolved MergeChecks Condition

```
#Prevent A Merge With Unresolved MergeChecks
curl --request POST \
--url 'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--user 'app-user-name:app-password' \
--data '{
"kind": "enforce_merge_checks",
"pattern": "*",
"value": null
}'
```

As I'm making three diff API calls to get the solution, I want to make this as a single Curl command like below.When I execute the below Curl it executed successfully but all the required conditions are not applied on the branch only few of them are applied.

```
curl --request POST \
--url 'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--user 'app-user-name:app-password' \
--data '{
"kind":"push",
"value": null,
"branch_match_kind": "glob",
"pattern": "master",
"users": [],
"groups": [],
"kind": "require_approvals_to_merge",
"pattern": "master",
"value": 2,
"branch_match_kind": "glob",
"kind": "enforce_merge_checks",
"pattern": "master",
"value": null
}'
```

Also I referred the below Url <https://community.atlassian.com/t5/Bitbucket-questions/Bad-request-when-adding-Branch-Restrictions-using-the-Rest-API/qaq-p/1150581>

which says ***This endpoint doesn't accept lists, it can only create one branch restriction at a time. What you have in the payload above is actually 4 branch restrictions, not one. They're shown in UI in one dialog, but each setting there is independent.***

I need a solution on this , Could you please correct If there is anything wrong in the command anywhere. Thanks in advance.


author: Theodora Boudale
body: Hi [@Niharika Pvs](/t5/user/viewprofilepage/user-id/5062142),

You are very welcome, it's good to hear that it worked.

Regarding your latest question, the comment you read from Daniil in the other post you linked is correct. It is not possible to create all 3 branch restrictions with one call. You will need to make 3 separate requests, one for each of the 3 restrictions.

If you have any other questions, please feel free to let me know.

Kind regards,  
Theodora


Tags: api

branch

rest-api

restriction