0

I am trying to create a branch restriction using below curl command but it is throwing a bad request. How can I create a working API call? This executed on Jenkins using sh and enclosed in double quotes ("").

curl -X POST -s 'https://api.bitbucket.org/2.0/repositories/$workspace/$repository/branch-restrictions' \
                        --header 'Authorization: Bearer $token' \
                        --header 'Accept: application/json' \
                        --header 'Content-Type: application/json' \
                        --data '{\"type\" : \"branchrestriction\",\"links\": {\"self\": {\"href\": \"https://api.bitbucket.org/2.0/repositories/$workspace/$repository/branch-restrictions\",}},\"kind\":\"push\",\"branch_match_kind\": \"branching_model\",\"branch_type\": \"feature\",\"pattern\": \"feature/*\",\"users\": [\"$user\"],\"groups\": []}'
halfer
  • 19,824
  • 17
  • 99
  • 186
Efox
  • 651
  • 3
  • 8
  • 19
  • Shot in the dark, but if this is your actual command, use double quotes instead of single quotes for the parts that need to use variable expansion, like `"Authorization: Bearer $token"` instead of `'Authorization: Bearer $token'`. In common shells like `bash` and `zsh`, single quotes preserves the literal string they wrap, rather than do expansion of vars. – wkl Aug 02 '22 at 02:26
  • I did actually use actual values but it is not working. Did you try? – Efox Aug 02 '22 at 02:29
  • I don't have a BB instance to test on so I can't really help beyond this - but I just noticed you also escape your double quotes for your `--data`, which aren't necessary if you're wrapping them with single quotes and would mean you're passing invalid JSON. – wkl Aug 02 '22 at 02:37
  • The whole command is actually inside a double quote as it is executed in jenkins. – Efox Aug 02 '22 at 02:43
  • Okay, please include all those details in your post for others, as well as any details like what the response code is that you're getting. Your JSON data _looks_ correct based on the Bitbucket docs, but this could also be something else entirely like the bearer token you're using doesn't have the permission level to actually create a branch restriction. – wkl Aug 02 '22 at 02:46

1 Answers1

0

I was able to successfully add branch restriction by removing other field due to incompatibility with other fields. See below changes.

curl -X POST -s 'https://api.bitbucket.org/2.0/repositories/$workspace/$repository/branch-restrictions'
--header 'Authorization: Bearer $token'
--header 'Accept: application/json'
--header 'Content-Type: application/json'
--data '{"type" : "branchrestriction","kind":"push","branch_match_kind": "glob","pattern": "feature/*","users": [],"groups": []}

Efox
  • 651
  • 3
  • 8
  • 19