调用gitlab接口合并merge request报错 406 branch can‘t be merged

问题背景

调用gitlab的合并merge request接口,得到返回值是406 branch can’t be merged

问题解决步骤

  1. 查询是否用户token没有赋予对应的操作权限
  2. 查询是否是保护分支等一系列问题

在排查了以上两个问题后发现依然是一样的报错,于是我决定请求获取merge request详情接口,将请求的返回结果列出来详细对比。终于找到了问题所在。

{
	"status_code": 200,
	"message": {
		"id": 9652,
		"iid": 4,
		"project_id": 876,
		"title": "333",
		"state": "opened",
		"created_at": "2023-07-20T17:51:55.941+08:00",
		"updated_at": "2023-07-20T17:51:55.941+08:00",
		"target_branch": "master",
		"source_branch": "branch002",
		"user_notes_count": 0,
		"upvotes": 0,
		"downvotes": 0,
		"author": {
			"id": 438,
			"username": "ext.qinghua.wu2",
			"name": "EXT Wu Qinghua(吴庆华)(UAES-XE/GST)",
			"state": "active",
			"avatar_url": "https://www.gravatar.com/avatar/4055081926d7684b91ec5a9fb8a439df?s=80&d=identicon",
			"web_url": "http://xe-git.uaes.com/ext.qinghua.wu2"
		},
		"assignees": [],
		"reviewers": [],
		"source_project_id": 876,
		"target_project_id": 876,
		"labels": [],
		"draft": false,
		"work_in_progress": false,
		"merge_when_pipeline_succeeds": false,
		"merge_status": "checking",
		"sha": "a52845acb2f4ed504a3d3f193db7769b1b322ac8",
		"reference": "!4",
		"references": {
			"short": "!4",
			"relative": "!4",
			"full": "test_community/lvjing0720Test03!4"
		},
		"web_url": "http://xe-git.uaes.com/test_community/lvjing0720Test03/-/merge_requests/4",
		"time_stats": {
			"time_estimate": 0,
			"total_time_spent": 0
		},
		"squash": false,
		"task_completion_status": {
			"count": 0,
			"completed_count": 0
		},
		"has_conflicts": false,
		"blocking_discussions_resolved": true,
		"subscribed": true,
		"changes_count": "1",
		"diff_refs": {
			"base_sha": "9ef8d915c2d20fdb7b0d06f0d1fe43c95445e13b",
			"head_sha": "a52845acb2f4ed504a3d3f193db7769b1b322ac8",
			"start_sha": "fccc7616173917873b254b2bd072d89839994acb"
		},
		"first_contribution": false,
		"user": {
			"can_merge": true
		}
	}
}

可以看到以上的返回结果中有一个字段叫merge_status,而值是checking,也就意味着gitlab正在checking代码是否有冲突等情况,是否可以合并。而merge_status目前看下来我看到有三种情况,分别是cannot_be_merged、checking、can_be_merged。只有在can_be_merged情况下才可以调用接口被正确合并,否则就会报错406 branch can’t be merged,或者其他错误。

相关参考链接:https://gitlab.com/gitlab-org/gitlab/-/issues/196962

你可能感兴趣的:(gitlab,gitlab)