Python-gitlab - project.branch.list filter

Hello,

I’m trying to use python-gitlab v3.12.0 and I want to get a list of branches that are merged.

Shouldn’t these work?

     branchlist = self.project.branches.list(merged=True, iterator=True)

Or even this?
branchlist = self.project.branches.list(query_parameters={‘merged’: ‘True’}, iterator=True).

They both seem to return a full list of branches with no filtering. Am I missing something?

1 Like

Hm, strange. I can confirm your observed behaviour, I wrote a quick script to print both, merged and unmerged branches and get all branches no matter the filter set. Branches - python-gitlab v3.12.0 does not look like it supports a branch filter.

#!/usr/bin/env python

import gitlab
import os
import sys

GITLAB_SERVER = os.environ.get('GL_SERVER', 'https://gitlab.com')
# https://gitlab.com/dnsmichi/opsindev.news
PROJECT_ID = os.environ.get('GL_PROJECT_ID', 33298437)
GITLAB_TOKEN = os.environ.get('GL_TOKEN')

if not GITLAB_TOKEN:
    print("Please set the GL_TOKEN env variable.")
    sys.exit(1)

gl = gitlab.Gitlab(GITLAB_SERVER, private_token=GITLAB_TOKEN)

# Main
project = gl.projects.get(PROJECT_ID, lazy=False, pagination="keyset", order_by="updated_at", per_page=100)

# Get all merged branches
merged_branches_names = []
for branch in project.branches.list(merged=True, iterator=True):
    merged_branches_names.append(branch.name)

print("Branches merged: {b}".format(b=", ".join(merged_branches_names)))

# Get un-merged branches
not_merged_branches_names = []
for branch in project.branches.list(merged=False, iterator=True):
    not_merged_branches_names.append(branch.name)

print("Branches not merged: {b}".format(b=", ".join(not_merged_branches_names)))
GL_TOKEN=$GITLAB_TOKEN python3 get_branches_by_state.py                            ─╯
Branches merged: main, test-branch
Branches not merged: main, test-branch

Looks like that either the filter for branches is not supported, or we hit a bug here. Mind opening an issue upstream to confirm? Thanks!

1 Like

It seems that the filter is not available for branches.list(). I’ve added a manual check in the loop for processing the data, the example is explained in Python-Gitlab - Reset iterator on RestobjectList - #2 by dnsmichi with an updated source code script.

1 Like

Going back and re-reading api, I agree; It does not support filter.