You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python GitLab API按属性筛选列出合并请求?

使用Python GitLab API筛选合并请求

Python GitLab API是对GitLab原生REST API的封装,你在curl里用的author_username、source_branch这类查询参数,直接就能作为关键字参数传给项目的merge_requests.list()方法,不用纠结Python API文档里有没有单独说明。

具体实现步骤:

  1. 初始化GitLab客户端
import gitlab

# 替换为你的GitLab实例地址和私有令牌
gl = gitlab.Gitlab("https://your-gitlab-instance.com", private_token="YOUR_PRIVATE_TOKEN")
# 如果是GitLab.com,可省略实例地址:gl = gitlab.Gitlab(private_token="YOUR_PRIVATE_TOKEN")
  1. 获取目标项目对象
# 可以用项目ID或项目路径(比如"username/project-name")
project = gl.projects.get("your-project-id-or-path")
  1. 传入筛选参数获取合并请求
# 按作者用户名和源分支筛选,还可按需添加其他参数
filtered_mrs = project.merge_requests.list(
    author_username="MY_NAME",
    source_branch="MY_BRANCH",
    state="opened",  # 可选,筛选状态:opened/closed/merged/all
    all=True  # 可选,获取所有分页结果,默认只返回第一页
)

# 遍历处理结果
for mr in filtered_mrs:
    print(f"MR编号: {mr.iid}, 标题: {mr.title}, 状态: {mr.state}")

额外说明:

所有GitLab原生REST API中合并请求列表接口支持的查询参数,都可以作为关键字参数传入list()方法。比如还可以用target_branch(目标分支)、labels(标签)、milestone(里程碑)等参数来进一步筛选。

内容的提问来源于stack exchange,提问作者flod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 00:50:29