如何通过python-gitlab正确设置GitLab合并请求删除源分支选项
解决python-gitlab设置合并请求“删除源分支”选项的问题
创建合并请求时直接设置
创建MR时,直接在create()方法的参数中指定should_remove_source_branch=True即可,示例代码:
import gitlab # 初始化GitLab连接 gl = gitlab.Gitlab("https://你的GitLab实例地址", private_token="你的私有令牌") project = gl.projects.get("你的项目ID或路径") # 创建带自动删除源分支的MR mr = project.mergerequests.create({ "source_branch": "你的源分支名", "target_branch": "目标分支名", "title": "MR标题", "should_remove_source_branch": True })
更新已存在的合并请求
对于已创建的MR,不能直接修改属性字典或调用错误的方法,必须使用edit()方法提交修改:
# 获取目标MR mr = project.mergerequests.get("MR的ID") # 更新删除源分支选项 mr.edit({"should_remove_source_branch": True})
注意事项
- 不要尝试用
mr.attributes['should_remove_source_branch'] = True或mr.should_remove_source_branch(True)这类写法,前者只是修改了本地对象的属性但不会同步到GitLab,后者是错误的调用方式(该属性是布尔值,不是可调用方法)。 - 确保你的python-gitlab是最新稳定版,旧版本可能存在参数支持问题,执行
pip install --upgrade python-gitlab升级。
内容的提问来源于stack exchange,提问作者user3732793
相关产品推荐
相关产品推荐

