Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I use list the branch restrictions based on branching model?

Radu Cristescu May 5, 2025

How do I use the Bitbucket API to list the branch restrictions based on branching model instead of pattern?

Documentation:

https://developer.atlassian.com/cloud/bitbucket/rest/api-group-branch-restrictions/#api-repositories-workspace-repo-slug-branch-restrictions-get

  • It does not say how I can filter by `branch_match_kind + branch_type`
  • The `q` parameter does not work

As things stand, I have to pull all the restrictions, including going through all the result pages. I can reduce the set by filtering by `kind` if I know what I'm looking for, but it's still annoying because I have to do further filtering afterwards.

1 answer

0 votes
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 6, 2025

Hey @Radu Cristescu ,

Thank you for reaching out to the community!

Unfortunately, that endpoint currently does not support filtering by the fields branch_match_kind and/or branch_type. I went ahead and raised a feature request to our development team to implement the support for filtering by those fields in this particular endpoint. You can access the feature request from the link below:

I would suggest that you add your vote there, since this helps both developers and product managers to understand the interest. Also, make sure you add yourself as a watcher in case you want to receive first-hand updates from that ticket. Please note that all features are implemented with this policy in mind.

While that feature is not available, it would be necessary to iterate through all the pages and then filter using an external tool such as jq.

Thank you, @Radu Cristescu !

Patrik S

Radu Cristescu May 6, 2025

Thanks. Page size 100 avoids the need to paginate for me (there are around 27 restrictions per repository I manage).

Below is a section of my shell script code that I wrote to work with this situation. It was annoying to write, so I thought I'd share, in case it helps others (or ChatGPT).

I don't have overlapping restrictions in the cases where I have both glob and branching model restrictions for a branch.

By the magic of Bitbucket API ignoring custom properties and properties that don't make sense in the context (e.g. the "id" field when creating a new restriction), I store a "_branch" private property so I can easily search for restrictions by type and branch regardless of their original source.

My use case is to find a restriction, delete it temporarily, and then put it back, so this works pretty well.

Code:

BibbucketAuth=my-username:token-value
BaseUrl
=https://api.bitbucket.org/2.0/repositories/my-workspace-slug

## inject auth into http command
http() {
command http -a "$BitbucketAuth" "$@"
}

## Basic wrapper to call Bitbucket REST API
bbapi() {
local Method="$1" Repo="$2" Api="$3"
shift 3
http -b -j "$Method" "$BaseUrl/$Repo/$Api" "$@"
}

## Get the branch names from the branching model
getEffectiveBranchingModel() {
local Repo="$1"
</dev/null bbapi GET "$Repo" effective-branching-model |
jq -r '"DEV_BRANCH=" + .development.name, "PROD_BRANCH=" + .production.name'
}

## Get all branch restrictions and store in the cache
## Attach private property "._branch",
## which is either from the branching model or the glob pattern
declare
-A _bbRestrictionCache

cacheAllRestrictions() {
local Repo="$1"

local
DEV_BRANCH PROD_BRANCH
eval "$(getEffectiveBranchingModel "$Repo")"

local allRestrictions=$(
</dev/null bbapi GET "$Repo" branch-restrictions pagelen==100 |
jq --arg DEV_BRANCH "$DEV_BRANCH" --arg PROD_BRANCH "$PROD_BRANCH" '.values[] |
if .branch_match_kind == "branching_model" then
if .branch_type == "development" then ._branch=$DEV_BRANCH
elif .branch_type == "production" then ._branch=$PROD_BRANCH
end
else ._branch = .pattern
end'
)

_bbRestrictionCache["$Repo"]="$allRestrictions"
}

## Get a branch restriction from the cache
getCachedRestriction() {
local Repo="$1" Branch="$2" Kind="$3"

if [[ -z "${_bbRestrictionCache[$Repo]}" ]]; then
cacheAllRestrictions "$Repo"
fi

local restriction=$(
jq --arg branch "$Branch" --arg kind "$Kind" \
'select(._branch==$branch and .kind==$kind)' <<<"${_bbRestrictionCache[$Repo]}"
)

if [[ -z "$restriction" ]]; then
echo "null"
else
echo "$restriction"
fi
}

Example usage:

## Get a restriction and delete it if it exists
checksRestriction
="$(getCachedRestriction "$Repo" "$Dest" enforce_merge_checks)"
if [[ "$checksRestriction" != "null" ]]; then
restrictionId="$(jq -r '.id' <<<"$checksRestriction")"
deleteRestriction "$Repo" enforce_merge_checks "$restrictionId"
fi

## ... do some stuff, e.g. merge a PR that hasn't been approved ...

## Put the restriction from above back (in the cleanup section)
if [[ "$checksRestriction" != null ]]; then
bbapi POST "$Repo" branch-restrictions <<<"$checksRestriction"
fi

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin Site Admin
TAGS
AUG Leaders

Atlassian Community Events